tags:

views:

72

answers:

3

The following code works fine

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

 main()
 open
 int a ;
 printf("This is testing code" );
 close

But If I exchange the lines

#define<stdio.h>
#define int char 

as

#define int char 
#define<stdio.h> 

it throws lot of errors like this

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 

Actually what is happening inside stdio.h ? Thanks in Advance.

+4  A: 

The reason for failure is that, #include<stdio.h> is replaced with the contents of stdio.h and when you replace int with char within the content, you break some declarations.

From /usr/include/bits/types.h which gets included indirectly through stdio.h

.
.
typedef unsigned short int __u_short;
.
.

Clearly when you replace int with char it becomes:

typedef unsigned short char __u_short;

Which causes compilation error as short cannot be applied to the char data type.

codaddict
+8  A: 

There are defined variables of type short int, long int and so on, which obviously fails when you change them through define to short char and long char.

Redefining basic C types is usually not a good idea.

Michal Čihař
I understand perfectly , I saw the changes after $cc -E filename.c
abubacker
+1 for the admonition about redefining basic types
David Gelhar
Redefining language tokens through the preprocessor (i.e., the 'open' and 'close' macros) is also a Very Bad Idea, and has been the source of much heartburn for me in the past. Not to mention that those particular definitions will bite you if you want to use the POSIX 'open' and 'close' functions.
John Bode
A: 

You #define will get activated and will replace all int's inside stdio.h and all the files it includes causing too many confusing errors.

Since, errors are from types.h, it must modifying some stuff like 'short int' to 'short char' etc.

Jay