Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C Program to print all ASCII values.
ASCII (American Standard Code for Information Interchange) is a character encoding standard that assigns numeric values to characters. In C programming, we can print ASCII values of characters using format specifiers without explicitly converting characters to integers.
Syntax
printf("%c \t %d<br>", i, i);
Method 1: Printing ASCII Values from A to z (65 to 122)
This example prints ASCII values for uppercase letters, special characters, and lowercase letters −
#include <stdio.h>
int main() {
int i;
printf("Character \t ASCII Value<br><br>");
// Print ASCII values from A to z
for (i = 65; i <= 122; i++) {
printf("%c \t\t %d<br>", i, i);
}
return 0;
}
Character ASCII Value A 65 B 66 C 67 D 68 E 69 F 70 G 71 H 72 I 73 J 74 K 75 L 76 M 77 N 78 O 79 P 80 Q 81 R 82 S 83 T 84 U 85 V 86 W 87 X 88 Y 89 Z 90 [ 91 \ 92 ] 93 ^ 94 _ 95 ` 96 a 97 b 98 c 99 d 100 e 101 f 102 g 103 h 104 i 105 j 106 k 107 l 108 m 109 n 110 o 111 p 112 q 113 r 114 s 115 t 116 u 117 v 118 w 119 x 120 y 121 z 122
Method 2: Printing All ASCII Values (0 to 255)
To print all possible ASCII values including control characters, digits, and extended ASCII −
#include <stdio.h>
int main() {
int i;
printf("ASCII Value \t Character<br><br>");
// Print all ASCII values from 0 to 255
for (i = 0; i < 256; i++) {
if (i >= 32 && i <= 126) {
printf("%d \t\t %c<br>", i, i);
} else {
printf("%d \t\t [Non-printable]<br>", i);
}
}
return 0;
}
Key Points
- The
%cformat specifier prints the character representation of an integer. - The
%dformat specifier prints the integer value (ASCII code). - ASCII values 32-126 represent printable characters.
- Values 0-31 and 127-255 are control or extended characters.
Conclusion
Printing ASCII values in C is straightforward using format specifiers. The same integer can be displayed as both a character and its numeric ASCII value using %c and %d respectively.
Advertisements
