C variable
The target
In this article, you’ll learn about the C variable that allows you to manipulate data in your program.
Introducing the C variable
A program consists of data and a set of instructions for processing that data. Data can be numbers and characters. To store data in a program, you need to use variables.
Variables allow you to store data during program execution.
Declare a variable
Before you can use a variable, you need to declare it. Variable declarations serve two purposes:
- Define the name of the variable.
- Defines the types of data a variable can store.
For example, the following statement declares a variable:
int age;
The int keyword tells C that the variable will hold an integer value. The variable name is age. The semicolon (;) End statement.
The general form of a variable declaration is:
type variable_name;
The type can be any valid type in C.
When you declare a variable:
- C reserves a space in memory to hold the value of a variable. The size of memory depends on the type of value the variable is to store.
- C also allocates a memory space associated with variable names and unique addresses.
Variables can be declared at any point in the program before they are used. It’s a good practice to declare variables closest to where they were first used.
Variable names in C should follow the following rules:
- Variable names can contain letters, digits, and underscores. The first character of a variable name must be a letter or underscore. However, you should avoid using the underscore (_) as the first letter, because it may conflict with standard system variables.
- According to ANSI C, variable names should have a maximum of 31 characters.
- In addition, variable names cannot be the same as reserved words or keywords in C.
Keywords in C are shown in the following table:
auto | break | int | reurn |
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 variables of the same type, you can declare them in a single statement. Such as:
int age, min_age, max_age;
Initialize a variable
For convenience, C allows variables to be initialized when they are declared. For example,
int age= 1;
Char ch = 'a';
It is a good practice to put initialized variables on a separate line and add descriptive comments explaining why the variable was initialized to a particular value. Such as:
int speed = 60; // Minimum speed limit on motorway
Variable assignment
To assign a value to a variable, use the assignment operator (=). Such as:
int age= 1; int speed_limit; age= 2; speed_limit = 60; // 60 kilometers per hourCopy the code
How it works.
- First, declare two variables age and speed_limit.
- Second, assign the age variable to 2 and the speed_limit variable to 50.
In addition to values, you can assign the value of one variable to another. Such as:
int revenue = 100, cost = 90; int profit; profit=revenue-cost; / / 10Copy the code
conclusion
- Variables store data during program execution.
- Be sure to declare a variable before you use it.
- A variable declaration includes the data type and variable name that the variable will hold.
- You can assign an initial value to a variable during the declaration.
- Assign a value to a variable using the assignment operator (=).