#define inside a function

This is a discussion on #define inside a function within the C Programming forums, part of the General Programming Boards category; Hey, Can anyone tell me what will happen if I use a #define statement inside a function. Since it is ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    #define inside a function

    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

  2. #2
    Code Abuser
    Join Date
    Jan 2009
    Posts
    16
    >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.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    Thank you,

    So I take it that the constant is defined throughout the program from the point where it is defined. Am i right?

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    sorrofix - is that correct? I was under the assumption that it only began the replacement after it parsed the #define. Ex

    Code:
    ...
    int j = BARF;
    #define BARF 12
    ...
    Would not be valid. Though I guess it actually depends on the compiler.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Code Abuser
    Join Date
    Jan 2009
    Posts
    16
    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.

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    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

  7. #7
    Code Abuser
    Join Date
    Jan 2009
    Posts
    16
    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.

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    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

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    5,851
    Quote Originally Posted by neandrake View Post
    Code:
    ...
    int j = BARF;
    #define BARF 12
    ...
    Would not be valid. Though I guess it actually depends on the compiler.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. whats wrong here
    By sreetvert83 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2005, 10:05 AM
  5. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 11:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21