I tried to compile this on CodeBlocks (version 13.12). Somehow, the pointer is incremented before it is called by toupper()
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char str[80] = "the only way";
char *p;
p = str;
while(*p){
*p++ = toupper(*p);
}
printf(str);
return 0;
}
This leads to the output "HE ONLY WAY" where as I am looking for the output "THE ONLY WAY". I tried it on my computer where it gives the wrong output. However, when I tried this code on a friends computer the code ran fine, giving the output "THE ONLY WAY". My question is why does my output differ? P.s If I replace
*p++ = toupper(*p);
with
*p++ = toupper( *(p-1));
I get the desired output of "THE ONLY WAY".