Data type of a variable or constant indicates that what type of data will be stored in the allocated memory space .
Types of Data Types
Primary Data Types
Secondary Data Types
User Defined Data types
Primary Data Types
In C, primary (or fundamental) data types are the most basic built-in types provided by the language. These serve as the foundation for declaring variables and building more complex data types. It is also known as basic data types and primitive data types. These are :-
int: Stores whole numbers (integers) without decimal points.
int age = 30;
char: Stores a single character. Characters are enclosed in single quotes.
char grade = 'A';
float: Stores single-precision floating-point numbers (numbers with decimal points).
float price = 19.99;
double: Stores double-precision floating-point numbers, offering more precision than float.
double pi = 3.1415926535;
Secondary Data Types
Secondary data types are also known as derived data types that are constructed or defined using one or more fundamental (or primitive) data types, or even other derived data types.
For example:
Arrays: Store a collection of elements of the same data type.
int numbers[5] = {1, 2, 3, 4, 5};
Pointers: Store memory addresses of other variables.
int value = 10;
int *ptr = &value; // ptr stores the address of 'value'
Structures (struct): Allow grouping of different data types under a single name.
struct Person {
char name[50];
int age;
};
struct Person p1;
Unions (union): Allow different data types to share the same memory location.
union Data {
int i;
float f;
};
union Data d1;
User-defined Data Types
User-defined data types in C are custom data types created by the programmer to organize and manage data more effectively than with built-in data types alone.
For Example:
Enumerations (enum): Define a set of named integer constants.
enum Day {MONDAY, TUESDAY, WEDNESDAY};
enum Day today = MONDAY;