C Data Types


In C programming, data types are used to define the kind of data a variable can hold. Every variable in C has a data type, which tells the compiler how much memory to allocate and what operations can be performed on the data. Understanding data types is essential for writing efficient, reliable, and error-free programs.

This chapter explains the different data types in C, their sizes, usage, and practical examples.

What is a Data Type?

A data type is an attribute of a variable that determines:

  1. The type of value the variable can store (e.g., integer, floating-point, character).

  2. The amount of memory allocated to store the variable.

  3. The operations that can be performed on the variable.

For example, an int can store whole numbers, while a float stores decimal numbers. You cannot perform arithmetic with a character in the same way as with an integer.

Categories of Data Types in C

C data types can be broadly categorized into four groups:

  1. Basic (Primitive) Data Types

  2. Derived Data Types

  3. Enumeration Data Types

  4. Void Data Type

Let’s look at each category in detail.

1. Basic Data Types

These are the fundamental data types used to store simple values.

Data Type Description Size (bytes) Example
int Integer numbers 2–4 int age = 25;
float Single-precision decimals 4 float price = 99.5;
double Double-precision decimals 8 double pi = 3.14159;
char Single characters 1 char grade = 'A';

Example of using basic data types:

#include <stdio.h>

int main() {
    int age = 20;
    float height = 5.8;
    char grade = 'B';
    double pi = 3.14159;

    printf("Age: %d\n", age);
    printf("Height: %.2f\n", height);
    printf("Grade: %c\n", grade);
    printf("Pi: %.5lf\n", pi);

    return 0;
}

2. Derived Data Types

Derived data types are based on the basic data types but allow more complex structures:

  1. Arrays: Collection of elements of the same data type.

    int numbers[5] = {1, 2, 3, 4, 5};
    
  2. Pointers: Store the memory address of a variable.

    int a = 10;
    int *ptr = &a;
    
  3. Structures (struct): Group of variables of different types under a single name.

    struct Student {
        int id;
        char name[20];
        float marks;
    };
    
  4. Unions: Similar to structures but share the same memory location for all members.

    union Data {
        int i;
        float f;
        char c;
    };
    

3. Enumeration Data Type

Enumeration (enum) allows you to define a set of named integer constants. It improves code readability by replacing numeric values with meaningful names.

Example:

enum Days {Monday, Tuesday, Wednesday, Thursday, Friday};
enum Days today = Wednesday;

printf("Today is %d\n", today);

Here, Monday is 0, Tuesday is 1, and so on.

4. Void Data Type

The void data type represents the absence of value. It is commonly used for:

  1. Functions that do not return any value.

    void greet() {
        printf("Hello World\n");
    }
    
  2. Pointers that do not have a specific type.

    void *ptr;
    

Size of Data Types

The size of a data type may vary depending on the system and compiler. A general guideline:

Data Type Typical Size (bytes)
char 1
int 2–4
float 4
double 8
void 0

Knowing the size is important for memory management, especially in programs with large data.

Type Qualifiers

C also provides type qualifiers to modify the basic data types:

  1. signed / unsigned:

    • Signed variables can store both positive and negative numbers.

    • Unsigned variables store only positive numbers.

    unsigned int age = 25;
    
  2. short / long:

    • Short reduces memory usage for small integers.

    • Long increases the range of numbers.

    long int population = 1000000;
    short int day = 31;
    

Example Program Using Multiple Data Types

#include <stdio.h>

int main() {
    int age = 25;
    float price = 49.99;
    double pi = 3.141592;
    char grade = 'A';
    unsigned int visitors = 100;

    printf("Age: %d\n", age);
    printf("Price: %.2f\n", price);
    printf("Pi: %.6lf\n", pi);
    printf("Grade: %c\n", grade);
    printf("Visitors: %u\n", visitors);

    return 0;
}

Output:

Age: 25
Price: 49.99
Pi: 3.141592
Grade: A
Visitors: 100

This program demonstrates multiple basic and qualified data types in action.

Best Practices

  1. Choose appropriate data types:
    Use the smallest data type sufficient for the purpose to save memory.

  2. Use type qualifiers wisely:
    Unsigned types are useful for values that cannot be negative. Long types are useful for large numbers.

  3. Comment complex declarations:
    For arrays, structures, or pointers, add comments to explain their purpose.

  4. Be consistent:
    Maintain consistency in variable types across your program for easier maintenance.

Summary of the Tutorial

Data types in C define the type and size of variables and determine what operations can be performed on them. Understanding basic, derived, enumeration, and void data types, along with type qualifiers, is crucial for effective programming. Proper use of data types helps in memory management, error prevention, and program readability. Mastering data types sets the foundation for working with constants, operators, and advanced programming concepts in C.


Practice Questions

  1. What is a data type in C, and why is it important?

  2. List the basic data types in C and give an example of each.

  3. Explain the difference between float and double with an example.

  4. What is an enumeration (enum) in C, and how is it used?

  5. Identify the error in the following code:

    unsigned int age = -5;
    
  6. Write a declaration for a long int variable named population and assign it a value of 1,000,000.

  7. Explain the difference between signed and unsigned integers with an example.

  8. Write a small C program using int, float, char, and unsigned int variables and print their values.

  9. What is the purpose of the void data type in C?

  10. Describe a situation where using a short int would be better than using a regular int.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top