C Program to Print A to Z Using a Loop

C Program to Print A to Z Using a Loop

A classroom chart needs letters in alphabetic order. Incrementing a character code assumes a contiguous letter encoding, which C does not require.

Traverse an explicitly ordered alphabet string rather than deriving letter order from implementation character-code arithmetic.

Before this exercise, review repeating work with loops. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why print the alphabet uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Print only the first five stored letters.

Print the Alphabet: follow the data

  1. Given data

    A classroom chart needs letters in alphabetic order. Incrementing a character code assumes a contiguous letter encoding, which C does not require.

  2. Rule

    Traverse an explicitly ordered alphabet string rather than deriving letter order from implementation character-code arithmetic.

  3. Observed result

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

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

Understand the operation

The stored alphabet lists the intended order directly. The index advances through array elements until it reaches the string terminator.

The index, not the character code, changes each iteration. %c writes one supplied basic character; the trailing space separates labels on the chart.

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) {
  const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  for (size_t i = 0; alphabet[i] != '\0'; i++) printf("%c ", alphabet[i]);
  putchar('\n');
  return 0;
}

Expected output

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  for (size_t i = 0; alphabet[i] != '\0'; i++) printf("%c ", alphabet[i]);
  putchar('\n');
  return 0;
}

Index zero selects A.

Successive indexes select the next explicitly stored letters, stopping before the final null character.

Follow the worked calculation

  1. Start with the sample

    Index zero selects A.

  2. Apply the rule

    Successive indexes select the next explicitly stored letters, stopping before the final null character.

  3. Check the result

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
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) {
  const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  for (size_t i = 0; alphabet[i] != '\0'; i++) printf("%c ", alphabet[i]);
  putchar('\n');
  return 0;
}

Print only the first five stored letters.

Worked answer

A count-based loop visits indexes zero through four. The result is A through E without requiring those character codes to be consecutive integers.

C17 · complete program

#include <stdio.h>
int main(void) {
  const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  for (size_t i = 0; i < 5; i++) printf("%c ", alphabet[i]);
  putchar('\n');
  return 0;
}

Expected output

A B C D E 
Run in C compiler →

Recognize the important boundaries

English alphabet order is this exercise’s explicit repertoire; it is not a universal ordering for every language or Unicode text.

Do not access index 26 as a printable letter in this 26-letter string; it designates the terminator.

Check your understanding

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

Why store the alphabet explicitly?
Choose one answer. You can try again.

Answer and explanation

C does not require letter codes to be consecutive in alphabetic order.

Correct. The sequence makes the desired order an input rather than an encoding assumption.

Use the idea independently

Traverse an explicitly ordered alphabet string rather than deriving letter order from implementation character-code arithmetic.

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 Check Whether a Number Is Prime →