Show that a character is really a number.
What to build
A program that prints one value in two different ways.
Requirements
- Print
C has code N, where C is the character and N its ASCII code. - Print
next is D, where D is the following character in the ASCII table.
Input
One character.
Output
Two lines.
Constraints
The character is a letter or a digit, other than z, Z or 9.
Example
Input:
A
Output:
A has code 65
next is B
Why it works
Characters are stored as numbers, so the same byte can be shown with %c or %d, and arithmetic moves to a neighbouring character.
Examples
Example 1 · Input
A
Expected output
A has code 65 next is B
Example 2 · Input
a
Expected output
a has code 97 next is b
Hints
Hint 1
%d shows a char as its number, and %c shows it as a letter.
Hint 2
c + 1 is the next character in the table.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
char c;
scanf(" %c", &c);
printf("%c has code %d\n", c, c);
char next = c + 1;
printf("next is %c\n", next);
return 0;
}