3 ms·
If you want to tell if your compiler is C or C++, run this program: #include <stdio.h> int main() { printf("%d\n", sizeof('a')); } It's the seco
by jcranmer 7y ago
If you want to tell if your compiler is C or C++, run this program:
#include <stdio.h>
int main() {
printf("%d\n", sizeof('a'));
}
It's the second thing C++ mentions in its list of incompatibility with C (the first is "new keywords").
A more obscure difference is this program:
int i;
int i;
int main() {
return i;
}
It's legal C but not legal C++.
- ksherlock 7y agoint main() { return 4//**/2 ; }
- eMSF 7y agoLine comments have been part of the C language for a long, long time (added in C99); so much so that, especially when discussing the subject on the internet, more and more often they predate some of the younger participants.
- _kst_ 7y agosizeof ('a') doesn't reliably tell you whether you're compiling C or C++. It yields the same result in an implementation where sizeof (int) == 1 (which requires CHAR_BIT >= 16). (The difference is that character constants are of type int in C, and of type char in C++.) So if sizeof ('a') == 1, then either you're compiling as C++ or you're compiling as C under a rather odd implementation. Both POSIX and Windows require CHAR_BIT==8. The only systems I know of with CHAR_BIT>8 are for digital signal processors (DSPs). If you want to tell whether you're compiling C or C++: #ifdef __cplusplus ... #else ... #fi
- unwind 7y agoYou need to use %zu to print values of type size_t, otherwise you get undefined behavior. So in C, don't run that program. :)