Home > Back-end >  Why in My Code, Type Casting is not Working in C?
Why in My Code, Type Casting is not Working in C?

Time:08-21

it is printing the value to be 55 instead of 7.

#include <stdio.h>

int main()
{
    char a = '7';
    printf("%d", (int)a);
    return 0;
}

CodePudding user response:

ASCII characters are represented by numbers. The numeric value of the character '7' is 55. As the characters '0' to '9' are laid out sequentially in ASCII, you can subtract '0' from '7' to get the number 7.

CodePudding user response:

#include <stdio.h>

int main()
{
char a = '7';
printf("%c",a);
return 0;
}

for character, you need to use %c. Your are getting 55 because in your code you tried to print numerical value of 7.

  • Related