Data Types in C
C Data Type specifies the type of value that a variable can store. C is a strongly typed language, a variable can store only that type of value that was defined during the declaration of the variable.
Note: If a variable is declared int type then it will store only int type value.
- We use variables to reserve memory blocks/locations in the computer's memory.
- We store value in those reserved memory blocks/locations.
- The value can be different types as well as different sizes.
- Data Types define the type of value that can be stored in a reserve block/location.
- The size of reserve space also depends on Data Type, for example, char reserves 1 byte and int reserves 2-byte space.
Types of Data Types in C
Based on the type of value and size Data Types in C are categorized into 3 broad categories.
- Primary Data Types or Fundamental Data Types.
- Derived Data Types.
- User-Defined Data Types.
Let’s understand, what are these three categories of C data types? the scenario where we can use them in our C programs.
1. Primary / Fundamental Data Types in C
There are 5 fundamental data types in the C language.
- Integer (int)
- Character (char)
- Floating point (float)
- Double precision floating point (double)
- Void (void)
Note: In the below list we have listed all primary data types with their value type, size, and value range.
- int: Integer, 4 Bytes, -32768 to 32767
- char: Character, 1 byte, -128 to 127
- float: Floating Point, 4 byte, 3.4e-38 to 3.4e+e39
- double: double precision floating point, 8 bytes, 1.7e – 308 to 1.7e+308
- void: Void, none, …
Note: Fundamental data types also offer extended data types for example long int, short int, long double, etc. Complete knowledge about extended data types is very important for the best utilization of storage memory.
a. Integer Type (int)
- In C programming, Integers are used to represent whole numbers within a fixed range of values.
- Commonly inter reserve one word of storage.
- Word size can vary from machine to machine, for example, some computers use 16-bit size to store a word and some use 32-bit size to store a word.
- If we use 16-bit word length then the value range will be -32768 to +32767
- If we use 32 bit word length then the value range will be -2,147,483,648 to +2,147,483,647
Use of int data type -
int salary=25000;
int age=27;
int result;
You can declare and also assign the value of the variable together or you can declare the variable first and assign the value later.
Integer type can be extended in 6 types, listed below in the list
- int
- short int
- long int
- unsigned int
- unsigned short int
- unsigned long int
b. Character (char)
Characters (char) are used to define a single character.
- To store a character requires 8 bits of storage space.
- There are two extended forms of char data type that are signed char and unsigned char.
- unsigned char have values between 0 and 255.
- signed char have values between -128 to 127.
Use of char Data Type:-
char istrue="y";
char isfalse;
char result="y";
c. Floating Point (float)
Floating points ( float ) are used to define real numbers and stored in 32 bits with 6 digits of precision in both kinds of 16 bit and 32-bit machines. Keyword float is used to define floating-point numbers in C programming.
- The floating-point number is not a complete number for example 3.4, 453.453, .000372, etc.
- To define a complete number we use int data type.
- To define incomplete numbers we use float data type.
Use of float data type:-
float result=3.97;
float height, average;
average=22.5;
height=174.38;
d. Double
Data type double is also used to define floating-point numbers. In such a condition where the 6 digits precision of float is not sufficient then we can double it to store the real number in 64 bits with 14 digits of precision.
- If double data type precision is not enough then we can use a long double data type.
- the long double data type is an extended data type of double
- The long double data type is used to define real numbers and stored in 80 bits with 19 digits of precision.
- We use long double data types where we need very high accuracy in value.
Use of double data type:-
double value=45.55723895485;
float range;
range=0.0032147562;
e. Void (void)
The void data type has no values. Basically void is used to define the type of function. If we are using a function type of void then that function will not return anything.
Use of void data type:
void main(){}
void calculate(){}
Data Type, Size, and Range Value
- unsigned int [16 bits, 0 to 65535]
- short int or signed short int [8 bits, -128 to 127]
- unsigned short int [8 bits, 0 to 255]
- long int or signed long int [32 bits, -2,147,483,648 to 2,147,483,647]
- unsigned long int[32 bits, 0 to 4,294,967,295]
- char or signed char [8 bits, -128 to 127]
- unsigned char [8 bits, 0 to 255]
- float [32 bits, 3.4E-38 to 3.4E+38]
- double [64 bits, 1.7E-308 to 1.7E+308]
- long double [80 bits, 3.4E-4932 to 1.1E+4932]
2. Derived Data Types
The data type which is derived from the fundamental data type is called the derived data type. There are three types of derived data types.
Note: You will learn all the above-derived data types in very detail in upcoming chapters.
3. User-Defined Data Types
The data type which is defined by the user is called a user-defined datatype for example structure, union, and enumeration.
C Language has a special feature “ type definition “ which allows users to define an identifier that can represent an existing data type. This user-defined identifier later can be used to declare variables. To define a user-defined data type, see the below examples.
Syntax & Example
Syntax: typedef type identifier;
Example: typedef int counts;
In C programming, structure, union, and enumeration are widely used to create user-defined data types. See how to define user-defined data types using struct, union, and enum keywords.
Structure Example
struct student
{
char name[50];
int roll_number;
float marks;
}
See the syntax and example of defining enum
Syntax & Example
Syntax: enum identifier { value1, value2,...valuen};
Example: enum month { jan, feb,...., dec};
Note: Later we will discuss all user-defined data types in-depth and also we will learn the best use of user-defined data types