Hey,
Can anyone tell me what will happen if I use a #define statement inside a function. Since it is a pre-processor directive, will scope and persistance come into picture for such constants?
Thanks in advance
Shanthala
Hey,
Can anyone tell me what will happen if I use a #define statement inside a function. Since it is a pre-processor directive, will scope and persistance come into picture for such constants?
Thanks in advance
Shanthala
>Since it is a pre-processor directive, will scope and persistance come into picture for such constants?
No. The preprocessor doesn't care about scope; it's basically just a find and replace before the code goes into the compiler. So yes, you can #define anything anywhere in your code, and it will compile as long as whatever comes out of the preprocessor has no syntax errors.
Thank you,
So I take it that the constant is defined throughout the program from the point where it is defined. Am i right?
sorrofix - is that correct? I was under the assumption that it only began the replacement after it parsed the #define. Ex
Would not be valid. Though I guess it actually depends on the compiler.Code:... int j = BARF; #define BARF 12 ...
Environment: OS X, GCC / G++
Codes: Java, C#, C/C++
AOL IM: neandrake, Email: neandrake (at) gmail (dot) com
I never said that the location of preprocessor commands within the code has no effect on the preprocessor output, all I said was that it doesn't care about scope or namespaces or whatnot. So no, you can't just #define a constant midway through your program and expect that all uses of the constant will be replaced, only the uses of the constant that are after the #define.
sorrofix - The way your post reads could be a little misleading
So yes, you can #define anything anywhere in your code, and it will compile as long as whatever comes out of the preprocessor has no syntax errors.
Environment: OS X, GCC / G++
Codes: Java, C#, C/C++
AOL IM: neandrake, Email: neandrake (at) gmail (dot) com
Yes, I suppose I should have noted that while you can #define anything anywhere in your code, the definitions will only take effect in code placed after the definition.
Wasn't trying to be picky - that's just how I read it the first time through.
Environment: OS X, GCC / G++
Codes: Java, C#, C/C++
AOL IM: neandrake, Email: neandrake (at) gmail (dot) com
You guess wrong (except in one peripheral case).
If BARF has not been previously #define'd, the definition of j is invalid. That behaviour does not depend on the compiler (assuming the compiler complies with the standard).
If BARF has been previously #define'd, then the #define directive is invalid. Unless you're using C99, where the redefinition is valid if BARF has been previously #defined as 12.