Thread: about preprocessor directives...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    about preprocessor directives...

    Hi,

    OFten i see programs starting with preprocessor directives such as #ifdef and ifndef and #define and etc and etc........

    BUt i don't understand one thing. #ifdef is followed by a constants or words, or watever u call it ( e.g. NDEBUG ). And many of these hav underscores or 2 underscores in between the word(s), in front, or after it. WHY ?
    Where are these 'words' defined? Where can i find out which 'words' are available? What do the underscores mean?

    pls explain, i think this topic is very important.

    thnx in advance

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    These are mangled names that make a header file unique. If the name is already defined then the header file has been opened already and it won't be opened again. It's both an inclusion guard as well as a unique identifier for a header file. The underscores you see are simply a common practice for name mangling, consider a header file myHeader.h. To mangle the name most people would use
    MYHEADER_H_

    If you see leading underscores then it's wrong, any identifier starting with an underscore is reserved by the implementation for further updates.

    -Prelude
    My best code is written with the delete key.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >Where are these 'words' defined?

    You define them by yourself.

    #define MYDEF 1001

    Now everywhere in my code where MYDEF appears it will be replaced by 1001 by the preprocessor.

    #ifdef MYDEBUG
    printf ("Debug Info: .....\n");
    #endif

    If you defined MYDEBUG at the beginning of your file, or in a header-file you included, then the compiler will compile the debug info statement. If MYDEBUG is not included, the debug code will not be compiled.

    This is called a compiler switch. You can define your own compiler switches to make it possible to add functionality or leave functionality away.

    #ifndef MYDEF

    This is the opponent of #ifdef.

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    In a macro in a program in my C Unleashed book, i see '__FILE__' and '__LINE__', ar they compiler specific?

    Where can i find compiler specific 'mangled names' ?

    Also, i didn't define NDEBUG anywhere, so i guess some mangled names are not programmer-defined.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're missing the point of preprocessor directives. Speficicly the '#ifdef' / '#ifndef' / #if directives.

    #ifdef UNIX
    #include "myunixheaders.h"
    #else
    #include "mydosheaders.h"
    #endif

    Now then, here, I need to either define UNIX or my program will compile as a DOS applicatioin. If I want a unix compile, before I compile, I edit the file, and before the #ifdef line, I add something like:

    #define UNIX

    See? Another example would be if I wanted to compile my application so the debug lines were displayed.

    #ifdef DEBUGON
    #define DEBUG(x) fprintf( stderr, x );
    #endif

    ... more code ...

    #ifdef DEBUGON
    DEBUG( "Allocating memory for %d nodes.\n", node_count );
    #endif

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59

    Talking ifndef

    consider this,



    #ifndef YOU
    #define YOU 10
    #endif

    this code of fragment, determines if YOU were define..
    if you were not define, then he would define YOU as 10

    #define is a macro substitution...
    meaning, before your program is compiled,
    it changes YOU into 10..
    that is the reason why it is called a preprocessor...


    and this stuffs are use in header files...

    try this out..

    #include<stdio.h>
    #include<stdio.h>

    int main(){
    balh blah balh
    return 0;
    }


    you can include stdio.h many times but,
    the compiler will treat it only once
    because it has been define..

    you can ask some of the guys from this board...

    they will surely teach you more that i said....



    thx

  7. #7
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    I get your point, but how do u explain the magled names '__FILE__' and '__LINE__' ? They compiler-specific or what?

  8. #8
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59

    they are

    yea,


    the ANSI standard specifies five built-in predefined macro names. they are

    __LINE__
    __FILE__
    __DATE__
    __TIME__
    __STDC__

    e.g.

    chack this out


    #include<stdio.h>

    #line 50
    int main(){
    printf("%d",__LINE__);
    return 0;
    }


    this will printf 52 on the screen...

    line is primarily used for debuggin porpoes and special application..



    did i explain well...

    tnx...
    " programming is 1% syntax and 99% logic "

  9. #9
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Thnx u explained very well....

    Is there any documentation on what u just said, like ANSI-standard predefined mangled names?

    I am using Dev C++, which uses the MingGw compiler i suppose.

  10. #10
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59

    try

    im am very sorry...



    i dont know about such documentation


    by the way, what is your purpose that you want to know about this advance stuff?

  11. #11
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Doesn't matter i found it thnx anyway

    Why do i learn? Coz i am reading C Unleashed now and i'm trying to learn advanced topics of C.. It's interesting u know...

  12. #12
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59

    Talking wish

    wish i could have that book....

    tnx again

    anytime man
    " programming is 1% syntax and 99% logic "

  13. #13
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    You can order that book. I couldn't find it in a bookshop, so i ordered it. Came in after a week, and the discounted price for me is $25 AUD, while the original price is like a hundred.

  14. #14
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59

    tnx

    do you have msn installed on your pc?
    i want to know...

    tnhx
    " programming is 1% syntax and 99% logic "

  15. #15
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    i use msn. email: [email protected]

    I would be happy to add u/.

Popular pages Recent additions subscribe to a feed