一、关于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. 救援行动(save) (BFS)

    时间限制: 1 Sec  内存限制: 64 MB提交: 42  解决: 9[提交][状态][讨论版] 题目描述 Angel被人抓住关在一个迷宫了!迷宫的长.宽均不超过200,迷宫中有不可以越过的墙以及 ...

  2. mysql优化参数thread_cache_size

    thread_cache_size功能在mysql数据库配置文件中是非常重要的一项功能了,如果对thread_cache_size优化做得好我们可以让服务器跑得非常快,设置不好就会发现很小访问量就非常 ...

  3. Oracle数据库五种约束

    oracle 数据库 数据表的5个约束类型:1.主键约束2.外键约束3.唯一约束4.检查约束5.非空约束 主键约束:用来唯一标示表中的一个列,一个表中的主键约束只能有一个,但是可以在一个主键约束中包含 ...

  4. 自定义颜色显示的CheckBox

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. 转-OpenJDK源码阅读导航跟编译

    OpenJDK源码阅读导航 OpenJDK源码阅读导航 博客分类: Virtual Machine HotSpot VM Java OpenJDK openjdk 这是链接帖.主体内容都在各链接中.  ...

  6. ADS1110/ADS1271

    ADS1110 1.初始化 软件:设置p任意2个为GPIO口 硬件:设置p0.2,p0.3为SDA,SCL 输入.输出 ADS1110的I2C地址(1001aaa)例如ADS1110A0的地址是100 ...

  7. xUtils3的简单介绍

    xUtils3的简介 xUtils是基于Afinal开发的目前功能比较完善的一个Android开源框架,最近又发布了xUtil3.0,在增加新功能的同时又提高了框架的性能. 1.xUtils包含了很多 ...

  8. #linux包之sysstat之iostat命令

    概述 对于I/O-bond类型的进程,我们经常用iostat工具查看进程IO请求下发的数量.系统处理IO请求的耗时,进而分析进程与操作系统的交互过程中IO方面是否存在瓶颈.同vmstat一样,iost ...

  9. MySQL数据库update更新子查询

    比如: ? 1 2 3 4 UPDATE test.tb_vobile a set a.name = '111 ' WHERE a.id = (select max(id) id from test. ...

  10. ubuntu- eclipse、CDT安装

    eclipse的安装: 应用程序->附件->终端                                       然后输入(中间可能需要你输入密码):              ...