关于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 namesizeof 6.28。不过在所有情况下都加括号会更好,例如,sizeof(6.28)

posted @ 2020-04-20 19:03  JimAmadeus  阅读(100)  评论(0)    收藏  举报