0
0
Cprogramming~5 mins

Why variables are needed in C

Choose your learning style9 modes available
Introduction

Variables help us store and use information in a program. They let us keep data that can change while the program runs.

When you want to remember a number someone typed in.
When you need to keep track of a score in a game.
When you want to store a name or message to show later.
When you want to do math with values that can change.
When you want to reuse a value many times without typing it again.
Syntax
C
type variable_name;

// Example:
int age;

Variables have a type that tells the computer what kind of data it holds, like numbers or letters.

You give each variable a name to use it later in your program.

Examples
Here, count stores whole numbers, price stores decimal numbers, and letter stores a single character.
C
int count;
float price;
char letter;
You can also give a variable a starting value when you create it.
C
int age = 25;
float temperature = 36.5;
Sample Program

This program shows how a variable score stores a number that changes. It starts at 0, then changes to 10.

C
#include <stdio.h>

int main() {
    int score = 0;  // store game score
    printf("Initial score: %d\n", score);

    score = 10;  // update score
    printf("Updated score: %d\n", score);

    return 0;
}
OutputSuccess
Important Notes

Variables make programs flexible because you can change their values anytime.

Always choose clear names for variables so your code is easy to understand.

Summary

Variables store data that can change while the program runs.

They have types and names to organize information.

Using variables helps write clear and flexible programs.