zoukankan      html  css  js  c++  java
  • typedef 和 #define 的区别

    typedef: 为数据类型创建别名;

    如:typedef char int8;

    #define: 用宏名表示一个字符串;

    如:#define INT8 char

    二者的区别:

    1.可以用其他类型说明符对宏类型名进行扩展修饰,但对 typedef 所定义的类型却不能这样做;

    如:typedef char int8;

          #define INT8 char

          unsigned INT8 i;    // 没有问题

          unsigned int8 j;     // 错误,非法

    2.在连续的几个变量的声明中,用 typedef 定义的类型能够保证声明中所有的变量均为同一种类型,但用 #define 定义的类型则无法保证。

    如:typedef char* p_int8;

          #define P_INT8 char*

          p_int8 i1, i2;    // i1 和 i2 均为指向 char 的指针

          P_INT8  j1, j2;   // j1 为指向 char 的指针, 但 j2 为char 型变量

    3. typedef 结尾需要加“;”,#define 不需要

    后记:在平常的应用中,一般用 typedef 封装数据类型,用#define 定义常量(数字/字符串)

  • 相关阅读:
    java 代码规范 sun 公司
    软引用、弱引用、虚引用
    socket
    httpURLConnection、URL、httpClient、httpPost、httpGet
    android service aidl 理解
    Python2.7-codecs
    Python2.7-textwrap
    Python2.7-StringIO和cStringIO
    Python2.7-difflib
    Python2.7-struct模块
  • 原文地址:https://www.cnblogs.com/Waming-zhen/p/5555615.html
Copyright © 2011-2022 走看看