Write a C program to print alphabets from a to z using for loop. How to print alphabets using loop in C programming. Logic to print alphabets from a to z using for loop in C programming.
Example
Input
Output
Alphabets: a, b, c, ... , x, y, z
Required knowledge
Basic C programming, Relational operators, For loop
Logic to print alphabets from a to z
Printing alphabets in C, is little trick. If you are good at basic data types and literals then this is an easy drill for you.
Internally C represent every character using ASCII character code. ASCII is a fixed integer value for each global printable or non-printable characters.
For example – ASCII value of a=97, b=98, A=65 etc. Therefore, you can treat characters in C as integer and can perform all basic arithmetic operations on character.
Step by step descriptive logic to print alphabets.
- Declare a character variable, say ch.
- Initialize loop counter variable from
ch = 'a'
, that goes tillch <= 'z'
, increment the loop by 1 in each iteration. The loop structure should look likefor(ch='a'; ch<='z'; ch++)
. - Inside the loop body print the value of ch.
Program to print alphabets from a-z
/**
* C program to print all alphabets from a to z
*/
#include <stdio.h>
int main()
{
char ch;
printf("Alphabets from a - z are: \n");
for(ch='a'; ch<='z'; ch++)
{
printf("%c\n", ch);
}
return 0;
}
To prove that characters are internally represented as integer. Let us now print all alphabets using the ASCII values.
Program to display alphabets using ASCII values
/**
* C program to display all alphabets from a-z using ASCII value
*/
#include <stdio.h>
int main()
{
int i;
printf("Alphabets from a - z are: \n");
/* ASCII value of a=97 */
for(i=97; i<=122; i++)
{
/*
* Integer i with %c will convert integer
* to character before printing. %c will
* take ascii from i and display its character
* equivalent.
*/
printf("%c\n", i);
}
return 0;
}
If you want to print alphabets in uppercase using ASCII values. You can use ASCII value of A = 65 and Z = 90.
Learn to print alphabets using other looping structures.
Learn more – Program to print all alphabets using while loop.
Output
Alphabets from a - z are: 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
Happy coding 😉
Recommended posts
- Loop programming exercises index.
- C program to check whether a character is alphabet or not.
- C program to check whether a character is alphabet, digit or special character.
- C program to check whether a character is vowel or consonant.
- C program to print ASCII values of all characters.
- C program to print all natural numbers from 1 to n.
- C program to enter any number and print it in words.