Sunday, 7 November 2010

40. ASCII Characters & Characters Constants

ASCII Characters


ASCII stands for American Standard Code for Information Interchange.
A character is 8 bits long.
The characters are represented by an 8 bit integer.
For example, the character code for ‘A’ is 65 and the character code for ‘a’ is 97.

Character Constants

All characters are represented as integers (usually signed), and can be treated as integers.
Escape codes correspond to characters, for use in single-quotes:
Examples: \n (newline), \\ (backslash), \" (double quote)
Example use: char a = ’\n’;
Variables of type char can be thought of as either a character of an integer.
printf( "%c", ’a’ ); /* a is printed */
printf( "%d", ’a’ ); /* 97 is printed */
printf( "%c", 97 ); /* a is printed */
printf( "%d", 97 ); /* 97 is printed */


Lower-case letters, upper-case letters, digits “consecutive”
’a’ == 97, ’b’ == 98, . . ., ’z’ == 122
’A’ == 65, ’B’ == 66, . . ., ’Z’ == 90
’0’ == 48, ’1’ == 49, . . ., ’9’ == 57
• Some more examples of the integer values corresponding to character constants:
’ ’ == 32, ’*’ == 42, ’\n’ == 10, ’\\’ == 92, . . .


Characters


#include<stdio.h>
void main() {
char i;
printf( "Here’s the alphabet, in lower-case:\n" );
for( i = 'a'; i <='z'; i++ ) { printf( "%c", i ); } printf( "\n\nHere’s the alphabet, in upper-case:\n" ); for( i = 'A'; i <= 'Z'; i++ ) { printf( "%c", i );
}
}

 Post by J.siam
 Ref Herbert Schildt

No comments:

Post a Comment