Wednesday, July 20, 2022

[FIXED] Why do we get the ASCII characters with int i within %c?

Issue

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?


Solution

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



Answered By - duong.ld
Answer Checked By - David Goodson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.