[c]数组与字符串长度

2017-3-16 写技术

来看看在C语言里面字符串与数组的长度怎么取得吧,不多说,上代码:

        char *str="abcdefg";
        char strq[10] = "abcdefg";
        printf("%d     %d\n", sizeof(str), strlen(str));
        printf("%d     %d\n", sizeof(strq), strlen(strq));

结果是:

8        7

10        7


/***************我是分隔线*************************************/

也就是说:

sizeof by an array will get length of array no point with it's content.

sizeof by a point of const string will get the length of the space which contained the string.

strlen by any type of string point will both get the length of string itself, of cause without the ending char '\0'.

标签: C

发表评论:

Powered by anycle 湘ICP备15001973号-1