I was going through this program:
#include<stdio.h>
main()
{
int c;
c = getchar();
while(c != EOF) {
putchar(c);
c = getchar();
}
}
Since the variable c is integer, it should store the integer equivalent value for the given input. Output shows 'a' is printed as 'a', 'b' as 'b' and 'c' as 'c' but, when I enterd the vale 65, output was also 65! So there must be some difference in the storage formats of the value 65 and the char 'a'
How are both values discriminated from each other?
It’s because input
65is being considered as two different character inputs, and the program is printing them sequentially.When you entered
6, the program reads it instantly and checks that it’s notEOF, so it’s printing it to the console. Then when you entered5, it’s again taking it as an input. Since this is also notEOF, your program is again printing it.So you are entering two characters, and this program is printing it correctly to the console, just as it supposed to do. Other than that, there is no discrimination.
a‘s integer equivalent is 65, so it will print 65 if you print a character like this –and this will print
a–