ARITHMETIC OPERATIONS ON CHARACTERS
C allows us
to manipulate characters the same way we do with numbers. Whenever a character
constant or a character variable is used in
an expression, it is automatically converted into an integer value.
an expression, it is automatically converted into an integer value.
x=’a’;
printf(“%d\n”,x);
will display the number 97 at the screen.
It is also possible to perform arithmetic operations on character constants and
variables.
x=’z’ – 1;
is a valid statement. In ASCII the value
of ‘z’ is 122 and therefore the statement will assign the value 121 to variable
x.
We
may also use character constants in relational expressions for example, the expression
ch>=
‘A’ && ch<=’Z’
would test whether the character contained
in the variable character is an upper-case letter. We can convert a character
digit
to its equivalent integer value using the following relationship.
to its equivalent integer value using the following relationship.
x=character
–‘0’;
=ASCII of’5’ – ASCII of ‘0’
=53 – 48
=5.
atoi()
This function converts a string of digits into their integer values.
Syntax: x
= atoi(string);
Program
void main()
{
Char number[6] = “2012”;
int n;
n=atoi(number);
printf(“%d”,n);
}
Output
2012
No comments:
Post a Comment