473,756 Members | 5,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Preprocessor directive

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 following example.

eg cosider 2 files sample.c and sample.h

sample.h
#define BLAH 10

sample.c
/* This should print 10 */
printf("\n %d ", BLAH);

func1();

printf("\n %d ", BLAH);

func1()
{
#undef BLAH
#define BLAH 15
/* This should print 15, but BLAH has a local scope */
printf("\n %d ", BLAH);

}

Thanks,
Nov 13 '05 #1
16 4502
What are you trying to accomplish ? The pre-processor simply expands your
macro when you compile your code. So, if you were to compile a separate .c
file that included 'sample.h', it would only see the definition of BLAH in
sample.h (which is 10). BLAH does not have local scope. As far as the
compiler is concerned, the argument to the last printf() statement is the
constant 15. Explain what you are trying to do and I can provide some
assistance.

"Trying_Har der" <fr***********@ yahoo.com> wrote in message
news:b0******** *************** ***@posting.goo gle.com...
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 following example.

eg cosider 2 files sample.c and sample.h

sample.h
#define BLAH 10

sample.c
/* This should print 10 */
printf("\n %d ", BLAH);

func1();

printf("\n %d ", BLAH);

func1()
{
#undef BLAH
#define BLAH 15
/* This should print 15, but BLAH has a local scope */
printf("\n %d ", BLAH);

Nov 13 '05 #2
Trying_Harder wrote:
Is it possible to redefine a macro with global scope after
undefining it in a function? If yes, could someone explain
how?
Macros are not `scoped'. Preprocessing takes place *before* scope of
any kind is established.
/If/ my question above isn't very clear you can refer to
the following example.

eg cosider 2 files sample.c and sample.h

sample.h
#define BLAH 10

sample.c
/* This should print 10 */
printf("\n %d ", BLAH);

func1();

printf("\n %d ", BLAH);

func1()
{
#undef BLAH
#define BLAH 15
/* This should print 15, but BLAH has a local scope */
printf("\n %d ", BLAH);

}


`BLAH' will be replaced by `15' for the rest of the translation unit.

HTH,
--ag

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Nov 13 '05 #3
Trying_Harder wrote:

Is it possible to redefine a macro with global scope after
undefining it in a function? If yes, could someone explain
how?
The scope of a macro definition lasts from the #define
to the #undef, or to the end of the translation unit. Macro
scopes do not nest, so there's no way to "push" a replacement
definition and then "pop" the original.
/If/ my question above isn't very clear you can refer to
the following example.

eg cosider 2 files sample.c and sample.h

sample.h
#define BLAH 10

sample.c
/* This should print 10 */
printf("\n %d ", BLAH);

func1();

printf("\n %d ", BLAH);

func1()
{
#undef BLAH
#define BLAH 15
/* This should print 15, but BLAH has a local scope */
printf("\n %d ", BLAH);

}


The numbers output would be 10, 15, and 10, in that order.
BLAH does not have "a local scope" in func1(); BLAH remains
defined as 15 all the way to the end of the compilation.

--
Er*********@sun .com
Nov 13 '05 #4
Trying_Harder <fr***********@ yahoo.com> wrote:
Is it possible to redefine a macro with global scope after
undefining it in a function? If yes, could someone explain
how? /* This should print 15, but BLAH has a local scope */


preprocessor directives don't have scope. The preprocessor will go from
the beginning of the program to the end and make replacements as necessary.
The first two BLAH's are being replaced by 10 because when test.h is
#included, it is #defined to 10. The BLAH in func1 was replaced by 15
because it was defined after main. If you put its declaration above main
then it will print 15 for all of them. Keep in mind, the preprocessor
is *not* operated at run-time, it is done *before* compilation.

What are you trying to accomplish? Even if you could pull that off you
probably shouldn't.

--
Harrison Caudill | .^ www.hypersphere.org
Computer Science & Physics Double Major | | Me*Me=1
Georgia Institute of Technology | '/ I'm just a normal guy
Nov 13 '05 #5
>
preprocessor directives don't have scope. The preprocessor will go from
the beginning of the program to the end and make replacements as necessary.
The first two BLAH's are being replaced by 10 because when test.h is
#included, it is #defined to 10. The BLAH in func1 was replaced by 15
because it was defined after main. If you put its declaration above main
then it will print 15 for all of them. Keep in mind, the preprocessor
is *not* operated at run-time, it is done *before* compilation.

What are you trying to accomplish? Even if you could pull that off you
probably shouldn't.
Let me start off with a thanks to all.
Actually, I have 3 modes the program can execute in, for example sake
`a', `b' and `c'. Now in each of these modes value of a particular
preprocessor macro is different, but the program can execute only in
a one mode in one execution.
Decision of what its value is, is taken soon after the program begins,
but I am doing this part in a function because of my aim to keep the
main function "light".

So func1() is where I am assigning the value to these macros depending
on `argv'.
One solution is to return a value from func1() hinting something about
the mode and #defining in main.
But, can't this be done in func1()?

Btw, in one of the other posts

<quoting Eric Sosman>
{
#undef BLAH
#define BLAH 15
/* This should print 15, but BLAH has a local scope */
printf("\n %d ", BLAH);

}

The numbers output would be 10, 15, and 10, in that order.
BLAH does not have "a local scope" in func1(); BLAH remains
defined as 15 all the way to the end of the compilation.

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^

If don't find this answer not convincing. If this was true then why
is the 3rd `printf' (in main) printing out 10 ? According to this
explanation it should print out 15 everywhere (after this "redefiniti on")
regardless of the scope.

Thank you for your help, I would really appreciate if someone could
refer me to some online resources containing preprocessor directives
explained in detail.

Thanks,
Nov 13 '05 #6
Charles Harrison Caudill <ku*****@myrna. cc.gatech.edu> wrote in message news:<bl******* ***@solaria.cc. gatech.edu>...
Trying_Harder <fr***********@ yahoo.com> wrote:
Is it possible to redefine a macro with global scope after
undefining it in a function? If yes, could someone explain
how?
/* This should print 15, but BLAH has a local scope */


preprocessor directives don't have scope. The preprocessor will go from
the beginning of the program to the end and make replacements as necessary.
The first two BLAH's are being replaced by 10 because when test.h is
#included, it is #defined to 10. The BLAH in func1 was replaced by 15
because it was defined after main. If you put its declaration above main
then it will print 15 for all of them. Keep in mind, the preprocessor
is *not* operated at run-time, it is done *before* compilation.

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
What happens if macro is decleared in main().Just a doubt, that is all.
#define blaa 10
void fun(void)
{
printf("%d\n",b laa);
#ifdef blaa
#undef blaa
#define blaa 90
#endif
printf("now blaa is = %d\n",blaa);
}
int main(void)
{
fun();
#ifdef blaa
#undef blaa
#define blaa 98
#endif
printf("now blaa has %d\n",blaa);
return 0;
}

What are you trying to accomplish? Even if you could pull that off you
probably shouldn't.

Nov 13 '05 #7
fr***********@y ahoo.com (Trying_Harder) wrote in message > >
If don't find this answer not convincing. If this was true then why

^^^

Sorry about the typo there. I meant to say "I dont find this answer
convincing".

p.s: Can someone please attend to my question above?

Thanks,
Nov 13 '05 #8
Trying_Harder wrote:
p.s: Can someone please attend to my question above?


Here's the original code:

[Trying_Harder]
| sample.h
| #define BLAH 10
|
| sample.c
| /* This should print 10 */
| printf("\n %d ", BLAH);
|
| func1();
|
| printf("\n %d ", BLAH);
|
| func1()
| {
| #undef BLAH
| #define BLAH 15
| /* This should print 15, but BLAH has a local scope */
| printf("\n %d ", BLAH);
|
| }

Here's the (correct) answer you don't find convincing:

[Eric Sosman]
|> The numbers output would be 10, 15, and 10, in that order.
|> BLAH does not have "a local scope" in func1(); BLAH remains
|> defined as 15 all the way to the end of the compilation.

Here's the question I think you're referring to:

[Trying_Harder]
| If don't find this answer not convincing. If this was true then why
| is the 3rd `printf' (in main) printing out 10 ? According to this
| explanation it should print out 15 everywhere (after this "redefiniti on")
| regardless of the scope.

Preprocessor replacement works on the text of the source file, so
occurrences of "BLAH" in the lines following the "#undef", "#define"
directives are replaced with "15" during an early stage of
compilation. Occurrences of "BLAH" before this (but following the
first "#define") are replaced with "10". After all this has taken place
the output from "sample.c" looks something like this:

printf("\n %d ", 10);

func1();

printf("\n %d ", 10);

func1()
{

printf("\n %d ", 15);

}

Note that this all takes place at "compile-time", i.e. before the
program is run at all. Further stages of compilation translate your
program into some sort of executable format, by which point there is
no trace of "#define" or "BLAH" left; the preprocessor directives have
already been executed and only "runnable" code remains.

"BLAH" is not like a variable, which is initialised at runtime.
"func1()" does not change the value of "BLAH". The fact that the
second "#define" is textually inside the body of "func1()" is
irrelevant; there is nothing left of "BLAH" or its various definitions
by the time "func()" is called.

Jeremy.
Nov 13 '05 #9
da***********@y ahoo.com wrote:
What happens if macro is decleared in main().Just a doubt, that is all.
Nothing special about that, the preprocessor does not care if it's in
main() or somewhere else:
#define blaa 10
void fun(void)
{
printf("%d\n",b laa);
This would be replaced by the preprocessor by

printf("%d\n",1 0);
#ifdef blaa
#undef blaa
#define blaa 90
#endif
Since 'blaa' was already defined it now would be redefined to 90
printf("now blaa is = %d\n",blaa);
so this would end up as

printf("now blaa is = %d\n",90);
}
int main(void)
{
fun();
fun will now print out

10
90

(BTW this would also happen if you would redefine 'blaa' before the
call of fun() since the preprocessor has already done it's work on
the body of fun().)
#ifdef blaa
#undef blaa
#define blaa 98
#endif
And this will redefine 'blaa' to 98
printf("now blaa has %d\n",blaa);
so here you get
printf("now blaa has %d\n",98); return 0;
}

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@p hysik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oe rring
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
9854
by: Boris Kuznetsov | last post by:
This occurs in an empty project when I add the following string: #using <mscorlib.dll> Can anyone tell me why would #using ... not be working???? MSDN says nothing about this error. Please advice what to do?? mailto: doberman@nest.ntu-kpi.kiev.ua
13
2134
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 defined = 2;
2
4922
by: Gustavo L. Fabro | last post by:
Greetings. Is there a way to run the preprocessor twice? Rephrasing that, is there a way to: #define SOMETHING #pragma OTHERTHING and have the preprocessor in somecode.cpp evaluate: SOMETHING
7
4897
by: ddehterov | last post by:
Hello, I'm wondering, is #warning preprocessor command is documented in standart C or it's implementation specific? I cannot find any documentation about it. It works with gcc on freebsd and linux. -- Dmitri
6
3134
by: olivier.grant | last post by:
Hi All, I'm trying to define a macro that will allow me to write the following code : #include MY_MACRO( NAME, SPACE ) and end up with the following preprocessed code : #include NAME.hpp
6
2280
by: 2112 | last post by:
I'm compiling a dll that imports msado15.dll. When I'm using Windows in English, the msado15.dll is located at <drive>:\Program Files\Common Files\System\ADO\msado15.dll". When using Windows in Portuguese, the msado15.dll is located at <drive>:\Arquivos de programas\Arquivos comuns\System\ADO\msado15.dll I'd know if exists some preprocessor directive that could identify the language of Windows and import msado15.dll from the correct...
31
2926
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 statements that the compiler does not process. The good people in the alt.comp.lang.learn.c-c++ newsgroup insist that the preprocessor is just one of many passes. The preprocessor processes a grammer unique to the preprocessor and only that grammer. ...
7
621
by: Daniel Rudy | last post by:
Hello Group, Is there a preprocessor directive that will cause the compiler to print a warning diagnostic of whatever message that I choose? I'm thinking of something along the lines of: #ifndef uint64 #warning 64-bit mode not supported. #endif
5
4001
by: _dwin | last post by:
Hi, Does anyone know how to implement your own preprocessor directive? For instance, I would like to have a directive which goes like: #<directive_name<parameters, ...> ie. #compress input_file output_file Thanks.
0
9455
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9271
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9869
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9708
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2665
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.