Examples on Characters and ASCII Values in C

For Complete YouTube Video: Click Here

In this class, we will try to understand Examples of Characters and ASCII Values in C.

We have already explained character datatype and ASCII codes.

We can use the ASCII codes either as a character or as an integer.

We will try to understand this by using an example.

Examples on Characters and ASCII Values

Example 1

The image below is an example using the characters and ASCII values.

Examples on Characters and ASCII Values in C 1
Examples on Characters and ASCII Values in C 1

In the above example, we have defined a character variable ch with value A.

As the ASCII code is 65, the compiler will store the binary value (10000001) of 65 in “ch.”

In the printf statement, we are printing the value stored in “ch” in its integer format and corresponding character format.

Example 2

The image below is an example using the characters and ASCII values.

Examples on Characters and ASCII Values in C 2
Examples on Characters and ASCII Values in C 2

In the above example, we have done an increment operation on the character variable.

Even though it is a character variable, we can do arithmetic operations also.

The first printf in the above program prints, ” The character A has an ASCII code of 65.”

In the following line of code, the value stored in ch will get incremented to 66.

The second printf in the above program prints, ” The character B has an ASCII code of 66.”

The point that we have to understand is we can perform arithmetic operations on character variables.