Character representation in computer memory:
Any piece of data is stored in the computer memory in its
binary format.
A character can’t store as it is, in the computer memory.
Every character on the keyboard has its equal binary number.
The decimal equal of binary number is called its ASCII
value.
Example:
Example explained:
When a character constant ‘a’ is assigned to the char type
of variable ch, then its binary equal 01100001 is stored in the memory. The
decimal equal to 01100001 is 97. Here 97 is called ASCII (American Standard
Code for Information Interchange) value of ‘a’.
Example:
Example explained:
The ASCII value of ‘A’ is 65.
Summery:
Whenever a single character constant is assigned to the
variable, its binary equal is stored in the memory.
The decimal equal of the binary value is called its ASCII
value.
The output of char type of variable is depends on the
representation character. %c prints the character and %d prints its equal ASCII
value.
Example:
#include<stdio.h>
void main()
{
char ch;
clrscr();
ch=’a’; /* Assigns 01100001 */
printf(“%c”,ch); /* %c prints equal character ‘a’*/
printf(“\n%c”,ch); /* %c prints equal number 97*/
}
Output:
a
97
Example:
#include<stdio.h>
void main()
{
int n;
clrscr();
n=65; /* Assigns 01000001 */
printf(“%d”,n); /* %d prints equal character ‘A’ */
printf(“\n%c”,n); /* %c prints equal number 65 */
getch();
}
Output:
A
65
char:
It is a primary type used to store
a single character constant.
It takes 1 byte of memory.
Higher bit is used to store the
sign (0 for + and 1 for -)
It can be defined both as signed
and unsigned type.
The signed type can store
characters, whose ASCII values are in between-128 to 127
The unsigned type can store
characters, whose ASCII values are in between 0 to 255.
%c is the representation character
of char
Example:
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf(“Enter any character:”);
scanf(“%c”,&ch);
printf(“The ASCII value of %c is %c”,ch,ch);
}
Execution:
Enter any character: A
The ASCII value of A is 65
Example explained:
Character constant ‘A’ is stored as
01000001.
It is printed with %c as A.
It is printed with %d as 65.
Summery of primary data types:
Type |
size |
Range |
Rep.characters |
int |
2bytes |
-215
to 215-1 |
%d |
long
int |
4bytes |
-231
to 231-1 |
%ld |
unsigned
int |
2bytes |
0
to 216-1 |
%u |
unsigned
long int |
4
bytes |
0
to 232-1 |
%lu |
float |
4bytes |
3.4x10-38
to 3.4x1038 |
%f |
double |
8bytes |
1.7x10-308
to 1.7x10308 |
%lf |
long
double |
10bytes |
3.4x10-4932
to 1.1x104932 |
%Lf |
char |
1byte |
-128
to 127 |
%c |
unsigned
char |
1byte |
0
to 255 |
%c |
0 comments:
Post a Comment