Summary: in this tutorial, you will learn about C variables that allow you to manipulate data in your program.
Introduction to C variables
A program consists of data and a set of instructions that process the data. The data can be numbers and characters. To store data in the program, you use variables.
A variable allows you to store data during the excution of the program.
Declaring variables
Before using a variable, you need to declare it. A variable declartion serves two purposes:
- Define the name of the variable.
- Define the type of data that the variable can store.
For example, the following statement declares a variable:
int age;
Code language: C++ (cpp)
The keyword int
tells C that this variable will hold an integer value. The variable name is age. The semicolon (;) ends the statement.
The general form of a variable declaration is:
Code language: C++ (cpp)type variable_name;
The type can be any valid type in C.
When you declare a variable:
- C reserves a space in the memory to hold the value of the variable. The amount of memory depends on the type of value that the variable will store.
- C also allocates a memory space that associate with the variable name and a unique address.
A variable can be declared at any point in the program before it is used. It’s a good practice to declare a variable at the place that’s closest to its first use.
The variable name in C needs to follow these rules:
- The variable name can contain letters, digits, and the underscore (
_
) . The first character of the variable name must be a letter or underscore (_
). However, you should avoid using the underscore ( _) as the first letter because it can clash with standard system variables. - According to ANSI C, the variable name should have a maximum number of 31 characers.
- Also, variable names must not be the same as reserved words or keywords in C.
The following table shows the keywords in C:
auto | break | int | return |
case | char | register | signed |
const | continue | short | static |
default | do | sizeof | switch |
double | else | struct | union |
enum | extern | typedef | void |
float | for | unsigned | while |
goto | if | volatile |
If you have multiple variable with the same type, you can declare them in a single statement. For example:
int age, min_age, max_age;
Code language: C++ (cpp)
Initializing Variables
To make it more convenient, C allows you to initialize a variable when you declare it. For example
int age = 1;
char ch = 'a';
Code language: C++ (cpp)
It’s a good practice to place initialized variables on a separate line and add a descriptive comment to explain why the variable is initialized to a specific value. For example:
int speed = 50; // minimum speed limit on highway
Code language: C++ (cpp)
Assigning values to variables
To assign a variable a value, you can use the assignment operator (=
). For example:
int age = 1;
int speed_limit;
age = 2;
speed_limit = 50; // 50 miles per hour
Code language: C++ (cpp)
How it works.
- First, declare two variables age and speed_limit.
- Second, assign 2 to the age variable and 50 to the speed_limit variable.
Besides a value, you can assign a the value of one variable to another. For example:
int revenue = 100, cost = 90;
int profit;
profit = revenue - cost; // 10
Code language: C++ (cpp)
Summary
- A variable stores data during the execution of a program.
- Do declare a variable before using it.
- A variable declaration includes type of data that the variable will hold and the variable name.
- A variable can be assigned an initial value during declaration.
- Use the assignment operator (=) to assign a value to a variable.