Home > Back-end >  Why do we get the ASCII characters with int i within %c?
Why do we get the ASCII characters with int i within %c?

Time:12-07

If you run the following code you will get the ASCII characters, even though we use int i within %c inside the for loop.

#include <stdio.h>  
    
int main()  
{  
    int i;  
    
    for (i = 0; i <= 255; i  ) /*ASCII values ranges from 0-255*/  
    {  
        printf("ASCII value of character %c = %d\n", i, i);  
    }  

    return 0;  
}   

Could you please advise how is this possible since there are characters inside ASCII?

CodePudding user response:

printf() - Print formatted we use format specifiers to specify the format of given variable, %d format specifier to display the value of an integer variable. %c is used to display character as ASCII values ranges from 0-256 , %c will print the respective character symbol of number in Integer i

CodePudding user response:

Every variable in the computer is stored as binary and its value will depend on the type you define. For example, in memory store the value 1010. If you cast it as int, it will print -6, if you cast it as unsigned int, it will print 10. Same for int and char. It depends on you

  • Related