从外部特性来看:

#include <stdio.h> #include <string> int main() { char array[] = "12345"; int n = sizeof(array); // n is 6 int m = strlen(array); // m is 5 array[3] = 0; n = sizeof(array); // n is 6 m = strlen(array); // m is 3 return 0; }

两者最本质的区别在于sizeof是操作符,而strlen是函数。

他们在MSDN中的定义分别为:

sizeof Operator

sizeof expression

The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t.

The expression is either an identifier or a type-cast expression (a type specifier enclosed in

parentheses).

When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment. When applied to a statically dimensioned array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of dynamically allocated arrays or external arrays.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

strlen

Get the length of a string.

Routine Required Header:

strlen <string.h>

size_t strlen( const char *string );

Parameter

string:Null-terminated string

Libraries

All versions of the C run-time libraries.

Return Value

Each of these functions returns the number of characters in string, excluding the terminal

NULL. No return value is reserved to indicate an error.

Remarks

Each of these functions returns the number of characters in string, not including the

terminating null character. wcslen is a wide-character version of strlen; the argument of

wcslen is a wide-character string. wcslen and strlen behave identically otherwise.

可见strlen是针对string的,而sizeof仅仅是一个keyword.

大部分编译器在编译时就计算了sizeof的值,而strlen则是在程序执行过程中计算的。

sizeof计算出的是该类型所占内存的大小,而strlen计算出的则是字符串的实际长度。

Evaluating sizeof expr does not evaluate the expression.

The result of applying sizeof depends in part on the type involved:

l  sizeof char or an expression of type char is guaranteed to be 1

l  sizeof a reference type returns the size of the memory necessary to contain an object of the referenced type

l  sizeof a pointer returns the size needed hold a pointer; to obtain the size of the object to which the pointer pointers, the pointer must be dereferenced

l  sizeof an array is equivalent to taking the sizeof the element type times the number of elements in the array

sizeof()与strlen()的区别与联系相关推荐

  1. Sizeof与Strlen的区别与联系(转)

    一.sizeof sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型.对象.函数等.     它的功能是:获得保证能 ...

  2. C语言中sizeof与strlen的区别总结!

    C语言中sizeof与strlen区别总结! 先上一个例子: 结果: 一.sizeof与strlen char c[] = "abc\0cba"; 问题1:sizeof(c) = ...

  3. Sizeof与Strlen的区别与联系

    Sizeof与Strlen的区别与联系(转) 1.sizeof操作符的结果类型是size_t,它在头文件中typedef为unsigned int类型. 该类型保证能容纳实现所建立的最大对象的字节大小 ...

  4. C语言 - sizeof和strlen的区别

    sizeof和strlen的区别: 1.sizeof操作符的结果类型是size_t,它在头文件中typedef为unsigned int类型. 该类型保证能容纳实现所建立的最大对象的字节大小. 2.s ...

  5. sizeof和strlen的区别(其中涉及NUL的讲解)

    本文是自己结合平时所学的知识,对sizeof和strlen的区别进行了总结,如有不对的地方还请批评指证,共同进步!!! 一.从C语言的定义上来讲 1.sizeof是关键字,而strlen是包含在str ...

  6. C语言 sizeof 和 strlen 函数区别 - C语言零基础入门教程

    目录 一.sizeof 函数与 strlen 函数区别 1.获取字符串长度 – 针对字符串 2.获取指针/数组长度 – 针对指针/数组 3.sizeof 获取内存大小 4.经典案例 二.猜你喜欢 零基 ...

  7. 详说sizeof与strlen的区别与联系

    sizeof是C/C++中的关键字,它是一个运算符,其作用是取得一个对象(数据类型或者数据对象)的长度(即占用内存的大小,以byte为单位). 当参数分别如下时,sizeof返回的值表示的含义如下: ...

  8. 简述sizeof和strlen的区别

    sizeof和strlen的区别 属性不同: sizeof是运算符(关键字),strlen是函数 用法不同: sizeof可以使用类.结构体.共用体以及其他用户自定义的数据类型作为参数,然后计算相应的 ...

  9. sizeof与strlen的区别

    以下内容参考自<程序员面试宝典> sizeof与strlen的区别 1. sizeof操作符的结果类型是size_t,它在头文件中typedef为unsigned int类型.该类型保证能 ...

  10. C语言中的sizeof()和strlen()的区别

    C语言中的sizeof和strlen的区别 sizeof() strlen 大量案例(想深入了解可以看) 1.(花括号int的sizeof()) 2.(花括号char的sizeof()) 3.(字符串 ...

最新文章

  1. C#截取屏幕保存图片
  2. solr参数之facet
  3. mysql 标识符规则_MySQL 标识符到底区分大小写么——官方文档告诉你
  4. C#编译和运行过程图例
  5. java 8 新功能详解_Java 8的8个新功能
  6. JavaFX真实世界应用程序:欧洲电视网广播联盟
  7. beforeRouteEnter,beforeRouteLeave函数
  8. npm 发布一个全局的指令
  9. C Tricks(十三)—— trim 的实现
  10. 7-1 字符串的冒泡排序 (20 分)
  11. R实现地理位置与经纬度相互转换
  12. Dalvik源码阅读笔记(二)
  13. [Java] java打飞机小游戏
  14. i710870h和i710875h有多大差距?哪个好?
  15. 如何编辑已存在的.fig文件
  16. C++ Qt实现WPS、浏览器tab页面
  17. LiveZilla 详细 配置 设置 (四) 查看成果及总结
  18. 免费压缩视频大小最佳方法?
  19. (计算机体系结构)7.3 基于硬件的前瞻执行
  20. C++11 auto关键字的用法

热门文章

  1. Object-c:NSString的创建
  2. Apache服务器配置技巧
  3. 基于SSM实现餐饮管理系统
  4. MySQL 查看约束,添加约束,删除约束 添加列,修改列,删除列
  5. dubbo在idea下的使用创建 服务者,消费者 注册中心
  6. node中模块、AMD与CMD、ES6模块,node中使用ES6
  7. 46:八进制到十进制
  8. 5月23日 JavaScript
  9. H5学习系列之Communication API
  10. java多线程机制2(安全问题)