一、关于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. PC端的混合应用通讯问题

    exe使用C#开发,内嵌HTML页面HTML页面与exe程序的通讯方式可以使用以下方式: HTML通知exe:C#有个titlechange事件,可以监听内部HTML的title,那么HTML就可以通 ...

  2. Reverse a Singly LinkedList

    Reverse a Singly LinkedList * Definition for singly-linked list. * public class ListNode { * int val ...

  3. PADS Logic 常见错误报告内容

    1.PCB Decal LED0805 not found in Library pcb封装不在库中. 找到原图中的pcb-save to library 未分配PCB时候,右键Edit part-找 ...

  4. CUDA学习笔记(二)【转】

    来源:http://luofl1992.is-programmer.com/posts/38847.html 编程语言的特点是要实践,实践多了才有经验.很多东西书本上讲得不慎清楚,不妨自己用代码实现一 ...

  5. docker学习3-虚拟网络模式

    一.虚拟机网络模式 在理解docker网络隔离前,先看下之前虚拟机里对网络的处理,VirtualBox中有4中网络连接方式: NAT Bridged Adapter Internal Host-onl ...

  6. NDK环境搭建

  7. android XML解析器全解案例

    1.使用pull解析 package com.example.myxml; import java.io.InputStream; import java.util.ArrayList; import ...

  8. hydra

    转:http://www.cnblogs.com/patf/p/3142564.html 1.yum -y install openssl-devel pcre-devel ncpfs-devel p ...

  9. 【转】第7篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:全自动注册与反射方法分析

    作者: 牛A与牛C之间 时间: 2013-12-12 分类: 技术文章 | 2条评论 | 编辑文章 主页 » 技术文章 » 第7篇:Xilium CefGlue 关于 CLR Object 与 JS ...

  10. sqlserver 字符串拼接及拆开联表查询的问题

    一.sql根据一个以逗号隔开的人员guid类型的ID字符串查出其对应的姓名同样拼接成逗号隔开的字符串: 1.需求:管理员发送通知(通知分为普通通知,奖品订单,调查问卷三种类型)给用户,并且可以查看统计 ...