To make the question clear, I wrote some test codes:
#include <stdio.h>
#include <string.h>
char *foo(int a) {
printf("%d\n", a);
static char string[2];
string[0] = a > 0? '1' : '0';
string[1] = '\0';
return string;
}
int main(void) {
printf("%s\t%s\n", foo(1), foo(0));
return 0;
}
Running the code gives the output as follows:
0
1
1 1
I have two questions: 1. Why is 0 printed to 1? in the main printf function does the second foo run before the first? Whether this is a specific behavior or by accident. 2. Why is the final result 1, 1? Expected result should be 1, 0.
source
share