How can I convert ASCII code to corresponding int using getchar?

Basically I want to know what to put in the last statement printf

for the first %d

one because from my understanding it getchar

converts the entered character to ASCII code. So how do I display the entered character?

#include <stdio.h>

int main(void) {
    int c;
    printf("Enter a character: ");
    c = getchar();
    printf("The ASCII code for the character %d is %d\n", what to put here, c);
    return 0;
}

      

+3


source to share


1 answer


You need to include the format specifier %c

in the format string instead of %d

:



printf("The ASCII code for the character %c is %d\n", c, c);

      

+3


source







All Articles