宏观上的困惑

abu*_*ker 2 c c-preprocessor

以下代码工作正常

#define open {
#define close }
#include<stdio.h>
#define int char

 main()
 open
 int a ;
 printf("This is testing code" );
 close
Run Code Online (Sandbox Code Playgroud)

但如果我交换线路

#include<stdio.h>
#define int char 
Run Code Online (Sandbox Code Playgroud)

#define int char 
#include<stdio.h> 
Run Code Online (Sandbox Code Playgroud)

它会抛出很多像这样的错误

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 
Run Code Online (Sandbox Code Playgroud)

实际上stdio.h中发生了什么?

Mic*_*hař 8

有类型的定义的变量short int,long int等等,当你改变他们通过定义来这显然无法short charlong char.

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


cod*_*ict 5

失败的原因是,#include<stdio.h>替换为内容,stdio.h当您在内容中替换intchar,您会破坏一些声明.

/usr/include/bits/types.h这些被间接地通过包括stdio.h

.
.
typedef unsigned short int __u_short;
.
.
Run Code Online (Sandbox Code Playgroud)

显然当你intchar它替换它时:

typedef unsigned short char __u_short;
Run Code Online (Sandbox Code Playgroud)

这导致编译错误,因为short无法应用于char数据类型.