C Language Example - Loop Output of 26 Letters

C Language Examples

Loop through and output the 26 letters.

In the following example, we use the variable `letter` to store the letter to be output, then use a `for` loop to repeat 26 times, outputting the letter and adding a space after each one.

Inside the loop, the `printf` function is used to output the value of the variable `letter`; `%c` is `printf`'s format specifier, used to output a character.

Example 1

#include <stdio.h> int main() { char letter = 'A'; // The value of 'A' in ASCII is 65 // Use a loop to print 26 letters for (int i = 0; i < 26; i++) { printf("%c ", letter); letter++; // Increment the letter variable to get the ASCII code of the next letter } printf("\n"); return 0; }

Another example:

Example 2

#include <stdio.h> int main() { char c; for(c = 'A'; c <= 'Z'; ++c) printf("%c ", c); return 0; }

Run 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

If you want to output lowercase letters, simply set the initial value of the variable `letter` to `'a'` (in ASCII code, the value of `'a'` is 97), and then use `letter++` inside the loop:

Example

#include <stdio.h> int main() { char letter = 'a'; // The value of 'a' in ASCII is 97 // Use a loop to print 26 lowercase letters for (int i = 0; i < 26; i++) { printf("%c ", letter); letter++; // Increment the letter variable to get the ASCII code of the next letter } printf("\n"); return 0; }

Run 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

The following example lets you choose whether to input uppercase or lowercase:

Example - Output uppercase or lowercase letters

#include <stdio.h> int main() { char c; printf("Enter u to display uppercase letters, enter l to display lowercase letters:"); scanf("%c", &c); if(c== 'U' || c== 'u') { for(c = 'A'; c <= 'Z'; ++c) printf("%c ", c); } else if (c == 'L' || c == 'l') { for(c = 'a'; c <= 'z'; ++c) printf("%c ", c); } else printf("Error! Invalid character entered."); return 0; }

Run result:

输入 u 显示大写字母,输入 l 显示小写字母: l
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

C Language Examples

other extensions