273 views
0 votes
0 votes

#include <stdio.h>

int main() {

char c;

c=getchar();

printf("%d",c);

return 0;

}

Input: 2

Output:50

Can someone explain this behaviour?

1 Answer

Best answer
2 votes
2 votes

char c;                        /* getchar is a function that reads a single character from the standard input stream stdin, regardless of what it is, and returns it to the program*/

c=getchar();              /* Single character from stdin is assigned to c variable */

printf("%d",c);          /* %d tells printf that the corresponding argument is to be treated as an integer value; the type of the corresponding argument must be int. If we check ASCII value of 2 it is 50. */

                            

NOTE: If you type printf("%c",c); then it will print the character which is assigned to  variable c i.e, input = 2  and output = 2 

EDIT : for more information on ASCII click here and on standard streams click here.

selected by

Related questions

1 votes
1 votes
1 answer
1
tusharb asked Mar 1, 2022
414 views
Suppose we have a 32-bit memory and we have to representunsigned int a = -5What will the memory representation look like?000000…….1011or 111…..1011
2 votes
2 votes
2 answers
4
srestha asked May 13, 2019
917 views
Can someone explain the output of this code? and what (char*) is doing actually?#include<stdio.h struct Ournode{ char x, y, z; }; int main() { struct Ournode p={'1', '0',...