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.
Explain
Explain why print the alphabet uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Print only the first five stored letters.
Print the Alphabet: follow the data
Given data
A classroom chart needs letters in alphabetic order. Incrementing a character code assumes a contiguous letter encoding, which C does not require.
Rule
Traverse an explicitly ordered alphabet string rather than deriving letter order from implementation character-code arithmetic.
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
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
Start with the sample
Index zero selects A.
Apply the rule
Successive indexes select the next explicitly stored letters, stopping before the final null character.
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
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.
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.