一、关于sizeof

1.它是C的关键字、是一个运算符,不是函数;

2.一般用法为sizeof 变量或sizeof(数据类型);后边这种写法会让人误认为是函数,但这种写法是为了防止和C中类型修饰符(static、const、extern等)冲突。

二、demo

1.源码

test.c

#include <stdio.h>

int main()
{
int i;
printf("sizeof i is %d\n",sizeof i);
/*
以下语句不屏蔽会提示:test.c:12: 错误: expected expression before ‘int’
因为,基本数据类型int前的关键字会被认为成是类型修饰符(类似static、const、extern等,而sizeof关键字不是类型修饰符)
正确写法:printf("sizeof(int) is %d\n",sizeof(int));
*/
//printf("sizeof int is %d\n",sizeof int); enum Color{
GREEN = ,
RED,
BLUE,
GREEN_RED = ,
GREEN_BLUE
}ColorVal;
printf("sizeof ColorVal is %d\n",sizeof ColorVal);
/*
以下语句不屏蔽会提示:test.c:26: 错误: expected expression before ‘enum’,原因同上。
正确写法:printf("sizeof(enum Color) is %d\n",sizeof(enum Color));
*/
//printf("sizeof enum Color is %d\n",sizeof enum Color); union check{
int i;
char ch;
} c;
printf("sizeof c is %d\n",sizeof c);
/*
以下语句不屏蔽会提示:test.c:37: 错误: expected expression before ‘enum’,原因同上。
正确写法:printf("sizeof(union check) is %d\n",sizeof(union check));
*/
//printf("sizeof union check is %d\n",sizeof union check); struct list{
int i;
char ch;
} a;
printf("sizeof a is %d\n",sizeof a);
/*
以下语句不屏蔽会提示:test.c:48: 错误: expected expression before ‘enum’,原因同上。
正确写法:printf("sizeof(struct list) is %d\n",sizeof(struct list));
*/
//printf("sizeof struct list is %d\n",sizeof struct list);
return ;
}

C语言sizeof的更多相关文章

  1. C语言-sizeof()与strlen()的区别【转】

    先看看sizeof() 一.sizeof的概念 sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.它并不是函数.sizeof操作符以字节形式给出了其操作数的存储大小.操作数可以是 ...

  2. c语言 sizeof理解

    1.基本数据类型 char :1     short:2   int 4    long 4   long long :8    float:4    double :8字节. 2.数组:对应的基本数 ...

  3. C语言sizeof陷阱

    执行以下程序,查看输出: #include <stdio.h> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) int  ...

  4. c语言sizeof与strlen的区别

    #include <stdio.h> #include <stdlib.h> #include <string.h> //strlen与sizeof的区别 //st ...

  5. C语言 - sizeof和strlen的区别

    sizeof和strlen的区别: 1.sizeof操作符的结果类型是size_t,它在头文件中typedef为unsigned int类型. 该类型保证能容纳实现所建立的最大对象的字节大小. 2.s ...

  6. C 语言sizeof运算符

    #include<stdio.h> int main() { ; ); ; int size3 = sizeof a; int size4 = sizeof(a); int size5 = ...

  7. C语言 sizeof()用法介绍

    本文 转自https://www.cnblogs.com/huolong-blog/p/7587711.html   1.      定义 sizeof是一个操作符(operator). 其作用是返回 ...

  8. c语言sizeof用法(32位机)

  9. C语言sizeofkeyword

    说明: ******C语言sizeof是keyword.是一个操作符.它不是一个函数.用于计算可变.或内存数据字节数占用类型. ******sizeof有三种不同的方式: ***sizeof(变量名) ...

随机推荐

  1. URAL 1208 Legendary Teams Contest(DFS)

    Legendary Teams Contest Time limit: 1.0 secondMemory limit: 64 MB Nothing makes as old as years. A l ...

  2. IOS中如何获取手机的当前IP

    网上有许多类似的帖子.在搜索了资料以后.觉得下面的方法是最简单的. 使用的时候直接把类方法拖到自己新建的分类中就行. .h文件 #import <Foundation/Foundation.h& ...

  3. 对于python,一切事物都是对象,对象基于类创建

    新建列表.新建string字符串 li1 = [1, 2, 3, 4] li2 = list([1, 2, 3]) s1 = "abc" s2 = str("abc&qu ...

  4. JavaScript-闭包注意事项

    闭包允许内层函数引用父函数中的变量,但是该变量是最终值

  5. Linux-Memcache分布式部署方案(magent代理解决单点故障)

    Memcached的特点 Memcached作为高速运行的分布式缓存服务器具有以下特点. 1. 协议简单:memcached的服务器客户端通信并不使用复杂的MXL等格式, 而是使用简单的基于文本的协议 ...

  6. C#实现图片文件到数据流再到图片文件的转换 --转

    /----引入必要的命名空间 using System.IO; using System.Drawing.Imaging; //----代码部分----// private byte[] photo; ...

  7. OAuth2.0详解

    1.使用场景 A系统存放着订单信息 B系统需要查询A系统中的订单信息,但是必须要A系统验证通过后,才能查询. 此时,我们有两种验证方式: 1)拥有A系统的账户/密码 弊端是对A系统来说,直接提供账户/ ...

  8. WCF入门教程二[WCF应用的通信过程]

    一.概述 WCF能够建立一个跨平台的安全.可信赖.事务性的解决方案,是一个WebService,.Net Remoting,Enterprise Service,WSE,MSMQ的并集,有一副很经典的 ...

  9. 自定义Mvc5 Owin 验证

    public class AuthIn : IUserAuthenticate { public static ApplicationUserManager UserManager { get { r ...

  10. linux包之findutils之find和xargs命令

    解释 [root@localhost ~]# rpm -qa|grep findfindutils-4.4.2-6.el6.x86_64 find 路径 测试 动作 三个步骤的处理过程查找一个或多个目 ...