C Programs Tutorials | IT Developer
IT Developer

C Programming - C Data Types



Share with a Friend

C Programming - C Data Types

C Data Types

In C programming, data types specify the type of data a variable can hold. They determine the size, range, and operations that can be performed on the variable.

Categories of Data Types

  1. Basic Data Types:
    • int, float, char, double
  2. Derived Data Types:
    • Arrays, Pointers, Structures, Unions
  3. Enumeration Data Type:
    • enum
  4. Void Data Type:
    • void
  1. Basic Data Types
Type Keyword Size (bytes) Range

Integer

int

2 or 4

-32,768 to 32,767 (2 bytes)

Float

float

4

~3.4E-38 to 3.4E+38

Character

char

1

-128 to 127 (signed)

Double

double

8

~1.7E-308 to 1.7E+308

Examples

int age = 25;

float salary = 50000.50;

char grade = 'A';

double distance = 1234567.89;

  1. Derived Data Types
  2. a) Arrays
  • Store multiple elements of the same type.

Example:

int numbers[5] = {1, 2, 3, 4, 5};

  1. b) Pointers
  • Store the address of a variable.

Example:

int x = 10;

int *ptr = &x;  // Pointer to x

  1. c) Structures
  • Group different types of data.

Example:

struct Student {

    int id;

    char name[50];

    float marks;

};

  1. d) Unions
  • Share memory among variables.

Example:

union Data {

    int i;

    float f;

};

  1. Enumeration Data Type
  • Used to define named integer constants.

Example:

enum Day { Sunday, Monday, Tuesday };

enum Day today = Monday;

  1. Void Data Type
  • Represents "no value."
  • Used in functions that do not return a value.

Example:

void displayMessage() {

    printf("Hello, World!\n");

}

Modifiers in C

Data types can be modified to change their size and range using modifiers like:

  • Signed
  • Unsigned
  • Short
  • Long

Example

Type Size (bytes) Range

signed int

4

-2,147,483,648 to 2,147,483,647

unsigned int

4

0 to 4,294,967,295

short int

2

-32,768 to 32,767

unsigned short int

2

0 to 65,535

long int

8

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

unsigned long int

8

0 to 18,446,744,073,709,551,615

Example Code: Data Types in Action

#include <stdio.h>

int main() {

    int age = 25;

    float height = 5.9;

    char grade = 'A';

    double pi = 3.14159265358979;

    unsigned int score = 4294967295;

    printf("Age: %d\n", age);

    printf("Height: %.1f\n", height);

    printf("Grade: %c\n", grade);

    printf("Value of Pi: %.15lf\n", pi);

    printf("Score: %u\n", score);

    return 0;

}

Key Points

  1. Choose Data Types Wisely:
    • Use int for general numbers, float/double for decimals, and char for characters.
  2. Memory Matters:
    • Smaller data types save memory but have smaller ranges.
  3. Type Conversion:
    • Implicit (automatic) or explicit (casting) conversions can occur between data types.