I just learned C and fascinates with pointers. Recently I discovered C function system()
. I am able to get return values from a program I executed via system
("program.exe
"). Eg, program.c
:
#include <stdio.h>
int main(){
printf("hello world\n");
return 123;
}
and this code calls program.exe
, called call.c
#include <stdio.h>
#include <stdlib.h>
int main(){
int a;
printf("calling program.exe:\n");
a=system("program.exe");
printf("program.exe returned %d at exit\n",a);
return 0;
}
When I execute call.exe
, I get this
calling program.exe:
hello world
pm returned 123 at exit
I was like, wow! This return value and system()
function thing is like a new way to interprocess communication for me. But my question is, can I get a string return from the system()
function?
Ii tried changing program.c
"int main()
" to "char * main()
", and return 123
to return "bohemian rhapsody"
and change "int a;
" to "char *a;
", printf
format %d
to %s
in call.c
, but I only get funny characters when I execute call.exe. I wonder what's wrong?