473,386 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Preprocessor in C

51
Hi,

Please help me to understand what really happens in the below code.
Expand|Select|Wrap|Line Numbers
  1. #define num 20
  2. main()
  3. {
  4. printf("%d,",num);
  5. func();
  6. printf("%d",num);
  7. }
  8. void func()
  9. {
  10. #undef num
  11. #define num 50
  12. }
  13.  
The output is 20,20.
I expect the output to be 20,50
It seems to be strange to understand. Help me

Thanks in advance,
Chella
Oct 4 '07 #1
6 1494
Meetee
931 Expert Mod 512MB
Hi,

I had read somewhere "Macros are evils" but today I have experienced it after finding your problem's solution. I am getting output 50,50!! (Linux)

Experts can throw some light on this issue.

Regards
Oct 4 '07 #2
Savage
1,764 Expert 1GB
Hi,

Please help me to understand what really happens in the below code.

#define num 20
main()
{
printf("%d,",num);
func();
printf("%d",num);
}
void func()
{
#undef num
#define num 50
}

The output is 20,20.
I expect the output to be 20,50
It seems to be strange to understand. Help me

Thanks in advance,
Chella
Ok,let's see:

If I run your program I get 20,20( Dev-C++ on WinXp),but if you comment the function and add
Expand|Select|Wrap|Line Numbers
  1. #undef num
  2. #define num 50
directly into main I got 20,50 which is correct.

Now,zoddila got 50,50 on Linux which makes me think that result is platform depended.But when I tried the following:

Expand|Select|Wrap|Line Numbers
  1. void func()
  2. {
  3.  #define num2 50
  4. }
and tried calling it in main it said that num2 is undefined,so it must reside on stack which is cleared when function finishes.So,furthermore I tried:

Expand|Select|Wrap|Line Numbers
  1. void func()
  2. {
  3.  #undef num    
  4.  #define num 50
  5.  printf("%d\n",num);
  6. }
and result was correct.As the conclusion:

Never define a macro inside a function.
You can define it in main and it will seem as normal but it isn't.Macro will have other value in other functions,so always declare them in global namespace..

Savage
Oct 4 '07 #3
Meetee
931 Expert Mod 512MB
Thanks Savage for explanation

Regards
Oct 4 '07 #4
Savage
1,764 Expert 1GB
Thanks Savage for explanation

Regards
I' more then happy to help you two..:D

Savage
Oct 4 '07 #5
Banfa
9,065 Expert Mod 8TB
Also remember that macros are handled by the preprocessor, as are most # direcectives before the code ever gets anywhere near the compiler.

I recomend as an exercise that, if your compiler supports it, you output the code after preprocessing. This is the actual code compiled and normally you will find that the file is very long (because all headers have been included) has large amounts of blank lines (from the sections in the headers that are removed though preprocessor directives) and contains no macros, only the values that they had set.

In the original program by the time the code of function is executed the preprocessor statements it contained are long gone, they do not translate to code and macros are not variables. Macros are text substitutions in the code.
Oct 4 '07 #6
chella
51
Thank you very much. All the replies you gave for this problem are valuable!!

Regards,
Chella
Oct 4 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
24
by: Nudge | last post by:
I have an array, and an unrolled loop which looks like this: do_something(A); do_something(A); .... do_something(A); I thought: why should I type so much? I should write a macro. So I was...
16
by: Trying_Harder | last post by:
Is it possible to redefine a macro with global scope after undefining it in a function? If yes, could someone explain how? /If/ my question above isn't very clear you can refer to the...
13
by: seemanta dutta | last post by:
Greetings C gurus, I have used preprocessor directives since a very long time. But whenever I see some professional piece of C code, the linux kernel for example, I get literally confused by the...
18
by: /* frank */ | last post by:
My teacher said that array in C is managed by preprocessor. Preprocesser replace all array occurences (i.e. int a ) with something that I don't understand/remember well. What's exactly happens...
13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
9
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour....
2
by: Paolo | last post by:
I imported a VC++6.0 project into VC++7.1. The conversion operation makes a mess with Preprocessor Definitions, adding a "$(NoInherit)" for each file. For example: I had a DLL project in VC++6.0...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.