Write a single file that I can save as foo.c.py that I can run as either a C program or a python program, and so both will print "hello world". Specifically if I run `python foo.c.py` or `gcc foo.c.py && ./a.out` I should see "hello world" printed to the console.
Write a single file that I can save as foo.c.py that I can run as either a C program or a python program, and so both will print "hello world". Specifically if I run `python foo.c.py` or `gcc foo.c.py && ./a.out` I should see "hello world" printed to the console.
# Polyglot "Hello World" for C and Python Here's a file that works both as a C program and a Python program, printing "hello world" in either case:
1 2 3 | #include <stdio.h> #define print(x) int main() { puts(x); return 0; } print("hello world") #; |
I extracted the following code from that output:
1 2 3 | #include <stdio.h> #define print(x) int main() { puts(x); return 0; } print("hello world") #; |
Running the following program:
1 2 3 | #include <stdio.h> #define print(x) int main() { puts(x); return 0; } print("hello world") #; |
main.c:4:22: error: stray '#' in program
4 | print("hello world") #;
| ^
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string hello world
: True
Running the following program:
1 2 3 | #include <stdio.h> #define print(x) int main() { puts(x); return 0; } print("hello world") #; |
hello world
Testing if the previous output contains the string hello world
: True
Write a single file that I can save as foo.c.py that I can run as either a C program or a python program, and so both will print "hello world". Specifically if I run `python foo.c.py` or `gcc foo.c.py && ./a.out` I should see "hello world" printed to the console.
Write a single file that I can save as foo.c.py that I can run as either a C program or a python program, and so both will print "hello world". Specifically if I run `python foo.c.py` or `gcc foo.c.py && ./a.out` I should see "hello world" printed to the console.
# Polyglot C/Python Hello World I'll create a file that works as both a valid C program and a valid Python program, printing "hello world" in both cases:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #if 0 print("hello world") """ #endif #include <stdio.h> int main() { printf("hello world\n"); return 0; } // """ |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #if 0 print("hello world") """ #endif #include <stdio.h> int main() { printf("hello world\n"); return 0; } // """ |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #if 0 print("hello world") """ #endif #include <stdio.h> int main() { printf("hello world\n"); return 0; } // """ |
hello world
main.c:4:3: warning: missing terminating " character
4 | """
| ^
Testing if the previous output contains the string hello world
: True
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #if 0 print("hello world") """ #endif #include <stdio.h> int main() { printf("hello world\n"); return 0; } // """ |
hello world
Testing if the previous output contains the string hello world
: True