Abubacker Nainamohamed
Abubacker Nainamohamed

Reputation: 4797

Confusion in macro

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

#include<stdio.h>
#define int char 

as

#define int char 
#include<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 ?

Upvotes: 2

Views: 365

Answers (3)

codaddict
codaddict

Reputation: 455440

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.

Upvotes: 5

Jay
Jay

Reputation: 24905

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.

Upvotes: 0

Michal Čihař
Michal Čihař

Reputation: 10091

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.

Upvotes: 8

Related Questions