October 22, 2024
Chicago 12, Melborne City, USA
C++

How to write a piece of source code that can be executed as both C and Python, outputting 'Hello World'


I’m trying to write a piece of source code that is compatible with both C and Python compilers/interpreters, outputting "Hello World" in stdout.

Here are two of my tries:

#if __STDC__

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

#else

print("Hello, World!")
exit()

#endif
""""
#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
"""""

print("Hello World!")

However, both don’t work, leading to compile error.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video