I’m trying to take a character as input, and give its ascii value as output until the user gives 0 as input. It works, but there is always an extra ascii value of 10 being displayed. what am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
int main(void){
printf("Welcome to ASCII:\n");
char input;
while(input != 48){
scanf("%c", &input);
printf("ascii: %d\n", input);
}
printf("done\n");
}
output
Welcome to ASCII:
e
ascii: 101
ascii: 10
h
ascii: 104
ascii: 10
l
ascii: 108
ascii: 10
0
ascii: 48
done
10 is the ASCII value for
'\n', the newline character generated when you press Enter. You could add a check for that character and not print its value.Also it’s good style to use
'0'instead of writing the ASCII value 48.