C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2010, 10:22 AM   #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.
Chandana is offline   Reply With Quote
Old 02-09-2010, 10:44 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 12,459
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 02-09-2010, 10:48 AM   #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.
Chandana is offline   Reply With Quote
Old 02-09-2010, 11:02 AM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 12,459
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 02-09-2010, 11:26 AM   #5
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,078
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!
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 02-09-2010, 06:52 PM   #6
Staff software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 6,014
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.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 02-10-2010, 02:00 AM   #7
Registered User
 
Join Date: Jun 2005
Posts: 1,649
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%.
grumpy is offline   Reply With Quote
Old 02-10-2010, 02:09 AM   #8
Registered User
 
Join Date: Jan 2010
Posts: 238
Quote:
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)
_Mike is offline   Reply With Quote
Old 02-10-2010, 02:53 AM   #9
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,078
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 02-10-2010, 08:30 AM   #10
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,579
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
CornedBee is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:09 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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