1. 程式人生 > >C 標準庫 - string.h之strlen使用

C 標準庫 - string.h之strlen使用

rac ati oob [] mine eat including 定義 mina

strlen

  • Returns the length of the C string str.
  • The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).
  • 返回給定空終止字符串的長度,即首元素為 str 所指,且不包含首個空字符的字符數組中的字符數。
  • 計算字符串 str 的長度,直到空結束字符,但不包括空結束字符。
  • 若 str 不是指向空終止字節字符串的指針則行為未定義。

    size_t strlen( const char *str );

Parameters

str

  • C string.
  • 要計算長度的字符串。

Return Value

  • The length of string.
  • 該函數返回字符串的長度。

Example

//
// Created by zhangrongxiang on 2018/2/7 10:41
// File strlen
//
#include <stdio.h> #include <string.h> //Software is like sex: it"s better when it"s free. //My name is Linus, and I am your God //Linux is obsolete. //Linux is not Unix int main() { char *str = "My name is Linus, and I am your God"; size_t length = strlen(str); printf("%s --> length is %d
\n", str, (int) length); char *str2 = "一切皆文件"; length = strlen(str2); printf("%s -- length --> %d\n", str2, (int) length); //15 = 3 * 15 printf("%s -- sizeof --> %d\n", str2, (int) sizeof("一切皆文件")); //16 = 3 * 5 + 1 char str3[] = "萬物皆對象"; printf("%s -- sizeof --> %d\n", str3, (int) sizeof(str3)); //16 = 3 * 5 + 1 return 0; }

文章參考

  • http://zh.cppreference.com/w/c/string/byte/strlen
  • http://www.cplusplus.com/reference/cstring/strlen/
  • http://www.runoob.com/cprogramming/c-function-strlen.html

轉載註明出處

C 標準庫 - string.h之strlen使用