以下代码工作正常
#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中发生了什么?
失败的原因是,#include<stdio.h>替换为内容,stdio.h当您在内容中替换int时char,您会破坏一些声明.
从/usr/include/bits/types.h这些被间接地通过包括stdio.h
.
.
typedef unsigned short int __u_short;
.
.
Run Code Online (Sandbox Code Playgroud)
显然当你int用char它替换它时:
typedef unsigned short char __u_short;
Run Code Online (Sandbox Code Playgroud)
这导致编译错误,因为short无法应用于char数据类型.