长度计数中是否包含char null终止符 [英] is that char null terminator is including in the length count

查看:85
本文介绍了长度计数中是否包含char null终止符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include< stdio.h> 

int main(int argc,char * argv []){
char s [] = help;
printf(%d,strlen(s));
}

为什么上面的输出是4,不是5是正确的答案吗? / p>

在内存中应为'h','e','l','p','\0'。



谢谢。

解决方案

strlen :返回给定字节串的长度,不包括空终止符;

  char s [] = help; 
strlen(s)应该返回4。

sizeof :返回给定字节字符串的长度,包括空终止符;

  char s [] = help ; 
sizeof(s)应该返回5。


#include <stdio.h>

int main(int argc, char *argv[]) {
   char s[]="help";
   printf("%d",strlen(s));  
}

Why the above output is 4, isnt that 5 is the correct answer?

it should be 'h','e','l','p','\0' in memory..

Thanks.

解决方案

strlen: Returns the length of the given byte string not including null terminator;

char s[]="help";
strlen(s) should return 4.

sizeof: Returns the length of the given byte string, include null terminator;

char s[]="help";
sizeof(s) should return 5.

这篇关于长度计数中是否包含char null终止符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