Skip to main content

C basic data types

In the exercises, you will frequently see the following C data types.

TypeExplanationexample
intan integer with range (from −2,147,483,648 to +2,147,483,647)int a = 10;
chara characterchar c = 'a';
floata decimal numberfloat grade = 5.50;
Doublea decimal numerdouble pi = 3.1415926;

In C, the data type of a variable should be defined before used. Although there are some way to convert one data type to another, type casting is not suggested.

Counterexample

(1) mixed data type

int a = 2;
float b = 2.0;
int c = a + b;
float d = a + b;

Since a and b have different data types, C complier will do data type casting for c and d. This is not suggested.

Solution: Make a and b the same data type.

float a = 2.0;
float b = 2.0;
folat c = a + b;
int a = 2;
int b = 2;
int c = a + b;