Download these FREE Ebooks:
1. Introduction to Digital Marketing
2. Website Planning and Creation
Main Data Types in C
Every element in C does have a corresponding data type. Each data type has a different set of operations that may be performed on it as well as different memory needs. Let's quickly go through each one:Here are some examples of some of the most common data types used in C:
- Char is the most basic data type in C. It saves a single character and utilises a single byte of memory in almost all compilers.
- An integer is stored in an int variable, as its name indicates.
- Floating point numbers with decimal values are stored as only one floats.
- Double-precision decimal integers are stored inside it (numbers with floating point values).
Data Type |
Memory (bytes) |
Range |
Format Specifier |
---|---|---|---|
short int |
2 |
-32,768 to 32,767 |
%hd |
unsigned short int |
2 |
0 to 65,535 |
%hu |
unsigned int |
4 |
0 to 4,294,967,295 |
%u |
int |
4 |
-2,147,483,648 to 2,147,483,647 |
%d |
long int |
4 |
-2,147,483,648 to 2,147,483,647 |
%ld |
unsigned long int |
4 |
0 to 4,294,967,295 |
%lu |
long long int |
8 |
-(2^63) to (2^63)-1 |
%lld |
unsigned long long int |
8 |
0 to 18,446,744,073,709,551,615 |
%llu |
signed char |
1 |
-128 to 127 |
%c |
unsigned char |
1 |
0 to 255 |
%c |
float |
4 |
%f |
|
double |
8 |
%lf |
|
long double |
16 |
%Lf |
For Example:
#include
int main()
{
int a = 1;
char b = 'G';
double c = 3.14;
printf("Hello World!\n");
// printing the variables defined
// above along with their sizes
printf("Hello! I am a character. My value is %c and "
"my size is %lu byte.\n",
b, sizeof(char));
// can use sizeof(b) above as well
printf("Hello! I am an integer. My value is %d and "
"my size is %lu bytes.\n",
a, sizeof(int));
// can use sizeof(a) above as well
printf("Hello! I am a double floating point variable."
" My value is %lf and my size is %lu bytes.\n",
c, sizeof(double));
// can use sizeof(c) above as well
printf("Bye! Take care. :)\n");
return 0;
}
Output: