Skip to content

Instantly share code, notes, and snippets.

@merijn
Created February 26, 2021 10:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merijn/3ea788047b61ccff221af0837be1ec36 to your computer and use it in GitHub Desktop.
Save merijn/3ea788047b61ccff221af0837be1ec36 to your computer and use it in GitHub Desktop.
C vs C++
#include <stdio.h>
char X = 'a';
int main(int argc, char* argv[])
{
(void) argc;
(void) argv;
struct X {
char a[2];
};
if (sizeof(X) == 1) {
printf("I am a C program!\n");
} else {
printf("I am a C++ program!\n");
}
return 0;
}
@merijn
Copy link
Author

merijn commented Feb 26, 2021

A file that is both correct C11 and correct C++11, but does something different in each language. Compiles warning free with both gcc -std=c11 -Wall -Wextra -pedantic and g++ -std=c++11 -Wall -Wextra -pedantic (well, modulo extension rename, if you use .c to compile with g++ you'll get a warning about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment