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.

Non-Primitive Data Types

Non-primitive data types are made using primitive types. They help us store or represent more complex data.

Standard C Classification

Type Modifiers

C also provides modifiers that change the range or size of some data types.

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.

Rules for Variable Names

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

```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.

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.

More C Lessons

Browse all PrepCampus study materials