Sharedsupport / 2015-05-06-053839-c-worksheet.sagewsOpen in CoCalc
Authors: Harald Schilly, ℏal Snyder, William A. Stein
License: GNU General Public License v3.0
Description: Examples for support purposes.
%auto %sage def c(program): open("temp.c",'w').write(program) print os.popen("gcc temp.c -o a.out 2>&1 && ./a.out 2>&1").read() default_mode('c')
#include<stdio.h> int main(void) { printf("Hello world.\n"); }
Hello world.
#include<stdio.h> int main(void) { int n = 5, int m = 10; printf("Hello world: %d\n", n+m); }
temp.c: In function ‘main’: temp.c:3:16: error: expected identifier or ‘(’ before ‘int’ int n = 5, int m = 10; ^ temp.c:4:35: error: ‘m’ undeclared (first use in this function) printf("Hello world: %d\n", n+m); ^ temp.c:4:35: note: each undeclared identifier is reported only once for each function it appears in
#include<stdio.h> int main(void) { int n = 5, m = 10; printf("Hello world: %d\n", n+m); }
Hello world: 15