Learn int, char, float, double basic data types, how to declare the basic data types, how to use the data types in your c program for different projects and applications.
Learn firmware development theory, applications, get practical skills from online tests, projects and earn certificate for software jobs
Learn to develop software solutions for linux environment, implement requirements through real time projects and get required practical skills for software jobs
A variable occupies some space in memory. The data type that is assigned to a variable determines how many bytes of space it will occupy in memory, the range of values that can be stored within that memory space and the set of operations that can be applied to the variable.
For example int salary statement defines a variable salary. The data type int defines that it will take(2bytes or 4 bytes) of space in memory. Storing data in a variable is called intializing the variable and data stored in a variable can be printed using its name.
Data types in C can be categorized as basic data types and derived data types. Lets dicuss all the basic data types in this article.
A char data type can be declared by writing char prior to the char variable name. Examples of char variables are all numeric characters A to Z or a to z. char type is 8 bit signed or unsigned number. If nothing is mentioned before the variable name then it is counted as signed char variable.
If a character variable is to be declared as unsigned you need to write unsigned before char during declaration of the variable. Unsigned char can hold values from 0 to 255 and signed char can hold values from -128 to 127. To print a character %c notation is used to indicate just where in a line the character is to be printed. %c is called a format specifier.
An integer is a number with no fractional part. In C, an integer is never written with a decimal point. Examples are 2, –23, and 2456. Numbers such as 3.14, 0.22, and 2.000 are not integers. Integers are stored as binary numbers. The integer 7, for example, is written 111 in binary. Therefore, to store this number in an 8-bit byte, just set the first(MSB) 5 bits to 0 and the last(LSB) 3 bits to 1.
The int type is a signed or unsigned integer. If nothing is mentioned before the variable name then it is counted as signed int variable. If an integer variable is to be declared as unsigned you need to write unsigned before int during declaration of the variable. When an integer is declared as unsigned its value during initialization has to be appended by letter U.
An integer can be positive, negative, or zero. The range in possible values depends on the computer system. For some computers size of integer is 16bit(2 bytes) where as for some computers size of integer is 32bit(4 bytes). To print an integer %d notation is used to indicate just where in a line the integer is to be printed. %d is called a format specifier.
The type short int or, more briefly, short may use less storage than int , thus saving space when only small numbers are needed. Like int, short is a signed type. So a short integer can be declared like short int val or short val. A short integer value can be printed using percentage hd or hi. The type long int, or long, may use more storage than int, thus enabling you to express larger integer values. Like int, long is a signed type. %li is used to print signed long integers and %lu for unsigned long integers.
The type long long int, or long long, may use more storage than long int, thus enabling you to express larger integer values. Like int, long long is a signed type. %lli is used to print signed long long integers and %llu for unsigned long long integers.
Financial and mathematically oriented programs often make use of floating-point numbers. In C, such numbers are called type float, double, or long double. Data with decimal places are written by using floating point. Some floating-point numbers are 9.57, 3.16E7, 7.00, and 2e–8. Notice that adding a decimal point makes a value a floating-point value. So 7 is an integer type but 7.00 is a floating-point type. The notation 3.16E7 means to multiply 3.16 by 10 to the 7th power, that is, by 1 followed by 7 zeros. The 7 would be termed the exponent of 10. A variable of float data type is printed using %f format specifier.
Double has 64 bit precision for floating point number (1 bit for the sign, 11 bits for the exponent and 52 bits for the value) i.e. double has 15 decimal digits of precision. Float has 32 bit precision for floating point number ( 1-bit for the sign, 8 bits for exponent, 23 bits for the value) i.e. float has 6 decimal digits of precision.
|Data Type||Memory (bits)||Range||Format Specifier|
|short int||16||-32,768 to 32,767||%hd|
|unsigned short int||16||0 to 65,535||%hu|
|unsigned int||32||0 to 4,294,967,295||%u|
|int||32||-2,147,483,648 to 2,147,483,647||%d|
|long int||64||-2,147,483,648 to 2,147,483,647||%ld|
|unsigned long int||64||0 to 4,294,967,295||%lu|
|long long int||64||-(2^63) to (2^63)-1||%lld|
|unsigned long long int||64||0 to 18,446,744,073,709,551,615||%llu|
|signed char||8||-128 to 127||%c|
|unsigned char||8||0 to 255||%c|