Jump to content

Tell the difference between C and C++

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
I just learned that string literals are integers in C and characters in C++.
Using this knowledge, I made a program that outputs a different message based on the compiler you are using. There is probably a more standard way of doing this, but here is my way:
#include <stdio.h>

int main()
{
	if (sizeof('k')==sizeof(int)) { //Everyone knows k is the best letter
		printf("You used a C compiler.\n");
	} else if (sizeof('k')==sizeof(char)) {
		printf("You used a C++ Compiler.\n");
	} else {
		printf("What compiler are you using?\n");
	}
	return 0;
}
It is even smart enough to tell you when it doesn't know what you are using.
Here is output from my terminal:
alex@Cheese:~/Programs$ gcc test.c -o test
alex@Cheese:~/Programs$ ./test
You used a C compiler.
alex@Cheese:~/Programs$ g++ test.c -o test
alex@Cheese:~/Programs$ ./test
You used a C++ Compiler.

Edited by Guest, 25 October 2009 - 12:57 PM.

Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#2
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Cool, but the size of an integer will vary, so you might want to use ...==sizeof(int) instead of 4 to make it more portable. A char is always one byte. Nice idea, though :D
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa