繁体   English   中英

宏观上的困惑

[英]Confusion in macro

以下代码工作正常

#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中发生了什么?

定义了short intlong int等类型的变量,当你通过define更改为short charlong char时,这些变量显然会失败。

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

失败的原因是, #include<stdio.h>被替换为stdio.h的内容,当你在内容中用char替换int时,你会破坏一些声明。

来自/usr/include/bits/types.h ,它通过stdio.h间接包含

.
.
typedef unsigned short int __u_short;
.
.

很明显,当你用char替换int ,它变为:

typedef unsigned short char __u_short;

这导致编译错误,因为short不能应用于char数据类型。

#define将被激活并将替换stdio.h中的所有int以及它包含的所有文件,从而导致太多混乱的错误。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM