I'm having issues with accepting user input and the printing its ascii value in C. I'm tasked with writing a program that simply takes a single char as input and prints out its ascii value, and only stops when the user inputs 0 (the ascii value of 0 is 48). My problem is that if the the printf seems to function one loop behind scanf.
while(x == 1){
scanf("%c\n",&thisChar);
ascii = thisChar;
if(ascii == 48){
x = -1;
}
printf("Ascii: %d\n", ascii);
}
For example, when I run this from the command line, I get something like this:
f
0
Ascii: 102
f
Ascii: 48
and then the program ends. With those same inputs, I want the output to be:
f
Ascii: 102
0
Ascii: 48
and then end there. What is the error in my logic?
"%c\n"to" %c"- BLUEPIXYscanffamily of functions! - hydeascii == '0'is absolutely equivalent toascii == 48only that it is easier to understand what your code is supposed to mean. - VoidStar