一、关于sizeof

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

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

二、demo

1.源码

test.c

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int i;
  6. printf("sizeof i is %d\n",sizeof i);
  7. /*
  8. 以下语句不屏蔽会提示:test.c:12: 错误: expected expression before ‘int’
  9. 因为,基本数据类型int前的关键字会被认为成是类型修饰符(类似static、const、extern等,而sizeof关键字不是类型修饰符)
  10. 正确写法:printf("sizeof(int) is %d\n",sizeof(int));
  11. */
  12. //printf("sizeof int is %d\n",sizeof int);
  13.  
  14. enum Color{
  15. GREEN = ,
  16. RED,
  17. BLUE,
  18. GREEN_RED = ,
  19. GREEN_BLUE
  20. }ColorVal;
  21. printf("sizeof ColorVal is %d\n",sizeof ColorVal);
  22. /*
  23. 以下语句不屏蔽会提示:test.c:26: 错误: expected expression before ‘enum’,原因同上。
  24. 正确写法:printf("sizeof(enum Color) is %d\n",sizeof(enum Color));
  25. */
  26. //printf("sizeof enum Color is %d\n",sizeof enum Color);
  27.  
  28. union check{
  29. int i;
  30. char ch;
  31. } c;
  32. printf("sizeof c is %d\n",sizeof c);
  33. /*
  34. 以下语句不屏蔽会提示:test.c:37: 错误: expected expression before ‘enum’,原因同上。
  35. 正确写法:printf("sizeof(union check) is %d\n",sizeof(union check));
  36. */
  37. //printf("sizeof union check is %d\n",sizeof union check);
  38.  
  39. struct list{
  40. int i;
  41. char ch;
  42. } a;
  43. printf("sizeof a is %d\n",sizeof a);
  44. /*
  45. 以下语句不屏蔽会提示:test.c:48: 错误: expected expression before ‘enum’,原因同上。
  46. 正确写法:printf("sizeof(struct list) is %d\n",sizeof(struct list));
  47. */
  48. //printf("sizeof struct list is %d\n",sizeof struct list);
  49. return ;
  50. }

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. PHP的一些要点

    1.用户评论的内容应当使用htmlspecialchars()函数进行过滤,如htmlspecialchars($_POST['content']);再写入数据库,防止用户评论中含有JS和HTML代码 ...

  2. Linux驱动设计—— 驱动调试技术

    参考博客与书籍: <Linux设备驱动开发详解> <Linux设备驱动程序> http://blog.chinaunix.net/uid-24219701-id-2884942 ...

  3. 黑马程序员——JAVA基础之Map集合

    ------- android培训.java培训.期待与您交流! ---------- Map集合: 该集合存储键值对.一对一对往里存.而且要保证键的唯一性. 和Set很像,其实Set底层就是使用了M ...

  4. Docker系列之(二):使用Mesos管理Docker集群(Mesos + Marathon + Chronos + Docker)

    1. Mesos简介 1.1 Mesos Apache Mesos 是一个分布式系统的管理软件,对集群的资源进行分配和管理. Mesos主要由以下几部分组成: Master: 管理各Slave节点 S ...

  5. Freezing Your Tuples Off 之 vacuum_freeze_min_age

    The vacuum_freeze_min_age setting determines the youngest XID which will be changed to FrozenXID on ...

  6. c++中的dictionary对象:map的使用备忘

    #include <map> #include <iostream> using namespace std; void main(){ map <string, int ...

  7. 使用架构(XSD)验证XML文件

    假使说XML是一个数据库,那么XSD就是这个数据库的结构.由此可见,XSD是如此重要,如果没有它,我们如何声明以及验证我们需要的XML数据文件的格式和合法性呢?那是不可能完成的任务,如果你将XML数据 ...

  8. oracle的高可用与负载均衡

    浏览了一下Oracle官方的网页以及非官方的ppt,简单了解了一下Oracle提供的高可用方案.1. RACRAC,  Real Application Clusters多个Oracle服务器组成一个 ...

  9. Nancy 简单学习

    1. 环境 vs2010 , nancy 2. 需要测试的功能 1. 输出 页面输出简单文本 2. 输出json 数据 3. 操作 1. 创建asp.net 空项目 2. 添加引用Nancy库 使用n ...

  10. 09_android入门_采用android-async-http开源项目的GET方式或POST方式实现登陆案例

    根据08_android入门_android-async-http开源项目介绍及使用方法的介绍,我们通过最常见的登陆案例进行介绍android-async-http开源项目中有关类的使用.希望对你学习 ...