While we are on the topic of getchar(), it allegedly returns an int value. Yet:
Code:
int x;
int main()
{
x=getchar(); // enter 3
printf("%d\n",x); // prints out 51 instead
}
Code:
int x;
int main()
{
x=getchar(); // enter 3
printf("%c\n",x); // prints out 3 correctly
}
Code:
char x;
int main()
{
x=getchar(); // enter 3
printf("%d\n",x); // prints out 51 instead
}
Code:
char x;
int main()
{
x=getchar(); // enter 3
printf("%c\n",x); // prints out 3 correctly
}
So it's clear that getchar() is returning the ASCII value, which is always an integer, as it always reads the input as a characters. But it seems like whether we declare int x or char x makes no difference at all? Does that mean in the above discussion, if we had written char ch instead of int c it would be equally valid?