C Program to Print the ASCII Value of a Character

C Program to Print the ASCII Value of a Character

A classroom exercise asks for an ASCII code. A C character constant comes from the execution character set, so the program must state its encoding assumption.

ASCII assigns numeric codes to a particular character repertoire. C does not require its execution character set to be ASCII; converting a char prints its execution code, which matches ASCII on compatible systems.

Before this exercise, review storing values and reading and checking input. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why character code and ascii uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Compute the numeric value of the known digit character zero without hard-coding its byte code.

Character Code and ASCII: follow the data

  1. Given data

    A classroom exercise asks for an ASCII code. A C character constant comes from the execution character set, so the program must state its encoding assumption.

  2. Rule

    ASCII assigns numeric codes to a particular character repertoire. C does not require its execution character set to be ASCII; converting a char prints its execution code, which matches ASCII on compatible systems.

  3. Observed result

    Execution code for A: 65 ASCII code for A: 65

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

The fixed ASCII value of A is 65. The actual value of the C character constant 'A' is an implementation character-set property, not a language guarantee of 65.

Use an unsigned-char representation when examining a byte and cast it to unsigned for %u. C guarantees the basic decimal digits are contiguous; subtracting '0' from a known digit obtains its numeric digit value without assuming ASCII.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  unsigned char letter = 'A';
  printf("Execution code for A: %u\n", (unsigned)letter);
  printf("ASCII code for A: %u\n", 65u);
  return 0;
}

Example output on an ASCII-compatible verification host

Execution code for A: 65
ASCII code for A: 65
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  unsigned char letter = 'A';
  printf("Execution code for A: %u\n", (unsigned)letter);
  printf("ASCII code for A: %u\n", 65u);
  return 0;
}

The verification compiler’s character constant A has execution code 65.

The separate ASCII reference line is always the stated code 65. The two lines agree on this host but need not on every C implementation.

Follow the worked calculation

  1. Start with the sample

    The verification compiler’s character constant A has execution code 65.

  2. Apply the rule

    The separate ASCII reference line is always the stated code 65. The two lines agree on this host but need not on every C implementation.

  3. Check the result

    Execution code for A: 65 ASCII code for A: 65

    Execution code for A: 65
    ASCII code for A: 65
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  unsigned char letter = 'A';
  printf("Execution code for A: %u\n", (unsigned)letter);
  printf("ASCII code for A: %u\n", 65u);
  return 0;
}

Compute the numeric value of the known digit character zero without hard-coding its byte code.

Worked answer

The subtraction produces zero because the character is the starting digit itself. This uses the guaranteed basic-digit ordering rather than the ASCII value 48.

C17 · complete program

#include <stdio.h>
int main(void) {
  unsigned char digit = '0';
  printf("Digit value: %u\n", (unsigned)(digit - '0'));
  return 0;
}

Expected output

Digit value: 0
Run in C compiler →

Recognize the important boundaries

A byte code is not necessarily a Unicode code point or a user-perceived character. An encoding must be named when those interpretations matter.

Do not assume letters are contiguous merely because decimal digits are. The alphabet-list example uses explicit text.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Does ISO C guarantee that 'A' has value 65?
Choose one answer. You can try again.

Answer and explanation

No; that is the ASCII value, not a universal execution-set guarantee.

Correct. The host may use an ASCII-compatible execution set, but C permits others.

Use the idea independently

ASCII assigns numeric codes to a particular character repertoire. C does not require its execution character set to be ASCII; converting a char prints its execution code, which matches ASCII on compatible systems.

Continue with testing the problem’s boundary cases.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Find Size of int, float, double and char →