Taken from http://www.ocf.berkeley.edu/~wwu/riddles/cs.shtml
It looks very compiler specific to me. Don't know where to look for?
Taken from http://www.ocf.berkeley.edu/~wwu/riddles/cs.shtml
It looks very compiler specific to me. Don't know where to look for?
Simple enough.
#include <stdio.h>
int main(int argc, char ** argv) {
#ifdef __cplusplus
printf("C++\n");
#else
printf("C\n");
#endif
return 0;
}
Or is there a requirement to do this without the official standard?
__cplusplus
is defined to the value 199711L
when compiling a C++ translation unit". (Section 16.8: [cpp.predefined]
) –
Pullover __cplusplus
is defined to be 199711L only when the compiler is completely conforming. gcc, for example, still defines __cplusplus
to be 1. –
Zymo typedef
s(Note that the struct
needs to be declared in an inner scope so that it takes precedence over the outer name in C++.)
#include <stdio.h>
int main(void)
{
char x;
{
struct x { char dummy[2]; };
printf("%s\n", sizeof (x) == 1 ? "C" : "C++");
}
}
A similar version that doesn't rely on the ambiguity between sizeof (type)
and sizeof (variable)
, using only types:
#include <stdio.h>
int main(void)
{
typedef char t;
{
struct t { char dummy[2]; };
printf("%s\n", sizeof (t) == 1 ? "C" : "C++");
}
}
struct
/class
equivalence, automatic typedef
s, and automatically-generated default constructors#include <stdio.h>
int isC = 0;
void Foo() { isC = 1; }
int main(void)
{
struct Foo { int dummy; };
Foo();
printf("%s\n", isC ? "C" : "C++");
}
struct
declarations in CAlso see Symbol clashing of inner and outer structs, C++ vs C
#include <stdio.h>
int main(void)
{
typedef struct inner { int dummy; } t;
{
struct outer { struct inner { t dummy[2]; } dummy; };
printf("%s\n",
sizeof (struct inner) == sizeof (t)
? "C++"
: "C");
}
}
//
commentsThis won't work with C99 or with C89 compilers that support //
as an extension.
#include <stdio.h>
int main(void)
{
printf("%s\n",
0 //* */
+1
? "C++"
: "C");
}
or alternatively:
printf("%s\n",
1 //* */ 2
? "C++"
: "C");
sizeof
differences with char
literalsNote that this isn't guaranteed to be portable since it's possible that some hypothetical platform could use bytes with more than 8 bits, in which case sizeof(char)
could be the same as sizeof(int)
. (Also see Can sizeof(int) ever be 1 on a hosted implementation?)
#include <stdio.h>
int main(void)
{
printf("%s\n", sizeof 'a' == 1 ? "C++" : "C");
}
This is based off of the 5.16, 5.17, 5.18 example in the ISO C++03 standard, and it works in gcc but not in MSVC (possibly due to a compiler bug?).
#include <stdio.h>
int main(void)
{
void* array[2];
printf("%s\n",
(sizeof (((void) 0), array) / sizeof (void*) == 1)
? "C"
: "C++");
}
This one isn't strictly legal, but some compilers are lax.
#include <stdio.h>
int main(void)
{
int isCPP = 1;
printf("%s\n", (1 ? isCPP : isCPP = 0) ? "C++" : "C");
}
(You also could check for the __cplusplus
preprocessor macro (or various other macros), but I think that doesn't follow the spirit of the question.)
I have implementations for all of these at: http://www.taenarum.com/csua/fun-with-c/c-or-cpp.c
char
by definition is 1 byte, and a byte must be at least 8 bits, but it can be more. That's why CHAR_BIT
exists. –
Godin struct x { ... }
is the typical way to declare a struct
in C with type struct x
. If you wanted a typedef
so you don't need to type struct
all the time when using it, you'd do typedef struct x { ... } simpler_name
. Note that the struct
tag (x
) and the typedef
(simpler_name
) live in separate C namespaces, so you also can do typedef struct x { ... } x
. C++ creates such typedef
s implicitly for struct
and class
, hence x
in approach #1 resolves differently in C++ than in C. –
Godin We had to do a similar assignment at school. We were not allowed to use preprocessor (except for #include
of course). The following code uses the fact that in C, type names and structure names form separate namespaces whereas in C++ they don't.
#include <stdio.h>
typedef int X;
int main()
{
struct X { int ch[2]; };
if (sizeof(X) != sizeof(struct X))
printf("C\n");
else
printf("C++\n");
}
typedef struct X {} Y; void X();
compiles, but if you add void Y();
you will get a compiler error: the symbol Y
, in the non-user-defined namespaces is already used by the typedef
. –
Grimsley Simple enough.
#include <stdio.h>
int main(int argc, char ** argv) {
#ifdef __cplusplus
printf("C++\n");
#else
printf("C\n");
#endif
return 0;
}
Or is there a requirement to do this without the official standard?
__cplusplus
is defined to the value 199711L
when compiling a C++ translation unit". (Section 16.8: [cpp.predefined]
) –
Pullover __cplusplus
is defined to be 199711L only when the compiler is completely conforming. gcc, for example, still defines __cplusplus
to be 1. –
Zymo puts(sizeof('a') == sizeof(int) ? "C" : "C++");
sizeof(int)
can be 1 if CHAR_BIT
is at least 16. –
Amitie int
, in C++ they're of type char
. –
Amitie Here's the program:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("This is %s\n", sizeof 'a' == sizeof(char) ? "C++" : "C");
return 0;
}
And here is some nice reading on C and C++ differences.
Just look to see if the __STDC__
and __cplusplus
compiler macros are defined.
I'm guessing the intent is to write something that depends on differences between the languages themselves, not just predefined macros. Though it's technically not absolutely guaranteed to work, something like this is probably closer to what's desired:
int main() {
char *names[] = { "C", "C++"};
printf("%s\n", names[sizeof(char)==sizeof('C')]);
return 0;
}
sizeof
with the type's allowed range of values. –
Godin EOF
is required to be negative, so it's true that it must be different from any value of unsigned char
. However I don't see how it implies sizeof(int)>sizeof(unsigned char)
. int
is not required to hold all values of unsigned char
, only those values that can be read through getchar
(which need not span the entire range of unsigned char
). On PDP-10, where CHAR_BIT == 36
, sizeof(char)
indeed equals sizeof(int)
. –
Amitie sizeof(int) == 1
and have all int
values be valid character returns from getchar()
, in which case EOF
can also be a valid character, and testing whether EOF
indicates end-of-file needs to be performed with feof()
(and testing for error with ferror()
). And Crays are an example of sizeof(int) == 1
: they used to have all the integer types be 64 bits wide. –
Nudity For what it's worth, here's another answer:
char x[sizeof(char *)+2], y[1];
printf("%.*s\n", sizeof(1?x:y)-sizeof(char *)+1, "C++");
You could try preprocessor directives, but that might not be what they are looking for.
© 2022 - 2024 — McMap. All rights reserved.