You are on page 1of 7

Character Handling in C Programming

Data types are used to classify data which tells the compiler or interpreter how the
programmer intends to use the data. Most programming languages support various
types of data, including integer, real, character or string, and Boolean.

Our interactions (inputs and outputs) with a program are treated as a stream of
bytes. These bytes represent data that can be interpreted as representing values
that we understand. Additionally, within a program, we process this data in various
ways such as adding them up or sorting them. This data comes in different forms
such as:

 Your name (usually a string of characters)


 Age (usually an integer)
 Price of an item (floating point)

In C programming the size of the data types are as follows:

 Integer or int values are stored in 2 or 4 bytes depending on your OS. You can
use printf (“%d”, sizeof(int)); to verify.
 Character or char values are stored in 1 byte.
 Floating point or float values are stored in 4 bytes.
How Does scanf() Read Data?

When scanf() is used to read an integer from the user (standard input) it will skip
over all the white spaces until it either encounters the number or newline character
(\n).

int num;

scanf (“%d”, &num);

When scanf() is used to read a character from the user it reads the very next
character and stores it in a variable.

char ch;

scanf (“%c”, &ch);

Remember that a character can be:

 Any number character between ‘0’ and ‘9’


 Any lower case letter between ‘a’ and ‘z’
 Any upper case letter between ‘A’ and ‘Z’
 Any special character on the keyboard e.g. ‘<’, ‘+’, ‘*’, ‘&’ etc.
When we enter a character or multiple characters in the form of a string we usually
press enter to allow the scanf function to read from the user input. Pressing enter
creates a new line character right after the character or at the end of the string of
characters. When we type “John” for example and press enter what the program
sees is “John\n”.
Character Arithmetic

Short for American Standard Code for Information Interexchange, ASCII is a


standard that assigns letters, numbers, and other characters in the 256 slots
available in the 8-bit code. The ASCII decimal (Dec) number is created from binary,
which is the language of all computers. As shown in the table below, the lowercase
"h" character (char) has a decimal value of 104, which is "01101000" in binary.

ASCII TABLE

Char Dec Binary Char Dec Binary Char Dec Binary

! 033 00100001 A 065 01000001 a 097 01100001

" 034 00100010 B 066 01000010 b 098 01100010

# 035 00100011 C 067 01000011 c 099 01100011

$ 036 00100100 D 068 01000100 d 100 01100100

% 037 00100101 E 069 01000101 e 101 01100101

& 038 00100110 F 070 01000110 f 102 01100110

' 039 00100111 G 071 01000111 g 103 01100111

( 040 00101000 H 072 01001000 h 104 01101000

) 041 00101001 I 073 01001001 i 105 01101001

* 042 00101010 J 074 01001010 j 106 01101010

+ 043 00101011 K 075 01001011 k 107 01101011

, 044 00101100 L 076 01001100 l 108 01101100

- 045 00101101 M 077 01001101 m 109 01101101

. 046 00101110 N 078 01001110 n 110 01101110

/ 047 00101111 O 079 01001111 o 111 01101111

0 048 00110000 P 080 01010000 p 112 01110000

1 049 00110001 Q 081 01010001 q 113 01110001

2 050 00110010 R 082 01010010 r 114 01110010


3 051 00110011 S 083 01010011 s 115 01110011

4 052 00110100 T 084 01010100 t 116 01110100

5 053 00110101 U 085 01010101 u 117 01110101

6 054 00110110 V 086 01010110 v 118 01110110

7 055 00110111 W 087 01010111 w 119 01110111

8 056 00111000 X 088 01011000 x 120 01111000

9 057 00111001 Y 089 01011001 y 121 01111001

: 058 00111010 Z 090 01011010 z 122 01111010

; 059 00111011 [ 091 01011011 { 123 01111011

< 060 00111100 \ 092 01011100 | 124 01111100

= 061 00111101 ] 093 01011101 } 125 01111101

> 062 00111110 ^ 094 01011110 ~ 126 01111110

? 063 00111111 _ 095 01011111 _ 127 01111111

@ 064 01000000 ` 096 01100000

From the table we can see that the range of decimal values for numbers stored as
characters are from ‘0’ decimal value of 48 to ‘9’ decimal value of 57.

If we have a number that is stored as a character e.g. char ch = ‘5’; and we would
like to get the integer (decimal) value of that character, we need to perform some
character arithmetic.

char ch = ‘5’;
int num;
num = ch – ‘0’;
The last statement is saying that we want to minus the decimal value of ‘0’ from
the decimal value of the character value stored in ch which is ‘5’. This gives us:

num = 53 – 48 = 5

This method can be used to convert any number character to it integer value. It can
also be used with the letters of the alphabet.

Below are some functions in the <ctype.h> header file that are useful in character
handling:
 getchar() – used to read a character from the standard input (keyboard)
e.g. char ch = getchar();
 islower() – used to determine if a character is a lower case letter
e.g. if(islower(ch))
 isupper() – used to determine if a character is an uppercase letter
e.g. if(isupper(ch))
 isdigit() – used to determine if a character is a digit (‘0’ to ‘9’). E.g. if(isdigit(ch))
Converting Binary to Decimal

000111000

Most Least
Significant Bit Significant Bit
(MSB) (LSB)

28 27 26 25 24 23 22 21 20

0 0 0 1 1 1 0 0 0

20 x 0=0
21 x 0=0
22 x 0=0
23 x 1=8
24 x 1= 16
25 x 1= 32
26 x 0=0
27 x 0=0
28 x 0=0
TOTAL 56

Decimal Value of the character ‘8’ = 0 +0 + 0 + 32 + 16 + 8 + 0 + 0 + 0 = 56


Decimal to Binary Conversion.
To convert a decimal number -:
1. Write down the number.
2. Divide it by 2 and note the remainder.
3. Divide the quotient obtained by 2 and note the remainder.
4. Repeat the same process till we get 0 as the quotient.
5. Write the values of all the remainders starting from the bottom to the top.

e.g.
Convert 25 into binary.

2 25 2510 to binary is 110012


2 12 r 1
2 6 r0 Read the remainders
from the bottom to
2 3 r0 the top to find the
2 1 r1 binary number

0 r1

You might also like