In this chapter we are going to learn about Data Types.
Data Types provides information about what kind of data we are going to store in a Variable we declare.
Primitive Data Types in C
- Integer
- Character
- Float
- Double
- Void
Integer stores whole numbers. Character stores one character. Float stores number with decimal point. Double stores same as float but with higher precision. Void type means nothing. When we declare function as a void it returns nothing.
Type | Size | Range |
char | 1byte | -128 to 127 |
unsigned char | 1byte | 0 to 255 |
int | 2 bytes or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
unsigned int | 2 bytes or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
short | 2 bytes | -32,768 to 32,767 |
unsigned short | 2 bytes | 0 to 65,535 |
long | 4 bytes for 32bit os or 8 bytes for 64bit os | -2,147,483,648 to 2,147,483,647 or -9223372036854775808 to 9223372036854775807 |
unsigned long | 8 bytes | 0 to 18446744073709551615 |
Floating Point Types
Type | Size | Range | Precision |
float | 4 byte | 1.2E-38 to 3.4E+38 | 6 decimal places |
double | 8 byte | 2.3E-308 to 1.7E+308 | 15 decimal places |
long double | 10 byte | 3.4E-4932 to 1.1E+4932 | 19 decimal places |
Format Specifiers
To understand format specifiers, we will run following program and you can understand everything yourself.
test.c
#include<stdio.h>
int main()
{
int loan_amount_in_dollar = 1000;
float percentage_interest=2.50;
char option='y';
double precise_debt=2.6543434343984394839483;
char string[]={'H','e','l','l','o'};
printf("\n %s", string);
printf("\n %d",loan_amount_in_dollar);
printf("\n %f", percentage_interest);
printf("\n %lf",precise_debt);
printf("\n %c \n\n", option);
return 0;
}
Following is the Output of the above program.
$ gcc test.c
$ ./a.out (press enter to run a.out)
Hello
1000
2.500000
2.654343
y
Format Specifiers are used to display value of the variable of specific data type. If we want to display the value of an integer type we should use %d or %i format specifier.
To display the value of float variable, we have to use %f format specifier inside the double quote. Note that we always use format specifiers inside double quotes.
%lf is used for double floating point ( floating point number with higher precision. )
%c is used to display character. When we talk about any value of the variable, this value is called literal. E.g. char ch=’r’; ( r is a character literal)
%s is format specifier of string. We have to use it as an array of characters. In the above example, when we passed variable named ‘string’ in printf it is actually passing starting address of an array. Means it is the pointer to the first memory location of an array.