【C语言难弄关键字1】----sizeof

【C语言难弄关键字1】----sizeof

关键字/函数?

sizeof是关键字,经常有人认为是函数,我们用编译器来测试一下它的一些用法
(测试环境为:centos 6.9虚拟机)

[klaus@localhost keyword_study]$ cat sizeof.c
#include <stdio.h>

int main(int argc,char *argv[])
{
        int i=0;
        printf("sizeof(int):    %d\n",sizeof(int) );
        printf("sizeof(i):      %d\n",sizeof(i) );
        printf("sizeof int:     %d\n",sizeof int );
        printf("sizeof i:       %d\n",sizeof i );
        return 0;
}
[klaus@localhost keyword_study]$ gcc sizeof.c      
[klaus@localhost keyword_study]$ ./a.out      
sizeof(int):    4
sizeof(i):      4
sizeof i:       4
[klaus@localhost keyword_study]$

sizeof i: 4 ,sizeof后面没有接括号也可以,但是对于一个函数来说,不加括号完全不行,由此看来sizeof并不是函数。

sizeof在计算变量所占用空间时,括号可以省略,但计算类型大小时不能省略,一般情况下,都带括号,伪装成函数。

注:本篇文章学习来自《c语言深度剖析》,侵权必删。

发布了80 篇原创文章 · 获赞 86 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/klaus_x/article/details/82961558