Convert ASCII number to ASCII Character in C

Viewed 187055

In C is there a way to convert an ASCII value typed as an int into the the corresponding ASCII character as a char?

5 Answers
char A;
printf("ASCII value of %c = %d", c, c);

In this program, the user is asked to enter a character. The character is stored in variable c. When %d format string is used, 65 (the ASCII value of A) is displayed. When %c format string is used, A itself is displayed.

Output: ASCII value of A = 65

#include <stdio.h>
#include <cs50.h>
int d;
int main(void){
    string a = get_string("enter your word: ");
    int i = 0;
    while(a[i] != '\0'){
        printf("%i ", a[i]);
        i++;
    }
    printf("\n");

}
Related