Data Types in C
To decide how much memory is allocate for that particular variable and which operation should be performed is depend on a Data Types.
Classification Of Data Types:-
1. Basic/ Primitive/ Predefined data types
2.Derived data types
3. User defined data types
I. Basic/ Primitive/ Predefined Data Types:-
- Data types which are already declared or defined by a language designer is known as Basic/ Primitive/ Predefined data types.
- Following are the basic data types:
- integer(int)
- character(char)
- single point precision(float)
- double point precision(double)
- No value available(void)
II. Derived Data Types:-
- Derived data types do not create or declare a new data type.
- They add some functionality to the basic data types.
- Following are the derived data types.
- Array:- e.g.: char [ ], int [ ], etc.
- Pointer:- e.g.: char*, int*, etc.
- Function:- e.g.: int(int, int), float(int) etc.
III.User Defined data Types:-
- Data types which are defined or declared by a user itself is known as user defined data types.
- Following are the user defined data types:
- Structure(struct)
- Union(union)
- Enumeration(enum)
So these are various data types. Further we are going to learn more about particular data types.
Data Type Qualifier :-
- They are used to indicate the special properties of data within an object.
- There are two types:-
- const qualifier:- Declaring an object or variable const indicates that its value will not be changed throughout the program.
- volatile qualifier:- Declaring an object or variable volatile indicates that the value of the variable may change at any time throughout the program.
Data Type Modifier:-
- The type modifier modifies or change the range and the arithmetic properties of basic data type.
- There are four Data Type Modifiers:-
- signed int:- It is used to store magnitude of a variable. In case of signed integer, out of 32 bits, we can use 31 bits.
- unsigned int:- In case of unsigned modifier, user can use all 32 bits.By default, unsigned modifier is assigned by the compiler.
- short int:-short modifier modifies the size of a variable.
- long int:-long modifier modifies the size of a variable.By default, long modifier is assigned by a compiler.
example:-
signed int i; //last bit is used to store magnitude
unsigned int i; //all 32 bits we can use
short int i; //size will be modified. size of (i)=2 bytes.
long int i; //size of (i)=4 bytes
Data types and its memory storage and ranges in C
- Each data type has different memory requirement and perform specific operations accordingly.
- The memory of Data types may vary(i.e. change) from compiler to compiler.
- Also the Ranges of data types may also vary from compiler to compiler.
- Ranges defines that upto which number the data types can stored values.
So here we have listed the Data types and their memory requirement and their ranges in C language. They are as follows:-
Sr. No
|
Data Types
|
Turbo C
|
MS Visual Studio
| ||
Memory
|
Ranges
|
Memory
|
Ranges
| ||
1
|
Char
|
1 Byte
|
-128 to 127
|
1 Byte
|
-128 to 127
|
2
|
int
|
2 Bytes
|
-32768 to 32767
|
4 Bytes
|
-2147783648 to 2147483647
|
3
|
float
|
4 Bytes
|
3.4* 10-38 to 3.4*1038
|
4 Bytes
|
3.4* 10-38 to 3.4*1038
|
4
|
double
|
8 Bytes
|
1.7*10-308 to 1.7*10308
|
8 Bytes
|
1.7*10-308 to 1.7*10308
|
5
|
Signed char
|
2 Bytes
|
-128 to 127
|
1 Byte
|
-128 to 127
|
6
|
Signed int
|
1 Byte
|
-32768 to 32767
|
4 Bytes
|
-2147483648 to 2147483647
|
7
|
Unsigned char
|
1 Byte
|
0 to 255
|
1 Byte
|
0 to 255
|
8
|
Unsigned int
|
2 Byte
|
0 to 65535
|
4 Bytes
|
4294967295
|
9
|
Short int
|
2 Bytes
|
-32768 to 32767
|
2 Bytes
|
-32768 to 32767
|
10
|
Long int
|
4 Bytes
|
-
|
4 Bytes
|
-
|
11
|
Long float
|
8 Bytes
|
-
|
8 Bytes
|
-
|
12
|
Long double
|
10 Bytes
|
3.4*10-4932 to 1.1*104932
|
8 Bytes
|
1.7*10-308 to 1.7*10308
|
13
|
void
|
Object of void type cannot be created
|
Let us see an example for a size of an integer and its range.
#include <stdio.h>int main()
{
int a = 1;
printf("value is %d and size is %lu bytes \n", a,sizeof(int)); //%d indicates an integer and //%lu indicates unsigned int
return 0;
}
{
int a = 1;
printf("value is %d and size is %lu bytes \n", a,sizeof(int)); //%d indicates an integer and //%lu indicates unsigned int
return 0;
}
Good
ReplyDeleteThank you!!!
Delete