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.
Here’s a single file `foo.c.py` that can be compiled and run as a C program or interpreted as a Python script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /* This is a polyglot file that works as both a C program and a Python script. */ #if 0 """ """ #include <stdio.h> int main() { printf("hello world\n"); return 0; } #define PYTHON #endif #ifdef PYTHON print("hello world") #endif |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /* This is a polyglot file that works as both a C program and a Python script. */ #if 0 """ """ #include <stdio.h> int main() { printf("hello world\n"); return 0; } #define PYTHON #endif #ifdef PYTHON print("hello world") #endif |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /* This is a polyglot file that works as both a C program and a Python script. */ #if 0 """ """ #include <stdio.h> int main() { printf("hello world\n"); return 0; } #define PYTHON #endif #ifdef PYTHON print("hello world") #endif |
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string hello world
: False
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /* This is a polyglot file that works as both a C program and a Python script. */ #if 0 """ """ #include <stdio.h> int main() { printf("hello world\n"); return 0; } #define PYTHON #endif #ifdef PYTHON print("hello world") #endif |
File "/usr/src/app/main.py", line 2
/*
^
SyntaxError: invalid syntax
Testing if the previous output contains the string hello world
: False