Using C Datatypes (with Examples)
The Datatype defines the type of data being used. We have covered C datatypes in detail in the previous tutorial.
The C language has 5 basic (primary or primitive) data types, they are:
-
Character - char
-
Integer - int
-
Floating-point - float
-
Double - double
-
Void - void
Let's learn about each one of them one by one.
To learn about the size of data types, range of values for datatypes, and various type modifiers like signed
, unsigned
, long
, and short
- Visit C datatypes in detail
1. char Datatype
The char
datatype refers to character values, enclosed in single quotes, with a range of -127 to 127.
As it's clear from the range, we can even use small integer values in the char
datatype.
For example,
char status = 'Y';
2. int Datatype
The int
datatype is used to store whole numbers, which are values without any decimal part or exponent part.
The int
datatype can store decimal (base 10), octal (base 8), and hexadecimal (base 16) values.
// simple int value
int a = 100;
// negative value
a = -100;
// unsigned int value - with suffix U or u
int x = 1000U;
// long int value
long int long_val = 3500L;
With the value of int
data type, we can use suffix U
or u
, to tell the compiler that the value is for unsigned
int
data type and suffix L
or l
for a long
int
value.
Learn more about type identifiers(signed
, unsigned
, long
, and short
) and how they change the primary datatypes when used with them - Visit C datatypes guide for beginners
3. float Datatype
The float
data type is used to store real numbers which may have a decimal (fraction) part or an exponential part. It is a single-precision number.
Let's see some examples for float
value,
float x = 127.675;
// with suffix F or f
float y = 1000.5454F;
Just like int
datatype, the float
can also be used with type modifiers - Learn more about it, visit C datatypes guide for beginners
4. double Datatype
The real numbers which are big enough that they cannot be stored in float
datatype, is stored as a double
datatype. It is a double-precision number. A double datatype value can hold above 15 to 17 digits before the decimal point and 15 to 17 digits after the decimal point.
Here is an example,
double x = 424455236424564.24663224663322;
We should only use the double
datatype when we need such large numbers, otherwise not, because using double
datatype makes the program slow.
Conclusion:
In this tutorial, we saw some examples for different data types available in the C language. To understand about the C data types in detail, visit C datatypes guide for beginners .