Data Type and Classification of Data Type with Examples

Data Types in C

Data types define the type of data a variable can store in a C program. They help allocate the appropriate memory size for variables and ensure proper data handling.


Classification of Data Types

C provides several data types, broadly classified into the following categories:


1. Primary Data Types (Basic Data Types)

These are the fundamental data types used to declare variables.

Data Type Description Example
int Stores integers (whole numbers). int a = 10;
float Stores floating-point (decimal) numbers. float b = 3.14;
double Stores double-precision floating-point numbers (more precision than float). double c = 3.14159;
char Stores single characters. char d = 'A';
void Represents no value (used for functions that return nothing). void myFunction();

2. Derived Data Types

These are data types derived from primary types to handle more complex data.

Type Description Example
Array Collection of variables of the same type. int arr[5] = {1,2,3,4,5};
Pointer Stores memory addresses of variables. int *ptr; ptr = &a;
Structure Combines variables of different types. struct Point { int x; int y; };
Union Combines variables of different types but shares memory. union Data { int i; float f; };
Function Block of code that performs a specific task. int add(int a, int b);

3. Enumeration Data Type

Used to define a set of named integer constants.

Type Description Example
enum Represents a user-defined type with constants. enum Color {Red, Green, Blue};

4. User-Defined Data Types

Users can create their own data types.

Type Description Example
typedef Used to create an alias for an existing type. typedef unsigned int uint;
struct Combines multiple types into a single unit. struct Point { int x, y; };

5. Data Modifiers

C provides data type modifiers to alter the size and range of data types.

Modifier Description Example
short Reduces the size of an int. short int a;
long Increases the size of an int or double. long int b;
unsigned Only positive values allowed (extends range). unsigned int c;
signed Both positive and negative values. signed char d;

Examples of Data Types

 

#include <stdio.h>

int main() {
// Primary Data Types
int age = 25;
float height = 5.9;
char grade = ‘A’;

// Derived Data Types
int scores[3] = {95, 85, 90}; // Array
struct Point { int x, y; }; // Structure
struct Point p = {10, 20};

// Enumeration Data Type
enum Color {Red, Green, Blue};
enum Color myColor = Green;

// Data Modifiers
unsigned int distance = 150;

// Print the variables
printf(“Age: %d\n”, age);
printf(“Height: %.2f\n”, height);
printf(“Grade: %c\n”, grade);
printf(“Scores: %d, %d, %d\n”, scores[0], scores[1], scores[2]);
printf(“Point: (%d, %d)\n”, p.x, p.y);
printf(“Color: %d\n”, myColor); // Outputs 1 for Green
printf(“Distance: %u\n”, distance);

return 0;
}

 

Summary of Data Types

Category Examples Usage
Primary int, float, char Store basic data like numbers and text.
Derived Arrays, Pointers, Structs Handle complex data structures.
Enumeration enum Manage named constants.
User-Defined typedef, struct Create custom data types.
Modifiers short, long, unsigned Adjust size/range of primary types.

By understanding these classifications, you can use the appropriate data types to optimize your C programs!

Leave a Comment

Your email address will not be published. Required fields are marked *