Ad

Using %c In Scanf And Assigning The Value To An Int Variable In C?

- 1 answer

int main()
{
    int i= 0;
    printf("i value is %d\n",i);
    scanf("%c", &i);  // I am giving an input of 255 here

    printf("i after scan %d\n",i); // This prints 50. How???
    
    return 0;
}

Can someone explain how does the printf statement give 50? I have a little-endian machine.

Ad

Answer

Your program won't even compile as I is undeclared. I am assuming that it is a typo. Since you are scanning %c it will read only one character which is 2 from 255. Now 2 has ascii code of 50 which is being printed.

Ad
source: stackoverflow.com
Ad