C语言教程
C语言控制语句
C语言函数
C语言数组
C语言指针
C语言字符串
C语言数学函数
C语言结构
C语言文件处理
C预处理器

C 使用循环显示从 A 到 Z 的字符的程序

使用循环从 A 到 Z 显示字符的 C 程序

在本例中,您将学习打印英文字母表中的所有字母。
要理解此示例,您应该了解以下C 编程 主题:
C if...else 语句 C while 和 do...while 循环

打印英文字母的程序

#include <stdio.h>
int main() {
    char c;
    for (c = 'A'; c <= 'Z'; ++c)
        printf("%c ", c);
    return 0;
}
输出
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
在这个程序中, for 循环用于以大写字母显示英文字母。
这里对上面的程序稍作修改。该程序根据用户提供的输入以大写或小写形式显示英文字母。

打印小写/大写字母

#include <stdio.h>
int main() {
    char c;
    printf("Enter u to display uppercase alphabets.\n");
    printf("Enter l to display lowercase alphabets. \n");
    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! You entered an invalid character.");
    }
    return 0;
}
输出
Enter u to display uppercase alphabets. 
Enter l to display lowercase alphabets. 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
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4