user1809300
user1809300

Reputation: 723

print letters and their ascii codes

i try to print all the letter from A to Z and from a to z and their ascii codes but i end up with an infinite loop when i am trying to run it ,so where is the mistake ?

#include <stdio.h>

int main(void) {
 int i;
 char ch_1,ch_2;

 for (ch_1='A'; ch_1<='Z'; ch_1++) printf("letter: %c ASCII code:%d\n",ch_1,ch_1);
 for (ch_2='a'; ch_1<='z'; ch_2++) printf("letter: %c ASCII code: %d\n",ch_2,ch_2);
}

Upvotes: 1

Views: 1891

Answers (3)

avinashse
avinashse

Reputation: 1460

your mistake is in the second for loop condition. your wrote ch_1 instead of ch_2.

Upvotes: 2

Roy Dictus
Roy Dictus

Reputation: 33139

In the 2nd for line, your end-of-sequence function is wrong. It says:

ch_1<='z'

and it should say:

ch_2<='z'

A common mistake!

Upvotes: 4

MOHAMED
MOHAMED

Reputation: 43528

for (ch_2='a'; ch_2<='z'; ch_2++)

and not

for (ch_2='a'; ch_1<='z'; ch_2++)

Upvotes: 9

Related Questions