_countof、sizeof、strlen的区别和用法
2014-03-12 22:25
423 查看
1 countof:是系统定义的一个宏,求取元素的数组的元素个数
2 sizeof:运算符,在头文件中typedef为unsigned
int,其值在编译时即计算好了,获得保证能容纳实现所建立的最大对象的字节大小
3 strlen:是一个函数,在运行时执行,返回字符串的长度。该字符串可能是自己定义的,也可能是内存中随机的,该函数实际完成的功能是从代表该字符串的第一个地址开始遍历,直到遇到结束符NULL。返回的长度大小不包括NULL
2 sizeof:运算符,在头文件中typedef为unsigned
int,其值在编译时即计算好了,获得保证能容纳实现所建立的最大对象的字节大小
3 strlen:是一个函数,在运行时执行,返回字符串的长度。该字符串可能是自己定义的,也可能是内存中随机的,该函数实际完成的功能是从代表该字符串的第一个地址开始遍历,直到遇到结束符NULL。返回的长度大小不包括NULL
// crt_countof.cpp #define _UNICODE #include <stdio.h> #include <stdlib.h> #include <tchar.h> int main( void ) { _TCHAR arr[20], *p; printf( "sizeof(arr) = %d bytes\n", sizeof(arr) ); printf( "_countof(arr) = %d elements\n", _countof(arr) ); // In C++, the following line would generate a compile-time error: // printf( "%d\n", _countof(p) ); // error C2784 (because p is a pointer) _tcscpy_s( arr, _countof(arr), _T("a string") ); // unlike sizeof, _countof works here for both narrow- and wide-character strings }
sizeof(arr):40个字节 _countof(arr):20个元素
相关文章推荐
- [转,不错]sizeof()用法汇总&&和strlen的区别
- C语言中sizeof与strlen的用法区别
- strlen与sizeof的区别和用法
- C语言中的sizeof操作符用法及和strlen的区别
- sizeof与strlen的用法与区别
- char *s和char s[]的区别与联系以及strlen()和sizeof()的用法
- 记录wcslen,strlen,sizeof,_coutof等的用法和区别
- sizeof 与 strlen 的一些用法及区别
- strlen和sizeof的区别,以及extern的用法
- sizeof,strlen,length的用法区别
- 关于sizeof和strlen的区别和用法
- Sizeof与strlen区别以及用法
- sizeof和strlen的一些用法和区别
- C语言中sizeof与strlen的区别是什么
- C语言中sizeof与strlen区别
- sizeof与strlen的区别
- sizeof与strlen的区别
- sizeof和strlen的区别(其中涉及NUL的讲解)
- sizeof 与 strlen 的区别及使用
- Sizeof与Strlen的区别与联系