Thread: predefined macros

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    predefined macros

    i have reached the chapter in the book im working though that discusses pre processing and i tried to play around with a few of the predefined macros such as _FUNC_ or _STDC_VERSION_. The first one said it was undeclared but asked if i men _unix when i tried that it said it was undeclared and asked me if i meant _unix again. the second one i called with a printf statment but the compiler just said it was undeclared.

    do i need a header file for these or am i doing something wrong.
    many thanks
    coop

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Double underscores
    Code:
     __FILE__

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cooper1200
    i have reached the chapter in the book im working though that discusses pre processing and i tried to play around with a few of the predefined macros such as _FUNC_ or _STDC_VERSION_. The first one said it was undeclared but asked if i men _unix when i tried that it said it was undeclared and asked me if i meant _unix again. the second one i called with a printf statment but the compiler just said it was undeclared.
    Neither of these predefined identifiers require a header inclusion. The first one is actually __func__ and the second one is actually __STDC_VERSION__ (note the consecutive underscores).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    sorry guys im the double underscore wasnt clear in the book

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by cooper1200 View Post
    sorry guys im the double underscore wasnt clear in the book
    A quick tip if you are using GCC. The option -dM will list all #defines, including those predefined; -E option will stop GCC after preprocessing, so, you can do:

    Code:
    $ gcc -dM -E - < /dev/null | less
    To see all predefined macros GCC has available for you...

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by flp1969 View Post
    A quick tip if you are using GCC. The option -dM will list all #defines, including those predefined; -E option will stop GCC after preprocessing, so, you can do:

    Code:
    $ gcc -dM -E - < /dev/null | less
    To see all predefined macros GCC has available for you...
    i just tried that got an error message saying no file input and a load of weird characters i guess i dont have gcc installed directly as i use codeblocks ide although i have the man pages for gcc and codeblocks uses it as its compiler
    Last edited by cooper1200; 04-23-2019 at 07:14 AM.

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by flp1969 View Post
    A quick tip if you are using GCC. The option -dM will list all #defines, including those predefined; -E option will stop GCC after preprocessing, so, you can do:

    Code:
    $ gcc -dM -E - < /dev/null | less
    To see all predefined macros GCC has available for you...
    That command does not show, __FILE__, __LINE__, and __func__.

    And why __func__ is lowercase confuses me! ;^)

    __FILE__ and __LINE__ are built into the preprocessor, and __func__ seems to be built into the compiler itself.
    Code:
    int main(void)
    {
       char *file = __FILE__;
       int   line = __LINE__;
       const char *func = __func__;
    
       return 0;
    }
    
    /* compile with:
     gcc -E program-name.c
    To see the output of the preprocessor
    */

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by cooper1200 View Post
    i just tried that got an error message saying no file input and a load of weird characters i guess i dont have gcc installed directly as i use codeblocks ide although i have the man pages for gcc and codeblocks uses it as its compiler
    If you are using Windows and got GCC installed:
    Code:
    gcc -dM -E - < NUL | more

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by flp1969 View Post
    If you are using Windows and got GCC installed:
    Code:
    gcc -dM -E - < NUL | more
    no i have mint although it is on a virtual machine

  10. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by rstanley View Post
    That command does not show, __FILE__, __LINE__, and __func__.
    These are created dynamically when a code is compiled... We are not compiling, but just preprocessing (-E option).

    Quote Originally Posted by rstanley
    And why __func__ is lowercase confuses me! ;^)
    Is defined this way by ISO 9989:1999. GCC defines also __FUNCTION__ if you are confused by the lowercase version.

    Quote Originally Posted by rstanley
    /* compile with:
    gcc -E program-name.c
    To see the output of the preprocessor
    */
    This will list everything done by the preprocessor, including #include lines, #if,#else,#endifs, #line, #error etc.... -dM option limits the listing to #define only.
    If you want to preprocess only, you could do as below ("cpp" is C Pre-Processor, that's why, on Unixes, a C++ file extension is '.cc', '.c++', '.C' (uppercase) or '.cxx'):
    Code:
    cpp program-name.c

  11. #11
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    @flp1969 please compile the sample program I provided in #7 above, with the command I provided, and you WILL see that __FILE__ and __LINE__ are resolved by the preprocessor, not the compiler, as I stated.
    Code:
    gcc -E program-name.c
    WILL only run the preprocessor!

  12. #12
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by rstanley View Post
    @flp1969 please compile the sample program I provided in #7 above, with the command I provided, and you WILL see that __FILE__ and __LINE__ are resolved by the preprocessor, not the compiler, as I stated.
    Yep... because __func__ isn't a predefined macro (as stated in ISO 9989:1999 6.4.2.2:1 and 6.10.8:1), it is an identifier defined by the compiler when used.

    Quote Originally Posted by rstanley
    Code:
    gcc -E program-name.c
    WILL only run the preprocessor!
    Yep, and I didn't say this wouldn't run only the preprocessor... I said, this will list all preprocessed info, while using -dM option will limit the listing to symbolic definitions only.
    Last edited by flp1969; 04-23-2019 at 08:20 AM.

  13. #13
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    I stand corrected: The -dM option will not "limit" the listing... it will list ALL defined symbols, and only defined symbols, except those dynamically created by the compiler (__FILE__ and __LINE__ -- this is strange, since gcc documentation includes "predefined" macros).

    Code:
    $ cpp -dM - < /dev/null | grep FILE
    $ cat > test.c <<< 'int main(void){}'
    $ cpp -dM test.c  | grep FILE
    $
    PS: It doen't make sense listing __LINE__, since will change for every line... But __FILE__ should be listed for a given filename (test.c above, for example)...
    Last edited by flp1969; 04-23-2019 at 08:55 AM.

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    It doen't make sense listing __LINE__, since will change for every line... But __FILE__ should be listed for a given filename (test.c above, for example)...
    It's great for debugging - This is a quick example of how it can be used... [edit] I know that it isn't actually "thowing" an exception, it was just taken from some code I already had and simplified... [/edit]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define THROW_EXCEPTION(exType) my_throwing_exception( __FILE__ , \
                                                           __LINE__ , \
                                                           exType)
    
    #define MALLOC_FAILED_EX 0
    
    
    const char *getExceptionMessage(int exType)
    {
        switch (exType)
        {
        case MALLOC_FAILED_EX:
            return "malloc call failed";
    
            /* ... */
        }
        return "Unknown error";
    }
    
    void my_throwing_exception( const char *fileName, int lineNumber, int exType)
    {
        fprintf(stderr, "\nExeption found\nFile: %s\nLine: %d,\nMessage: %s", fileName, lineNumber, getExceptionMessage(exType));
    }
    
    int main(void)
    {
        char *apple = NULL;
        int *banana = NULL;
        long int *grape = NULL;
    
        int result = EXIT_SUCCESS;
    
        if (NULL == (apple = malloc(sizeof(*apple))))
        {
            THROW_EXCEPTION(MALLOC_FAILED_EX);
            result =  EXIT_FAILURE;
        }
    
        free(apple);
    
        if (NULL == (banana = malloc(sizeof(*banana))))
        {
            THROW_EXCEPTION(MALLOC_FAILED_EX);
            result =  EXIT_FAILURE;
        }
    
        free(banana);
    
        if (NULL == (grape = malloc(sizeof(*grape))))
        {
            THROW_EXCEPTION(MALLOC_FAILED_EX);
            result =  EXIT_FAILURE;
        }
    
        free(grape);
    
        return result;
    }
    I first saw it used this way in a code tracer from R. Heathfield et. al., "C Unleashed", "Chapter 7: When Things Go Wrong: Code-Mending" (A great advanced book that was originally recommended by Salem)
    Last edited by Click_here; 04-23-2019 at 06:41 PM.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question regarding predefined macros
    By ang_ks in forum C++ Programming
    Replies: 6
    Last Post: 07-13-2007, 11:51 PM
  2. Need Help w/ I/O and Predefined Character Functions
    By impact in forum C++ Programming
    Replies: 1
    Last Post: 05-22-2007, 02:19 AM
  3. predefined symbolic constnts
    By getout in forum C Programming
    Replies: 2
    Last Post: 01-04-2007, 11:59 AM
  4. predefined button color
    By FOOTOO in forum Windows Programming
    Replies: 3
    Last Post: 01-30-2005, 04:07 PM
  5. Predefined FileOpen, SaveAs, etc. in C?
    By xjcass in forum Windows Programming
    Replies: 2
    Last Post: 11-08-2003, 09:38 AM

Tags for this Thread