I can't seem to understand what exactly is going on here
#include <stdio.h>
const char* mes(int a)
{
static char mess[100];
sprintf(mess, "%d", a);
return mess;
}
const int* hes(int a)
{
static int arr[100];
arr[0] = a;
return arr;
}
int main()
{
printf("%s %s\n", mes(55), mes(25)); //55 55
printf("%s\n", mes(55)); //55
printf("%s\n", mes(25)); //25
printf("%d %d\n", hes(55)[0], hes(25)[0]); //55 25
}
In the first printf the second function seems to be ignored and the output of the earlier input gets printed again.
At first I assumed it was a static variable issue, so I tried printing them separately and then they seem to work fine.
Then I assumed it was a printf issue so I tried to simulate the same behavior with an integer array, and it worked fine there too.
I've run this program a couple of times with various inputs, ruling out the possibility of UB.
So, what exactly am I missing here?
EDIT: I encountered this issue somewhere else and couldn't understand what was happening. So I reproduced the issue in a short sample code. But my question stands, (as many have mentioned) are all the parameters evaluated before printing? If so there should be an overwrite in both cases (int and char array) regardless of evaluation order.