代码之家  ›  专栏  ›  技术社区  ›  abubacker

宏观混乱

  •  2
  • abubacker  · 技术社区  · 14 年前

    下面的代码可以正常工作

    #define open {
    #define close }
    #include<stdio.h>
    #define int char
    
     main()
     open
     int a ;
     printf("This is testing code" );
     close
    

    #include<stdio.h>
    #define int char 
    

    作为

    #define int char 
    #include<stdio.h> 
    

    In file included from /usr/include/stdio.h:36,
                     from print.c:19:
    /usr/include/bits/types.h:35: error: both 'short' and 'char' in declaration specifiers
    /usr/include/bits/types.h:37: error: both 'long' and 'char' in declaration specifiers
    /usr/include/bits/types.h:42: error: both 'short' and 'char' in declaration specifiers
    /usr/include/bits/types.h:43: error: both 'short' and 'char' in declaration specifiers
    .................................................
    so and so 
    

    实际上在stdio.h里面发生了什么?

    3 回复  |  直到 8 年前
        1
  •  5
  •   codaddict    14 年前

    失败的原因是, #include<stdio.h> 替换为 stdio.h int 具有 char 在内容中,您打破了一些声明。

    /usr/include/bits/types.h 它通过

    .
    .
    typedef unsigned short int __u_short;
    .
    .
    

    很明显当你更换 内景 具有 它变成:

    typedef unsigned short char __u_short;
    

    这会导致编译错误 short 不能应用于 数据类型。

        2
  •  8
  •   Michal Čihař    14 年前

    已定义类型为的变量 short int long int 以此类推,当您通过define to更改它们时,显然会失败 short char long char .

    重新定义基本的C类型通常不是一个好主意。

        3
  •  0
  •   Jay    14 年前

    因为,错误来自types.h,所以它必须修改一些东西,比如'short int'到'short char'等等。