#ifndef

This is a discussion on #ifndef within the C++ Programming forums, part of the General Programming Boards category; I was looking through system header files a while back, and I noticed that each one has some statement along ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    86

    #ifndef

    I was looking through system header files a while back, and I noticed that each one has some statement along the lines of:

    Code:
    #ifndef _CPP_
    or something similar, I dont remember exactly what it looked like.

    what do those statements mean, and how does one use them?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    oh har har

    I know what hte commands ARE, but I don't know the syntax for #ifndef, ie: the statements seem to be able to determine what language is being used, or what OS is being used. How does one do that kind of thing

  4. #4
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    but I don't know the syntax for #ifndef
    The first link in the google page Codeplug posted has information on that.

    the statements seem to be able to determine what language is being used, or what OS is being used. How does one do that kind of thing
    By using it with common predefined macros.

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    consider this simple example:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    #ifdef __cplusplus
    #define CPP 1
    #else
    #define CPP 0
    #endif
    
    int main()
    {
       if (CPP)
       {
               cout<<"It's C++ compiler";
       }
       else
       {
        
           cout<<"It's C compiler";
       }
       system("pause");
    }
    Last edited by Micko; 03-28-2005 at 02:57 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    would this work?

    Code:
    #ifdef _cplusplus
    #include <iostream>
    void printHello(){
    
    cout<<"hello\n";
    
    }//end printHello
    #elseif
    #include <stdio.h>
    void printHello(){
    
    printf("Hello\n");
    
    }//end printHello
    #endif

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Why not try it yourself and see?
    But never mind, here analyze this:
    Code:
    #ifdef __cplusplus
    #include <iostream>
    #include <cstdlib>
    void __foo()
    {
         std::cout<<"CPPKivla";
    }
    #define foo __foo
    #else
    #include <stdio.h>
    #include <stdlib.h>
    void __foo()
    {
         printf("CKivla");
    }
    #define foo __foo
    #endif
    int main()
    {
        foo();
       system("pause");
    }
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    almost. you have the concept right, but the preprocessor directives slightly wrong (or Dev-C++ doesn't support all of the preprocessor directives)

    anyways, this works in Dev-C++ 4.9.9.2:

    Code:
    #if (defined __cplusplus)
    #include <iostream>
    using namespace std;
    int main()
    {
    	cout << "c++" << endl;
    	cin.get();
    	return 0;
    }
    #else
    #include <stdio.h>
    int main(void)
    {
    	printf("c\n");
    	getchar();
    	return 0;
    }
    #endif
    by "works," i mean that it compiles as both C and C++, and when the C program is run it outputs c and when the C++ program is run it outputs c++. not sure why i couldn't get #ifdef to work.

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    what does foo stand for?

    kind of random, but whenever I create a new carbon project, my about menu is always called about foo unitl I change it, and then Micko used it, what does it mean?

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    foo is just the generic example variable name etc. like if someone is explaining pointers sometimes they will make an example like:
    Code:
    int foo = 5; //this is an int variable with the value 5
    int * pfoo = &foo; //pointer to foo
    *pfoo = 6; //now foo has the value of 6
    i don't know what it actually stands for, though.

    actually, google is amazing. explore this page and the links from it.
    http://www.dictionary.net/foo
    apparently it originated from the slang abbreviation FUBAR, the meaning of which you can find out for yourself (not appropriate for the forums)

    now i know. and so do you.

  11. #11
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    As a note, the FAQ, like google, is also amazing.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  12. #12
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    A useful technique is to stop the same code being compiled twice:

    Code:
    #ifndef MYFILE_H
    #define MYFILE_H
    
    int myFunc (int arg1);
    int hello (void);
    
    #endif
    If MYFILE_H has not been defined, the code in your header will be compiled and its definition is flagged. So if for some reason, the file is included more than once, all subsequent inclusions will note that MYFILE_H has already been defined and skip over the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #ifndef explanation?
    By Zooker in forum C Programming
    Replies: 2
    Last Post: 02-12-2009, 05:59 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Warning on an ifndef directive
    By DavidP in forum C++ Programming
    Replies: 2
    Last Post: 08-02-2007, 01:31 AM
  4. #ifndef
    By paperbox005 in forum C Programming
    Replies: 4
    Last Post: 09-24-2004, 07:25 PM
  5. ifndef
    By mayfda in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2003, 02:29 PM

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