Data types in C Programming  language

A data type defines the kind of data a variable can hold in programming, such as integers, characters, or decimals. It determines memory allocation and the operations possible on the data. Common data types include integers, floats, characters, and custom structures, ensuring efficient data handling and meaningful program behavior.


1. Primitive Data Types:

int: Represents integers (whole numbers). It usually takes 4 bytes, allowing for a range of -2,147,483,648 to 2,147,483,647. Variants include:

short int: Smaller range, typically 2 bytes.

long int: Larger range, often 4 or 8 bytes.

unsigned int: Only positive values, doubling the positive range.

int num = 10;
short int smallNum = 5;
long int bigNum = 100000;
unsigned int posNum = 20;


float: Stores single-precision decimal values, generally 4 bytes, useful for less precise calculations (e.g., 3.14).

float pi = 3.14;

double: A double-precision decimal type, typically 8 bytes, providing more accuracy than float (e.g., 3.14159265359).

double precisePi = 3.14159265359;

char: Holds a single character or ASCII value. Usually 1 byte, with variations:

signed char: Range from -128 to 127.

unsigned char: Range from 0 to 255.

char letter = ‘A’;
signed char signedLetter = -64;
unsigned char unsignedLetter = 200;



2. Derived Data Types:

Array: A collection of values of the same type, e.g., int arr[5];. Arrays can be one-dimensional or multi-dimensional.

int arr[5] = {1, 2, 3, 4, 5};
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

Pointer: Stores the address of a variable rather than its value, allowing for dynamic memory management and complex data structures.

int num = 10;
int *ptr = #  // Pointer to the variable num

Structure (struct): Combines variables of different types into a single unit, like a record, e.g., struct Person { char name[50]; int age; };.

struct Person {
    char name[50];
    int age;
    float height;
};

struct Person person1 = {“Alice”, 30, 5.5};

Union: Similar to a structure but all members share the same memory space, so only one member can store a value at a time, which saves memory.

union Data {
    int i;
    float f;
    char str[20];
};

union Data data;
data.i = 10;


3. Enumeration (enum):

Defines named integer constants to improve code readability, e.g., enum Day { MON, TUE, WED };. By default, each name corresponds to an integer, starting from 0.


4. Void:

void: Represents “no value” and is commonly used in:

Function return types, indicating no return value (void display();).

Functions with no parameters (void func(void);).

Void Pointers: A generic pointer type that can hold addresses of any data type, useful for memory allocation.

void display() {
    printf(“Hello, World!”);
}

int main() {
    display();
    return 0;
}

This overview includes code examples alongside explanations to make the functionality of each data type clear. Let me know if there’s anything you’d like to elaborate on!

The article above is rendered by integrating outputs of 1 HUMAN AGENT & 3 AI AGENTS, an amalgamation of HGI and AI to serve technology education globally.

(Article By : Himanshu N)