关于sizeof
所熟知的sizeof的用法这里不表,下面直接给出体现问题的代码。
1 #define _CRT_SECURE_NO_WARNINGS 2 3 #include<stdio.h> 4 #include<string.h> 5 6 #define PRISE "what a super marvelous name!" 7 8 int main(void){ 9 char name[40]; 10 printf("What's your name?\n"); 11 scanf("%s",name); 12 printf("Hello, %s. %s\n", name, PRISE); 13 pringf("Your name of %d letters occupies %d memory cells\n", strlen(name), sizeof name); 14 pringf("The phrase of praise has %d letters", strlen(PRISE)); 15 printf("and pccupies %d memory cells. \n", sizeof PRISE); 16 return 0; 17 }
该例子中sizeof在调用时,并没有加上圆括号。
是否使用圆括号取决于你是想获取一个类型的大小还是想获取某个具体量的大小。
圆括号对于类型是必要的,而对于具体量则是可选的。
也就是说,应该用sizeof(char)或sizeof(int),但是也可以使用sizeof name或sizeof 6.28。不过在所有情况下都加括号会更好,例如,sizeof(6.28)。

浙公网安备 33010602011771号