Data Types in C Programming

In C programming, data types define the type and size of data that can be stored in a variable. Every variable in C must be declared with a specific data type to inform the compiler about the size and type of data the variable will store. This helps the compiler allocate appropriate memory for the variable and allows the programmer to apply only valid operations to it.

C provides several basic, derived, and user-defined data types to work with.

1. Basic Data Types

These are the fundamental data types provided by C. They include integer types, floating-point types, and character types.

1.1 Integer Types (int)

The int data type is used to store whole numbers (i.e., numbers without fractional components). Depending on the system architecture, the size of an int typically ranges from 2 to 4 bytes, allowing the storage of values within a specific range.

Types of Integers:

TypeSizeRange
int2 or 4 bytes-32,768 to 32,767 (2 bytes) or -2,147,483,648 to 2,147,483,647 (4 bytes)
short int2 bytes-32,768 to 32,767
long int4 or 8 bytes-2,147,483,648 to 2,147,483,647 (4 bytes) or larger for 8 bytes
unsigned int2 or 4 bytes0 to 65,535 (2 bytes) or 0 to 4,294,967,295 (4 bytes)
unsigned short int2 bytes0 to 65,535
unsigned long int4 or 8 bytes0 to 4,294,967,295 (4 bytes) or larger for 8 bytes

Example:

int a = 10;          // Standard integer

unsigned int b = 20; // Unsigned integer

long int c = 100000; // Long integer

1.2 Floating-Point Types (float, double)

These types are used to store real numbers (i.e., numbers with fractional components).

TypeSizePrecisionRange
float4 bytes6 decimal places3.4E-38 to 3.4E+38
double8 bytes15 decimal places1.7E-308 to 1.7E+308
long double10 bytes or more19 decimal places3.4E-4932 to 1.1E+4932

Example:

float pi = 3.14;          // Single-precision floating-point

double large_pi = 3.1415926535; // Double-precision floating-point

long double precise_pi = 3.14159265358979323846; // Long double precision

1.3 Character Type (char)

The char data type is used to store single characters. A char in C is usually 1 byte, capable of storing a single character from the ASCII character set.

TypeSizeRange
char1 byte-128 to 127 or 0 to 255 (ASCII values)

Example:

char letter = ‘A’;  // Single character

char digit = ‘5’;   // Character representing a number

2. Derived Data Types

Derived data types are types that are derived from basic data types. These include arrays, pointers, structures, and unions.

2.1 Arrays

An array is a collection of elements of the same data type stored in contiguous memory locations. Each element in the array can be accessed using an index.

Syntax:

data_type array_name[array_size];

Example:

int numbers[5] = {1, 2, 3, 4, 5};  // Array of integers

float marks[3] = {95.5, 88.0, 76.5};  // Array of floats

2.2 Pointers

A pointer is a variable that stores the memory address of another variable. Pointers are heavily used in dynamic memory allocation and are critical for working with arrays, functions, and structures in C.

Syntax:

data_type *pointer_name;

Example:

int num = 10;

int *ptr = #  // Pointer to integer, stores the address of ‘num’

2.3 Structures

A structure is a user-defined data type that allows grouping variables of different types under a single name. It is used to represent a record.

Syntax:

struct structure_name {

    data_type member1;

    data_type member2;

    // More members

};

Example:

struct Person {

    char name[50];

    int age;

    float height;

};

struct Person person1 = {“John Doe”, 30, 5.9};  // Initialize structure

2.4 Unions

A union is similar to a structure, but in a union, all members share the same memory location. This means that only one member can hold a value at any given time.

Syntax:

union union_name {

    data_type member1;

    data_type member2;

    // More members

};

Example:

union Data {

    int i;

    float f;

    char str[20];

};

union Data data;

data.i = 10;  // Only one member can hold a value at a time

3. User-Defined Data Types

These are data types defined by the user using the typedef and enum keywords. C allows users to create their own data types for better code readability and maintainability.

3.1 typedef

The typedef keyword allows you to create a new name (alias) for an existing data type.

Syntax:

typedef existing_data_type new_name;

Example:

typedef unsigned int uint;  // Define a new name ‘uint’ for ‘unsigned int’

uint age = 25;

3.2 Enumerations (enum)

Enumerations define a set of named integer constants that increase by 1 starting from 0 by default. Enumerations are often used to define a collection of related constants for easy handling.

Syntax:

enum enum_name {constant1, constant2, constant3, …};

Example:

enum Weekday {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

enum Weekday today = Wednesday;  // Assign enum value

  • In this example, Sunday has a value of 0, Monday has a value of 1, and so on.

4. Void Data Type

The void type is used to declare functions that do not return any value or to declare pointers that point to an unspecified type.

Void Function:

A function that does not return a value is declared as void.

Syntax:

void function_name() {

    // Function code

}

Example:

void printMessage() {

    printf(“Hello, World!\n”);

}

Void Pointer:

A void pointer is a generic pointer that can point to any data type. However, you cannot dereference a void pointer without casting it to another pointer type.

Syntax:

void *pointer_name;

Example:

int a = 10;

void *ptr = &a;  // Void pointer

5. Size of Data Types

The size of each data type can vary depending on the system architecture (e.g., 32-bit vs. 64-bit systems). However, typical sizes for most systems are:

Data TypeSize (Bytes)
char1 byte
int4 bytes
float4 bytes
double8 bytes
long double10 or more bytes
short int2 bytes
long int4 or 8 bytes
voidNo size

You can use the sizeof() operator to determine the exact size of a data type on your system.

Example:

#include <stdio.h>

int main() {

    printf(“Size of int: %lu bytes\n”, sizeof(int));

    printf(“Size of float: %lu bytes\n”, sizeof(float));

    return 0;

}

error: Content is protected !!
Open chat
1
Hi,how Can We Help You ?