954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Preprocessor and typedef

We know #define statements are executed before the program is compiled (preprocessor directives) and so the characters used should have no other meaning (say keywords),but still the following statement is not possible and is erroneous why ?

#define int float


How many passes would the preprocessor directives run for as for the following type of statements,more than one pass would be required...

#define a b
#define b c
#define c a


Sort of confused right now...so thought of asking you guys...!!! Thanks in advance !!!

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

It seems it's your homework... See this forum rules... ;)
Some tips:
1. the scanner (lexical analyser) works "before" preprocessor so your 1st statement is wrong...
2. The C preprocessor is a one-pass text processor...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 
It seems it's your homework... See this forum rules... ;) Some tips: 1. the scanner (lexical analyser) works "before" preprocessor so your 1st statement is wrong... 2. The C preprocessor is a one-pass text processor...


No buddy its not my homework and thanks to you moderators that I know how this community works and have great respects for it.

I searched for answers in http://c-faq.com and others too didn't find a satisfying answer so asking you guys to clear my own programming doubts.

#define is dealt with by preprocessor right and not lexixal analyser ?

And as preprocessor is a one pass text processor cyclic replacements is impossible right?

And my earlier query of why

#define int float

is still not clear...

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

I tested the following:

#include <stdio.h>

#define int float

int main(void)
{
        int foo = 1.12;

        printf("Foo: %f\n", foo);

        return 0;
}


Which resulted in the following:

jsomers@devlin04 test $ gcc define.c
define.c: In function `main':
define.c:6: warning: return type of `main' is not `int'
jsomers@devlin04 test $ ./a.out
Foo: 1.120000


So it seems to work here.

tyfius
Newbie Poster
2 posts since Mar 2009
Reputation Points: 10
Solved Threads: 1
 

>And my earlier query of why
#define int float
>is still not clear...

You are substituting in your code, every wordint for the word float.
That's all.
So, what happens when for example you write?

float main(void)

It doesn't work properly, correct? Because main should return an int.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

Thanks everyone i was misguided by my own doubts.... ;) Thanks for helping.

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You