C语言sizeof
一、关于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的更多相关文章
- C语言-sizeof()与strlen()的区别【转】
先看看sizeof() 一.sizeof的概念 sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.它并不是函数.sizeof操作符以字节形式给出了其操作数的存储大小.操作数可以是 ...
- c语言 sizeof理解
1.基本数据类型 char :1 short:2 int 4 long 4 long long :8 float:4 double :8字节. 2.数组:对应的基本数 ...
- C语言sizeof陷阱
执行以下程序,查看输出: #include <stdio.h> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) int ...
- c语言sizeof与strlen的区别
#include <stdio.h> #include <stdlib.h> #include <string.h> //strlen与sizeof的区别 //st ...
- C语言 - sizeof和strlen的区别
sizeof和strlen的区别: 1.sizeof操作符的结果类型是size_t,它在头文件中typedef为unsigned int类型. 该类型保证能容纳实现所建立的最大对象的字节大小. 2.s ...
- C 语言sizeof运算符
#include<stdio.h> int main() { ; ); ; int size3 = sizeof a; int size4 = sizeof(a); int size5 = ...
- C语言 sizeof()用法介绍
本文 转自https://www.cnblogs.com/huolong-blog/p/7587711.html 1. 定义 sizeof是一个操作符(operator). 其作用是返回 ...
- c语言sizeof用法(32位机)
- C语言sizeofkeyword
说明: ******C语言sizeof是keyword.是一个操作符.它不是一个函数.用于计算可变.或内存数据字节数占用类型. ******sizeof有三种不同的方式: ***sizeof(变量名) ...
随机推荐
- PHP的一些要点
1.用户评论的内容应当使用htmlspecialchars()函数进行过滤,如htmlspecialchars($_POST['content']);再写入数据库,防止用户评论中含有JS和HTML代码 ...
- Linux驱动设计—— 驱动调试技术
参考博客与书籍: <Linux设备驱动开发详解> <Linux设备驱动程序> http://blog.chinaunix.net/uid-24219701-id-2884942 ...
- 黑马程序员——JAVA基础之Map集合
------- android培训.java培训.期待与您交流! ---------- Map集合: 该集合存储键值对.一对一对往里存.而且要保证键的唯一性. 和Set很像,其实Set底层就是使用了M ...
- Docker系列之(二):使用Mesos管理Docker集群(Mesos + Marathon + Chronos + Docker)
1. Mesos简介 1.1 Mesos Apache Mesos 是一个分布式系统的管理软件,对集群的资源进行分配和管理. Mesos主要由以下几部分组成: Master: 管理各Slave节点 S ...
- 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 ...
- c++中的dictionary对象:map的使用备忘
#include <map> #include <iostream> using namespace std; void main(){ map <string, int ...
- 使用架构(XSD)验证XML文件
假使说XML是一个数据库,那么XSD就是这个数据库的结构.由此可见,XSD是如此重要,如果没有它,我们如何声明以及验证我们需要的XML数据文件的格式和合法性呢?那是不可能完成的任务,如果你将XML数据 ...
- oracle的高可用与负载均衡
浏览了一下Oracle官方的网页以及非官方的ppt,简单了解了一下Oracle提供的高可用方案.1. RACRAC, Real Application Clusters多个Oracle服务器组成一个 ...
- Nancy 简单学习
1. 环境 vs2010 , nancy 2. 需要测试的功能 1. 输出 页面输出简单文本 2. 输出json 数据 3. 操作 1. 创建asp.net 空项目 2. 添加引用Nancy库 使用n ...
- 09_android入门_采用android-async-http开源项目的GET方式或POST方式实现登陆案例
根据08_android入门_android-async-http开源项目介绍及使用方法的介绍,我们通过最常见的登陆案例进行介绍android-async-http开源项目中有关类的使用.希望对你学习 ...