3

I basically want to know what to put in the last printf statement for the first %d, because from my understanding getchar converts the inputted character to ASCII code. So how do I display the inputted 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;
}
jambrothers
  • 1,540
  • 1
  • 15
  • 21
Michael
  • 139
  • 1
  • 12
  • 2
    `printf("The ASCII code for the character %c is %d\n", c, c);` – r3mainer May 25 '17 at 09:19
  • Your title seems to be the opposite of what you're actually asking. You don't want to know how to display the integer, you want to know how to convert the code back to a character. – Barmar May 25 '17 at 09:20
  • A character *is* its ASCII code (if your system uses ASCII) so there's no conversion. – n. m. could be an AI May 25 '17 at 09:41
  • Well, it's more likely to be a superset of ASCII than ASCII itself. Perhaps CP437 or UTF-8. Sooo, expect that getchar() might return any value from the execution character set—as well as sentinel values (EOF). – Tom Blodget May 25 '17 at 16:52

1 Answers1

3

You need to provide the %c format specifier in the format string instead of %d:

printf("The ASCII code for the character %c is %d\n", c, c);
JFMR
  • 23,265
  • 4
  • 52
  • 76