Back to C Programming
2026-07-127 min read

different types of data a variable can store

Learn different types of data a variable can store step by step with clear examples and exercises.

Why This Matters

Understanding the different types of data a variable can store is crucial for any C programmer. This knowledge helps you to write efficient, error-free code and avoid common pitfalls that can lead to runtime errors or unexpected behavior. In this lesson, we will delve into the various data types available in C programming, their uses, and how to declare and manipulate them effectively.

Prerequisites

Before diving into the details of data types in C, it's essential to have a solid understanding of the following concepts:

  1. Basics of C programming, including variables, operators, and control structures.
  2. Understanding the concept of memory allocation and how it works in C.
  3. Familiarity with basic input/output operations using functions like printf() and scanf().
  4. Knowledge of data structures such as arrays and pointers will also be beneficial for understanding more complex data types like structures and unions.

Core Concept

In C programming, data types are used to declare variables and specify their storage class, size, and properties. The following are the primary data types in C:

  1. Integer Types
  • char: 1 byte (8 bits), used for storing small integers and characters. The minimum and maximum values that can be stored in a char variable depend on the system's character encoding. On ASCII systems, the range is typically -128 to 127.
  • int: 4 bytes (32 bits) on most systems, used for general-purpose integer calculations. The minimum and maximum values that can be stored in an int variable also depend on the system. On a typical 32-bit system, the range is -2147483648 to 2147483647.
  • short: 2 bytes (16 bits), used when memory conservation is important. The minimum and maximum values that can be stored in a short variable depend on the system, but they are typically smaller than those for an int.
  • long: 4 or 8 bytes (32/64 bits), depending on the system, used for large integers. On a typical 64-bit system, the range for a long variable is -9223372036854775808 to 9223372036854775807.
  1. Floating-Point Types
  • float: 4 bytes (32 bits), used for single precision floating-point numbers. Floating-point numbers are represented using a format called IEEE 754, which allows for a certain level of precision and supports both positive and negative values.
  • double: 8 bytes (64 bits), used for double precision floating-point numbers. Double precision provides more accuracy than single precision but requires more memory.
  • long double: 10 or 12 bytes (80/106 bits), depending on the system, used for extended precision floating-point numbers. Long double provides even higher precision than double but may not be supported by all systems.
  1. Boolean Type
  • _Bool: a boolean data type introduced in C99, can only hold values of 0 (false) and 1 (true). Boolean variables are useful for representing conditional states or results of logical operations.
  1. Void Type
  • void: used as the return type for functions that do not return any value or as the type for a pointer to no object. The void keyword is also used when declaring function parameters to indicate that they do not accept any arguments.
  1. Enumerated Types
  • Enumerations are user-defined data types consisting of a set of named integer constants. They can be useful for improving readability and maintainability in large programs. For example:
enum Color { RED, GREEN, BLUE };

In this example, RED, GREEN, and BLUE are enumerated constants representing the colors red, green, and blue, respectively. The first enumeration constant is implicitly assigned the value 0, and subsequent constants are incremented by 1.

  1. Structure and Union Types
  • Structures allow you to group related variables together into a single entity, while unions provide a way to store different data types in the same memory location. Both structures and unions can be defined using the struct and union keywords, respectively.

Worked Example

Let's create a simple C program that demonstrates the use of various data types:

#include <stdio.h>

int main() {
char character = 'A';
int integer = 10;
float floating_point = 3.14f;
double double_precision = 2.71828;
_Bool boolean = 1;
enum Color color = RED;

printf("Character: %c\n", character);
printf("Integer: %d\n", integer);
printf("Floating-point: %.2f\n", floating_point);
printf("Double precision: %.6lf\n", double_precision);
printf("Boolean: %d\n", boolean);
printf("Color: %s\n", color == RED ? "RED" : (color == GREEN ? "GREEN" : "BLUE"));

return 0;
}

In this example, we declare variables of different data types and print their values using the printf() function. The output will be:

Character: A
Integer: 10
Floating-point: 3.14
Double precision: 2.718280
Boolean: 1
Color: RED

