#ifdef _cplusplus

This is a discussion on #ifdef _cplusplus within the C++ Programming forums, part of the General Programming Boards category; Hi, I have the following code - FIRST.CPP, CALL.C and stdafx.h FIRST.CPP: Code: #include "stdafx.h" extern "C" void callmethod(); int ...

  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
    21,884
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,884
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    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,425
    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%.

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    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,893
    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, 06: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, 10:24 PM
  5. Difference between ifdef and if defined?
    By z33z in forum C++ Programming
    Replies: 6
    Last Post: 10-28-2001, 10:36 PM

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