I'm trying to invoke a simple C Program from a python script (as a use case for another complex problem) but for some reason that I didn't understand, the C program not working the way I expect.
C Program testC.c
:
#include <stdio.h>
int main(void){
int num;
printf("Inside C Program!\n");
printf("Enter a Number: ");
scanf("%d", &num);
printf("%d\n", num*num);
return 0;
}
Python Program:
import subprocess
def run_c():
subprocess.call(['gcc', '-o', 'c', 'testC.c'])
subprocess.call('./c.exe')
def main():
print("Inside Python Code")
run_c()
print('Task is done')
if __name__ == '__main__':
main()
Expected Output:
Inside Python Code
Inside C Program!
Enter a Number: 5
25
C Program is over
Task is done
Actual Output:
Inside Python Code
5 <-- For some reason, I've to enter the num here to let the program execute.
Inside C Program!
Enter a Number: 25
C Program is over
Task is done
as you can see, I must enter the num
variable before any printf
that actually came before the scanf("%d", &num);
. I'll appreciate any help to understand better why that's happening.