I was trying to explain to my friend something about C coding and he asked me why his code (with "scanf") didn't work.
#include
int main() {
char x=scanf("%c",&x);
printf("%c\n",x);
return 0;
}
and this one yes
#include <stdio.h>
int main()
{
int k;
char x=getchar
printf("%c\n",x);
return 0;
}
When scanf
completes, x
contains the character that was read. However, that value is immediately overwritten when x
is assigned the return value of scanf
, which is the number of items successfully matched or EOF in the event of an error.
If you call scanf
without assigning the return value to x
you should get the expected result.
For example, this should work.
char x;
scanf("%c",&x);