Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I know whether I am compiling C or C++?

Programming

How do I know whether I am compiling C or C++?

by  Salem  Posted    (Edited  )
Most compilers can compile C programs and C++ programs, but for one reason or another, you need to be sure which language you're using.

Some reasons for doing this are:
1. Some features are interpreted differently between C and C++. For example sizeof('a')
In C, this is the same as sizeof(int), in C++, its the same as sizeof(char).
A more complete list can be found here
http://david.tribble.com/text/cdiffs.htm

2. You're a student studying C and you want to make sure that you don't learn any C++ by mistake.

Compilers normally use the file suffix to determine the language in use, so
For a C program, you save the file as prog.c
For a C++ program, you save the file as prog.cpp (or sometimes prog.cxx)

To make sure, compile this as a console program, and execute it to find out what it prints.
Code:
#include <stdio.h>

int main ( void ) {
#if defined(__cplusplus)
    printf( "Compiled with an ANSI-C++ compiler\n" );
#elif defined(__STDC__)
    printf( "Compiled with an ANSI-C compiler\n" );
#else
    printf( "I don't know what standard your compiler is\n" );
#endif
    printf( "Press return\n" );
    getchar();
    return 0;
}
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top