最滥帖惩罚计划 [1]
/*
Reference:
主 题: 关于sizeof的问题?达人来看一下~_~
http://community.csdn.net/Expert/topic/4747/4747505.xml?temp=.5877039
*/
#include <stdio.h>
int main(void)
{
char arr[5] = "hello, world";
printf("%i\n", sizeof(arr));
}
$ gcc -W -Wall -std=c99 -pedantic test.c
test.c: In function `main':
test.c:5: warning: initializer-string for array of chars is too long
$ ./a.out
5
$
char arr[5] = "hello";
用字符串字面值初始化数组,数组的第一个元素被初始化为字符串中的第一个字符,如此递推。字符串太长,超过字符数组声明中的长度时,多余字符被截断,但不算错误。(C++中则为错误)。该语句与下面语句等效:
char arr[5] = {'h', 'e', 'l', 'l', 'o'};/* 不计'\0' */