Common Mistakes

  1. Using the wrong data type for a variable: Using an inappropriate data type can lead to unexpected behavior, such as truncation of large numbers or loss of precision in floating-point calculations. Always choose the most suitable data type for your needs and consider the range and precision requirements of your program.
  1. Forgetting to include necessary header files: Header files like ` contain function declarations and macro definitions that are essential for working with various data types, such as input/output operations using printf() and scanf()`.
  1. Incorrectly handling string data: In C, strings are represented as arrays of characters terminated by a null character (\0). Common mistakes include forgetting to allocate enough memory for a string or not properly checking for the null character when reading user input.
  1. Ignoring type promotions: When performing arithmetic operations involving operands of different data types, C will automatically promote them to a common, higher-precision data type. Failing to account for these promotions can lead to unexpected results or errors.
  1. Not initializing variables: Uninitialized variables in C are given indeterminate values, which can lead to unpredictable behavior and runtime errors. It's always a good practice to initialize variables before using them.

Practice Questions

  1. Write a program that declares and initializes variables of all basic data types (integer, floating-point, and character) and prints their values using printf().
  2. Modify the previous example to read user input for each variable instead of hardcoding their values.
  3. Write a program that calculates the average of three integers entered by the user. Use appropriate data types for variables and handle any potential errors or edge cases.
  4. Create a structure called Person with fields for name, age, and gender (represented as an enumeration). Write a function to initialize a Person struct using user input and print its contents.
  5. Write a program that declares a union containing a character array and an integer variable. Demonstrate how you can switch between storing different data types in the union.
  6. Write a program that uses a structure to represent a point in 3D space with x, y, and z coordinates. Calculate the distance between two points using the Euclidean distance formula.
  7. Create an enumeration for card suits (HEARTS, DIAMONDS, CLUBS, SPADES) and another for card ranks (ACE, TWO, THREE, ..., KING). Write a program that declares a deck of cards as an array of structures containing suit and rank fields. Shuffle the deck using a simple randomization method (e.g., Fisher-Yates shuffle), then deal five cards to the player and five cards to the computer. Print the cards for both players.

FAQ

  1. Why is it important to choose the correct data type for a variable?

Using the appropriate data type ensures that your program runs efficiently, avoids unexpected behavior, and prevents errors such as truncation or loss of precision.

  1. What are some common mistakes when working with different types of data in C?

Common mistakes include using the wrong data type for a variable, forgetting to include necessary header files, incorrectly handling string data, ignoring type promotions during arithmetic operations, not initializing variables, and failing to account for endianness issues when dealing with multi-byte data types.

  1. How can I check if a user-entered value is within a specific range for an integer variable?

You can use conditional statements like if or switch to validate the input and handle errors gracefully. For example:

int age;
printf("Enter your age: ");
scanf("%d", &age);

if (age < 0 || age > 120) {
printf("Invalid age! Please enter a value between 0 and 120.\n");
} else {
// Continue with the rest of your code.
}
  1. What is endianness, and why does it matter in C programming?

Endianness refers to the order in which bytes are stored within a multi-byte data type (e.g., integer or floating-point). There are two common types of endianness: little-endian and big-endian. In little-endian systems, the least significant byte is stored first, while in big-endian systems, the most significant byte is stored first. Endianness matters because it can affect how data is interpreted when transferred between systems with different endianness or when working with network communications. To avoid issues related to endianness, you can use functions like htonl() and ntohl() for converting host-endian and network-endian integers, respectively.

  1. What are some best practices for handling strings in C?

Some best practices for handling strings in C include:

  • Allocating enough memory to store the string, including the null character (\0).
  • Checking for the null character when reading user input or processing strings.
  • Using functions like strlen() to determine the length of a string.
  • Being aware of potential buffer overflows and taking measures to prevent them, such as using safe string handling libraries or carefully validating user input.
  • Avoiding the use of C-style string concatenation (e.g., strcat()) in favor of safer alternatives like sprintf() or string literals with format specifiers (e.g., printf("Hello, %s!\n", name)).