Data Types in C Programming: Overview and Examples
Table of Content:
A datatype is a special keyword used to allocate sufficient memory space for the data, in other words, Data type is used for representing the data in main memory (RAM) of the computer. Data types represent the different values to be stored in the variable. Every data type has a range of values. The compiler allocates memory space for each variable or constant according to its data type.
Each variable in C has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. Let us briefly describe them one by one:
Datatypes in C Programming
Data types in C
Primary data types
- Integer Types
- Floating-point Types
- Character Types
Enumerated types
- enum
Derived data types
- Arrays
- Pointer
- Structure
- Union
void
- void
| Types | Data Types |
|---|---|
| Basic Data Type | int, char, float, double |
| Derived Data Type | array, pointer, structure, union |
| Enumeration Data Type | enum |
| Void Data Type | void |
Following are the examples of some very common data types used in C:
char: The most basic data type in C. It stores a single character and requires
a single byte of memory in almost all compilers.
int: As the name suggests,
an int variable is used to store an integer.
float: It is used to store
decimal numbers (numbers with floating point value) with single precision.
double: It
is used to store decimal numbers (numbers with floating point value) with double precision.
Primary data types
Integer Types
- short
- int
- long
Floating-point Types
- float
- double
Character Types
- char
Primary Datatype or Basic Datatype
The memory size of basic data types may change according to 32 or 64 bit operating system. Let's see the basic data types. Its size is given according to 32 bit architecture.
Integer Type Datatype
| Data Types | Memory Size | Range |
| int | 2 byte | ?32,768 to 32,767 |
| signed int | 2 byte | ?32,768 to 32,767 |
| unsigned int | 2 byte | 0 to 65,535 |
| short int | 2 byte | ?32,768 to 32,767 |
| signed short int | 2 byte | ?32,768 to 32,767 |
| unsigned short int | 2 byte | 0 to 65,535 |
| long int | 4 byte | -2,147,483,648 to 2,147,483,647 |
| signed long int | 4 byte | -2,147,483,648 to 2,147,483,647 |
| unsigned long int | 4 byte | 0 to 4,294,967,295 |
Floating-points Datatypes
| Type | Storage size | Value 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 |
Character Types Datatypes
| Data Types | Memory Size | Range |
|---|---|---|
| char | 1 byte | ?128 to 127 |
| signed char | 1 byte | ?128 to 127 |
| unsigned char | 1 byte | 0 to 255 |
sizeof(DataType) Operator
To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator.
To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator.
// Run on 64 bit Machine #includeint main() { printf("Storage size for int : %d \n", sizeof(int)); return 0; }
Storage size for int : 4 Press any key to continue . . .
Calculate Range
// Run on 64 bit Machine #include#include int main() { printf("Storage size for float : %d \n", sizeof(float)); printf("Minimum float positive value: %E\n", FLT_MIN ); printf("Maximum float positive value: %E\n", FLT_MAX ); printf("Precision value: %d\n", FLT_DIG ); return 0; }
Storage size for float : 4 Minimum float positive value: 1.175494E-038 Maximum float positive value: 3.402823E+038 Precision value: 6 Press any key to continue . . .
- Question 1: How a negative integer is stored.
- Question 2: What are the different data types in C?
- Question 3: What is data type in C?
- Question 4: What is the difference between int, char, float and double data types?
- Question 5: What is modifier in C?
- Question 6: What are different types of modifiers in C?
- Question 7: What is typecasting?
Related Questions
- Assignment 1: A program to print one line of text using C Programming Language
- Assignment 2: Program to add two numbers
- Assignment 3: Program to find sum of two numbers using c programming language.
- Assignment 4: Integer type variable in c programming language
- Assignment 5: character type variable in c programming language
- Assignment 6: Floating point type variable in c programming language
- Assignment 7: double type variable in c programming language
- Assignment 8: C program int datatype
- Assignment 9: C program float datatype
- Assignment 10: C program char datatype
- Assignment 11: C program unsigned char datatype
- Assignment 12: C program signed char datatype
- Assignment 13: C program unsigned int datatype
- Assignment 14: C program short datatype
- Assignment 15: C program unsigned short datatype
- Assignment 16: C program long datatype
- Assignment 17: C program unsigned long datatype
- Assignment 18: C program double datatype
- Assignment 19: unsigned character type variable in c programming language
- Assignment 20: signed character type variable in c programming language
- Assignment 21: short type variable in c programming language
- Assignment 22: unsigned short type variable in c programming language
- Assignment 23: long type variable in c programming language
- Assignment 24: unsigned long type variable in c programming language
- Assignment 25: signed long type variable in c programming language
- Assignment 26: signed short type variable in c programming language
- Assignment 27: long double type variable in c programming language
- Assignment 28: C program to find that entered year is leap year or not
- Assignment 29: Write a program for printing prime numbers between 1 and 100.
- Assignment 30: Program to find angles of a triangle
- Assignment 31: C program to perform input output of all basic data types