zoukankan      html  css  js  c++  java
  • sizeof运算符

    sizeof是一个单目运算符,它的运算对象是变量或数据类型,运算结果为一个整数。运算的一般形式如下:

    sizeof(<类型或变量名>) 

    它只针对数据类型,而不针对变量!

    若运算对象为变量,则所求的结果是这个变量占用的内存空间字节数;若运算对象是数据类型,则所求结果是这种数据类型的变量占用的内存空间字节数。

    sizeof是一个使用频率很高的操作符,经常用来获取变量或数据类型所占用的内存空间的大小,下面的程序显示了sizeof的用法。

    #include <stdio.h>

    struct Student

    {

            int number;

         char name[8];

    };

    enum season{

         spring,s ummer, fall, winter

    };

     

    int main()

    {

        int a = 10;

        float b = 3.5;

        struct Student s1 = {1, “zhangsan”};

        enum season myseason;

       

        printf ("the size of char is %d bytes ",sizeof(char));

        printf ("the size of short is %d bytes ",sizeof(short));

        printf ("the size of int is %d bytes ",sizeof(int));

        printf ("the size of a is %d bytes ",sizeof(a));

        printf ("the size of long is %d bytes ",sizeof(long));

        printf ("the size of long long is %d bytes ",sizeof(long long));

        printf ("the size of float is %d bytes ",sizeof(float));

        printf ("the size of b is %d bytes ",sizeof(b));

        printf ("the size of double is %d bytes ",sizeof(double));

        printf ("the size of struct Student is %d bytes ",sizeof(struct Student));

        printf ("the size of enum season is %d bytes ", sizeof (enum season));

        printf ("the size of myseason is %d bytes ", sizeof (myseason));

     

         return 0;

    }

    程序执行结果如下:

    linux@ubuntu:~/book/ch4$ cc test.c –o test -Wall

    linux@ubuntu:~/book/ch4$./test

    the size of char is 1 bytes

    the size of short is 2 bytes

    the size of int is 4 bytes

    the size of a is 4 bytes

    the size of long is 4 bytes

    the size of long long is 8 bytes

    the size of float is 4 bytes

    the size of b is 4 bytes

    the size of double is 8 bytes

    the size of struct Student is 12 bytes

    the size of enum season is 4 bytes

    the size of myseason is 4 bytes

    从该结果中,可以清楚地看到不同数据类型及变量所占的字节数,读者应该熟悉这些结果。还可以看到,变量所占用的空间,由其数据类型决定,与变量的值没有关系。

  • 相关阅读:
    负载均衡器部署方式和工作原理
    Android 有关于* daemon not running.starting it now on port 5037 *ADB
    微信开发常用文档及参考资料
    XML解析之sax解析案例(二)使用sax解析把 xml文档封装成对象
    XML解析之sax解析案例(一)读取contact.xml文件,完整输出文档内容
    XML解析之SAX解析过程代码详解
    通过PHP current()函数获取未知字符键名数组第一个元素的值
    PHP检测链接是否是SSL连接 ,也就是判断HTTPS
    PHP反射ReflectionClass、ReflectionMethod 入门教程
    PHP 反射API说明
  • 原文地址:https://www.cnblogs.com/wangjinshan/p/7063074.html
Copyright © 2011-2022 走看看