2010-03-09 125 views
2

下面的代碼工作正常混亂宏觀

#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裏面發生了什麼?

+2

不是應該的#include codaddict 2010-03-09 12:19:26

回答

5

失敗的原因是,#include<stdio.h>被替換的stdio.h的內容,當你的內容中與char取代int,你打破了一些聲明。

/usr/include/bits/types.h它獲取當你與char替換int通過stdio.h

. 
. 
typedef unsigned short int __u_short; 
. 
. 

顯然間接包含它變成:

typedef unsigned short char __u_short; 

這會導致編譯錯誤,因爲short不能應用於char數據類型。

8

有定義short int類型,long int的變量等等,當你改變他們通過定義來short charlong char這顯然失敗。

重新定義基本的C類型通常不是一個好主意。

+1

我完全理解,我看到的變化之後$ cc -E filename.c – abubacker 2010-03-09 12:22:21

+0

+1關於重新定義基本類型的告誡 – 2010-03-09 12:47:40

+0

通過預處理器重新定義語言令牌(即'open'和'close'宏)也是一個非常糟糕的想法,並且一直是源代碼過去對我來說很灼熱。更不用說,如果你想使用POSIX的「開放」和「關閉」功能,那些特定的定義會咬你。 – 2010-03-09 16:08:48

0

你#define將被激活,並將取代所有int的內部stdio.h及其包含的所有文件,導致太多令人困惑的錯誤。

因爲,錯誤是由types.h中,它必須改變一些東西,像「短整型」到「短焦」等