C Program to Display Characters from A to Z Using Loop

Program to print characters from 'A' to 'Z' using loop :


In this example, we are using a for loop to print alphabets from ‘A’ to ‘Z’. We have a char type variable ch, which is initialized with the value ‘A’ and incremented by 1 on every loop iteration. At every loop iteration, the value of ch is printed. The loop ends when the variable ch prints ‘Z’.
Program :
#include <stdio.h>
int main() {
  char ch;
  //Print characters from 'A' to 'Z'
  for (ch = 'A'; ch <= 'Z'; ++ch)
    //there is a whitespace after %c so that the
    //characters have spaces in between.
    printf("%c ", ch);
  return 0;
}
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 

Print A to Z in lowercase: 'a' to 'z' :


Similarly, you can print ‘A’ to ‘Z’ in lowercase. The logic is same as first example. Here ch is initialized with value ‘a’ and the loop ends when the condition ch <= 'z' meets.
Program :
#include <stdio.h>
int main() {
  char ch;
  //Print characters from 'a' to 'z'
  for (ch = 'a'; ch <= 'z'; ++ch)
    printf("%c ", ch);
  return 0;
}
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