473,394 Members | 1,746 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,394 software developers and data experts.

scope of preprocessor definition

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #define a 10
  3. void foo(void);
  4. main( )
  5. {
  6. printf("%d\n", a);
  7. foo( );
  8. printf("%d\n", a);
  9. }
  10. void foo(void)
  11. {
  12. #undef a
  13. #define a 50
  14. }
Why it is producing output as
10
10
instead of
10
50
Aug 6 '14 #1
2 1595
donbock
2,426 Expert 2GB
The program-build process is equivalent to the following:
  1. First the preprocessor translates your source and header files into an intermediate file.
  2. Then the compiler translates that intermediate file into an object file.
  3. Then the linker combines one or more object files and object libraries into an executable file.
  4. Sometime later, you run the executable file.
A particular compiler implementation might not generate all of these intermediate files, but it will act as if it did. Some compilers can be induced to preserve the intermediate file produced by the preprocessor so you can look at it.

The "scope" of a preprocessor macro (it is not a variable) is all of the lines between its #define and its #undef. The things that affect variable scope (such as function definitions and braces) have no effect on preprocessor macros.

You expected execution of function foo to change the value of macro a. In actuality, all of the preprocessor directives and macros have been fully expanded before the code is compiled. The compiler and executable have no awareness that there is such a thing as a preprocessor.
Aug 6 '14 #2
weaknessforcats
9,208 Expert Mod 8TB
To be clear about this:

Expand|Select|Wrap|Line Numbers
  1. #define a 10
tells the preprocessor to change all occurrences of a to 10. Later if you define a as something else, thoss 10's are still there.
Aug 7 '14 #3

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

Similar topics

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...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
7
by: Erik Leunissen | last post by:
L.S. How can I make certain code parts be compiled conditionally, depending on the definition of a macro such as: #define VERSION "2.3" Is it all right to do things like:
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...
6
by: Felix | last post by:
Hello, Using someting like #define WINVER 0x0500 in a header file works perfect, i.e. the preprocessor sees the definition. But I would like to set WINVER in the IDE (Properties -> C/C++ ->...
13
by: Henry Townsend | last post by:
I hope this is on-topic in c.l.c - it's about the C preprocessor more than the language per se, more generally about the K&R behavior, and most specifically about the Sun cpp which is why I've...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
2
by: =?Utf-8?B?bWFyaw==?= | last post by:
I am contemplating converting a huge collection of functions from C++ to VB.NET. I am concerned with a style used in the original code where a variable is set to a constant value with the # define...
2
by: Laurent Deniau | last post by:
I would like to know why the following small program does not compile (checked with gcc 4.1.2) and if the compiler behavior is correct: struct A; typedef void (T)(struct A*); void f(void) {...
9
by: Martin Wells | last post by:
Is there anything like __inline_is_supported to indicate whether a certain C compiler supports inline? I'm writing my code as fully portable C89, but I want to use inline. Martin
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.