ghosTzone
main(_){return _} && echo $? /* Keep It Simple Stupid | 本博客所有文章皆为原创 转载请注明 - ghosTM55 */

正确使用strlen计算字符数组长度

ghosTM55 posted @ 2008年11月22日 01:51 in C/C++ , 5833 阅读

#include<stdio.h>
#include<string.h>
/*#include<stddef.h>*/
int main(void)
{
        char str[]="helloworld!!!";
        size_t length=strlen(str);
        printf("%d\n",length);
        return 0;
}
 

  由于strlen()函数返回的值其实是size_t类型的,所以,采用size_t来声明一个记录数组长度的变量能够达到更好的移植性。数据类型 size_t是在stddef.h头文件中定义的,由于在stdio.h头文件中预处理了stddef.h,所以能够直接使用。

txi@ghosTunix:~$ date
2008年 05月 18日 星期日 21:14:45 CST


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter