A programming language should deal with different types of data. The type of data can be a Whole number, Real number or Character string. C language comes with predefined data types like int, long, float, double and char. We use the word define when we are initializing or assigning a constant value to a variable.
Different Types of Buckets hold different types of Data. A bucket can be thought of like a memory location.
C Primitive Data Types Infographic
Primitive data types are predefined data types in C language. Take a look at the Infographic. Save and Share it if you want.
C Data Types
Basic C data types are as follows.
- int - Integer data type
- short - Integer data type smaller than int
- long - Integer data type bigger than int
- float - Real number data type
- double - Real number data type bigger than float
- long double - Real number data type bigger than double
- char - Character data type
1) C Int Data Type
Integers are whole numbers without any decimal points. Size of an int variable is 4 bytes maximum in a 32 bit compiler. In 16 bit old compilers, size of int is only 2 bytes. 'u' is appended to define unsigned int variable. To represent signed variables, keyword 'signed' is not required. You can use if you want. Compiler will not through error.
int range
Range of signed int variable is from -2147483648 to 2147483647.
Range of unsigned int variable is from 0 to 4294967295.
10, 35, 876, 001 are int type.
0.5, 10.7, 22.5, 123.456789 are not int type. These are float or double type.
Declaring an Int Variable
int k;
Defining an int Variable
int k=10; int p; p=25; unsigned int a= 334u;
Format Specifier for Reading using Scanf
To read or scan signed int values, format Specifier '%d' is used.
To scan unsigned in values, format specifier '%u' is used.
Format Specifier for Printing using Printf
To print signed int values, format specifier '%d' is used.
To print unsigned int values, format specifier '%u' is used.
2) C Short Data Type
Size of a short variable is 2 bytes which is half the size of an int variable.
short range
Range of signed short variable is from -32768 to 32767.
Range of unsigned short variable is from 0 to 65535.
Declaring and Defining Short Variable
short pp = 123; short qq; qq = 456;
Format Specifier for Reading using Scanf
To read or scan signed short values, format Specifier '%d' is used.
To scan unsigned short values, format specifier '%u' is used.
Format Specifier for Printing using Printf
To print signed short values, format specifier '%d' is used.
To print unsigned short values, format specifier '%u' is used.
3) C Long Data Type
Size of a long variable is 4 bytes. Letter 'l' or 'L' is used to define signed long variable. Letters 'lu' or 'ul' is used to define unsigned long variable.
long range
Range of signed long variable is from -2147483648 to 2147483647.
Range of unsigned long variable is from 0 to 4294967295.
Declaring and Defining long variable
long ab = 45L; unsigned long p = 5678912ul;
Format Specifier for Reading using Scanf
To read or scan signed long values, format Specifier '%ld' is used.
To scan unsigned long values, format specifier '%lu' is used.
Format Specifier for Printing using Printf
To print signed long values, format specifier '%ld' is used.
To print unsigned long values, format specifier '%lu' is used.
4) C Float Data Type
Float numbers are simply real numbers with decimal points. You can append letter 'f' to define a floating point constant. Size of a float variable is 4 bytes.
3.1415 is a double data type constant.
3.1415f is a float data type constant. Use an explicit 'f' letter.
Declaring and Defining float variable
float ab = 3.14f; float p = (float)5678.1234; float q; q = 45.678f;
float range
Range of floating point variable can be from -3.4e38 to +3.4e38.
Format Specifier for Reading using Scanf
To read or scan float values, format Specifier '%f' is used.
Format Specifier for Printing using Printf
To print float values, format specifier '%f' is used.
5) C Double Data Type
C double data type is two times the size of a float variable. It can hold a real number with 8 bytes memory. Any real number without any appending letter is a double data type constant.
3.14 is a double data type constant.
Declaring and Defining double variable
double score= 98.12345678;
double range
Range of double variable can be from -1.7e308 to +1.7e308.
Format Specifier for Reading using Scanf
To read or scan double values, format Specifier '%lf' is used.
Format Specifier for Printing using Printf
To print double values, format specifier '%lf' is used.
6) C Long Double Data Type
C long double data type is two times the size of a double variable. It can hold a real number with 10 bytes memory. Any real number with an appending letter 'l' or 'L' is a long double constant.
3.14L is a long double data type constant.
Declaring and Defining double variable
long double score= 98.12345678L;
long double range
Range of long double variable can be from -1.7e4932 to +1.7e4932.
Format Specifier for Reading using Scanf
To read or scan long double values, format Specifier '%Lf' is used.
Format Specifier for Printing using Printf
To print long double values, format specifier '%Lf' is used.
7) C Char Data Type
C char data type is used to represent or store letters and symbols. Its size is only 1 byte.
ASCII - American Standard Code for Information Interchange defined integer codes for each letter, number and symbol.
Declaring and Defining char variable
char group= 'M';
char range
Range of signed char variable can be from -128 to + 127.
Range of unsigned char variable can be from 0 to + 255.
Format Specifier for Reading using Scanf
To read or scan character values, format Specifier '%c' is used.
Format Specifier for Printing using Printf
To print character values, format specifier '%c' is used.
Online Tests Available:
1 | C Data Types and Storage Classes Online Test 1 |
2 | C Data Types and Storage Classes Online Test 2 |
3 | C Data Types and Storage Classes Online Test 3 |
In next chapter we shall deal with Storage Classes. Storage classes decide where in memory a variable is stored.