Thread: #ifdef _cplusplus

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    10

    #ifdef _cplusplus

    Hi,

    I have the following code - FIRST.CPP, CALL.C and stdafx.h


    FIRST.CPP:
    Code:
       #include "stdafx.h"
      extern "C" void callmethod();
    
      int _tmain(int argc, _TCHAR* argv[])
     {
    	callmethod();
    	return 0;
     }
     void justcall(){
        #ifdef  __cplusplus
    	    printf("This is C++");
        #else
            printf("This is C code");
        #endif;
     }
    CALL.C
    Code:
    void callmethod(){
    	justcall();
    }
    stdafx.h:
    Code:
    #include <stdio.h>
    #include <tchar.h>
    
    extern "C" void justcall();


    The output when I run FIRST.CPP is "This is C++".
    From FIRST.CPP, I call callmethod() which is defined in a .c file and from this callmethod() I call justcall() which is in a .cpp file.


    My question is justcall() is called from a C program then why is the code inside #ifdef _cplusplus getting executed?

    Thanks,
    Chandana.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Chandana
    My question is justcall() is called from a C program then why is the code inside #ifdef _cplusplus getting executed?
    Because of the context of where that function was compiled, it was compiled as C++, even though it is called from a C function.
    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

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    10
    Thanks for the reply.

    what should I do if I want to get "This is C code" as the output.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What are you actually trying to do?

    After all, taken out of the context in which you asked it, your question is dumb, since the answer is: print "This is C code" unconditionally
    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

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The #ifdef is to determine the capabilities of the compiler compiling the code, so to speak. Since in executable form, it doesn't matter if it's compiled using C++ or C.
    The ifdef should be used as a means of knowing whether a C function or a C++ function calls the said function!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The use of _cplusplus in a .cpp file is redundant. This is not how it is normally used. It's normally used from within header files, which are typically named .h whether they are C or C++, and even then, its normal purpose is to turn off name mangling in order to allow C code to make calls to C++ code.

    The reason it prints "This is C++" is because the code is C++. The fact that it's being called from a C function is irrelevant.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Chandana View Post
    what should I do if I want to get "This is C code" as the output.
    Compile your code with a C compiler that is not a C++ compiler.

    Some compilers have dual hats: depending on command line options or other settings, they compile code as C or as C++. With those compilers, you need to pick the right mode (eg via command line settings). As brewbuck noted, some compilers compile as C if the file has a .c extension, and as C++ if the file has a .cpp (or .cc) extension.

    #ifdef _cplusplus is not checked at run time. It is checked as one of the phases during compilation (specifically, by the preprocessor).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    what should I do if I want to get "This is C code" as the output.
    Since you have stdafx.h I'm assuming you are using visual c++ (If any other compiler also uses this header file then ignore this)
    Right-click the file(s) you want to compile as C code and select properties.
    Then go to "C/C++" -> "Advanced" -> "Compile As" and select "Compile as C Code"
    (or just rename the file to something.c)

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Bad idea.
    Compiling .cpp files as C for some reason will serve to confuse and break compatibility with other compilers. Do the right thing: name it with a .c extension instead. It will compile fine in VS and other compilers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It won't, since it contains C++ constructs; specifically, the 'extern "C"'.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #ifdef error
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2010, 07:03 PM
  2. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  3. #ifdef - Can It Be Used With Logic Such as OR / AND?
    By dedham_ma_man in forum C Programming
    Replies: 3
    Last Post: 04-21-2006, 02:57 PM
  4. Use of #ifdef for portability - a specific case
    By hzmonte in forum C Programming
    Replies: 7
    Last Post: 11-03-2005, 11:24 PM
  5. Difference between ifdef and if defined?
    By z33z in forum C++ Programming
    Replies: 6
    Last Post: 10-28-2001, 11:36 PM