Variables and Data Types in C
Subject: Data Structures and Algorithms · Language: C · Level: beginner · 4 min read
Learn int, float, double, char, constants, variable declaration, initialization, and memory size.
When you write a C program, you often need to remember some values: a student's age, marks, grade, salary, roll number, or any result calculated by the program. A variable is the name we give to that stored value.
Think of a variable like a labelled box in memory. The label is the variable name, the item inside the box is the value, and the type of box is the data type.
What Is a Variable?
A variable is a named memory location used to store data while a program is running. The value can be used later, printed on the screen, changed, or used in calculations.
For example, in `int age = 21;`, the variable name is `age`, the data type is `int`, and the stored value is `21`.
Why Do We Need Data Types?
C is a strongly typed language. That means C wants to know the type of data before storing it. This helps the compiler decide three things: what kind of value is allowed, how much memory may be needed, and which operations can be performed on that value.
For example, a whole number like 21 can be stored in an `int`, but a decimal value like 86.5 should be stored in a `float` or `double`.
Memory View of a Variable
When you create a variable, C reserves memory for it. The variable name is used by the programmer, but the computer works with memory addresses.
For example, `int age = 21;` means C creates space for an integer and stores 21 in that space. Later, whenever you write `age`, the program uses the value stored in that memory location.
Types of Data Types in C
Many beginner platforms explain C data types in two broad groups: primitive and non-primitive. In standard C learning, you will also see the names basic, derived, and user-defined. Both explanations point to the same idea.
Primitive or Basic Data Types
Primitive data types are the basic built-in types provided by C. They store simple values directly.
- `int` stores whole numbers such as 10, 25, or -8
- `char` stores a single character such as 'A', 'x', or '7'
- `float` stores decimal numbers with single precision such as 86.5
- `double` stores decimal numbers with more precision than float
- `void` means no value. It is commonly used when a function does not return anything
Non-Primitive Data Types
Non-primitive data types are made using primitive types. They help us store or represent more complex data.
- Arrays store multiple values of the same type, such as `int marks[5]`
- Pointers store memory addresses, such as `int *ptr`
- Structures group different types under one name, such as student roll number, marks, and grade
- Unions store different members in the same memory location
- Enums give names to integer constants, such as days of the week
Standard C Classification
- Basic types: `int`, `char`, `float`, `double`, `void`
- Derived types: arrays, pointers, and functions
- User-defined types: `struct`, `union`, `enum`, and `typedef`
Type Modifiers
C also provides modifiers that change the range or size of some data types.
- `short` and `long` change the storage range of integer types
- `signed` allows both positive and negative values
- `unsigned` allows only zero and positive values, giving a larger positive range
Range and Overflow
Every data type has a limited range. If a value goes beyond that range, overflow can happen.
For example, an `unsigned` type cannot store negative values. If you store the wrong kind of value, the result may not be what you expect. This is why choosing the correct data type matters.
Declaration and Initialization
Declaration means telling C that a variable exists. Initialization means giving it a starting value.
- `int age;` declares a variable
- `int age = 21;` declares and initializes a variable
- `age = 22;` changes the value later
Rules for Variable Names
- A variable name can contain letters, digits, and underscore
- It cannot start with a digit
- It cannot be a C keyword such as `int`, `return`, or `while`
- Use meaningful names such as `totalMarks`, `age`, or `salary`
Constants
A constant is a value that should not change during program execution.
```c
const float PI = 3.14f;
```
Use constants for fixed values such as tax rate, maximum marks, or mathematical values.
Type Conversion
Type conversion means changing a value from one data type to another. Sometimes C does it automatically, and sometimes the programmer does it manually.
```c
float average = (float) total / count;
```
Here `(float)` helps avoid integer division and gives a decimal result.
Choosing the Right Type
- Use `int` when the value is a whole number
- Use `float` or `double` when the value has decimals
- Use `char` for a single character
- Use arrays, structures, and pointers when one simple value is not enough
```c
#include <stdio.h>
int main(void) {
int age = 21;
float percentage = 86.5f;
double salary = 55000.75;
char grade = 'A';
printf("Age: %d\n", age);
printf("Percentage: %.2f\n", percentage);
printf("Salary: %.2lf\n", salary);
printf("Grade: %c\n", grade);
return 0;
}
```
Format Specifiers
When printing variables with `printf`, C needs a format specifier so it knows how to display the value.
- `%d` is used for int
- `%f` is used for float
- `%lf` is used for double in printf
- `%c` is used for char
One important note: C does not have a separate built-in `string` data type like many modern languages. In C, a string is usually stored as a character array ending with the null character `\0`.
Memory Size Note
The exact memory size of a data type can depend on the compiler and system. A beginner should first understand the purpose of each type. Later, you can use `sizeof` to check the size on your compiler.
```c
printf("Size of int = %zu bytes\n", sizeof(int));
```
Quick Practice
Create variables for your name's first letter, your age, your percentage, and your target salary. Print all values using the correct format specifiers.