Loop Control Statements in C
Introduction to HTML
How to use the Github API
The image tag, anchor tag and the button tag
Ordered and Unordered Lists in HTML
The division tag
HTML Forms
Tables in HTML
Introduction to C Programming
Introduction to Python
Varibles and Datatypes in Python
Operators in Python
Typecasting in Python
Input and Output in Python
If Else in Python
Loops in Python
Break, Continue and Pass in Python
Python practice section 1
Lists in Python
Tuple in Python
ASCII Encoding and Typecasting in C
- What is ASCII ?
- Typecasting
- Implicit Type conversion
- Explicit Type conversion
- How to get ASCII code of a character ?
Jump to specific sections
What is a ASCII ?
Computer stores data only in the form of 0 and 1. Have you ever wondered how it
stores and understands characters like 'r', 'a','*', or '#' etc. It stores all these characters
in the form of a binary number. Every character in C has a unique ASCII Code ( a numeric code) to
identify each character. Below is the list of ASCII ranges of the different characters in C.
Characters | ASCII Ranges |
A-Z | 65-80 |
a-z | 97-122 |
0-9 | 48-56 |
Special Characters like '#','@','-' | 32–47 / 58–64 / 91–96 / 123–126 |
Typecasting
It is a technique to convert a value from one data type to another.
#include <stdio.h>
void main()
{
float f=3.14;
int a=f;
int b=3;
float r=b;
printf("Value of a = %d",a);
printf("Value of ar = %f",r);
}
Output
Value of a = 3 Value of r = 3.000000
Implicit Typecasting
In Implicit Type Conversion, the compiler automatically
converts an integer(lower data type) to float(higher data type).
#include <stdio.h>
void main()
{
float f=5/2.0;
printf("Result = %f",f);
}
Output
Result = 2.500000
In the above code, since one of the numbers is float the compiler automatically
converts the second number into float(higher data type) and then performs float division.
Explicit Type Conversion
In Explicit Type Conversion, the user instructs the compiler to convert from
one data type to another(higher to lower data type or lower to higher data type).
Let's take a look at the following example.
#include <stdio.h>
void main()
{
float f=5/2;
printf("Result = %f",f);
}
Output
2.000000
We expected the output to be 2.5. However, since both 5 and 2 are integers, the compiler
performed integer division which gives the result as 2.
To get the correct output, we write the following code.
#include <stdio.h>
void main()
{
float f=(float)5/2;
printf("Result = %f",f);
}
Output
Result = 2.500000
Here, we explicitly converted 5 to float. Now, since one of the numbers is float, the compiler automatically
converts 2 to float and then float division is performed that gave the result as 2.5.
How to get ASCII code of a character ?
To get the ASCII Code of a given character, we can do something like :
#include <stdio.h>
void main()
{
char ch;
printf("Enter a character : ");
scanf("%c",&ch);
printf("ASCII Code of %c = %d",ch,ch);
}
We can get the ASCII code of any character by typecasting.
Now, ch is of char type but if we convert it to an integer, it gives its ASCII code as result.
Output 1
Enter a character : R ASCII Code of R = 82
Output 2
Enter a character : # ASCII Code of R = 35