Variables:--
- During program execution number of operations are performed on the data.
- The data is stored in the memory. It is difficult to remember the memory address where the data is going to be stored, so to simplify this, C allows to refer these memory locations by particular name.
- A variable is a program entity which is used to hold the value.
- One variable can store only one value at a time. The value stored in variable can be changeable during program execution.
- Specific type of variables store only those type of values, for example: integer type of variables store only natural numbers.
- Each variable has a particular type. The type determines:
- O The size and storage layout of the variable in memory
- The range of values that can be stored within that memory
- The set of operations that can be applied on the on variable.
- To specify the variable type, data type is used. Variable may belong to any of the data type e.g. int, char, float, etc.
Example:--
#include <stdio.h>
int main()
{
// declaration and definition of variable 'a123'
char a123 = 'a';
// This is also both declaration
// and definition as 'b' is allocated
// memory and assigned some garbage value.
float b;
// multiple declarations and definitions
int _c, _d45, e;
// Let us print a variable
printf("%c \n", a123);
return 0;
}
Output:a
Rules for assigning variable name:--- Each variable name starts with an alphabet or underscore and then followed by any number of alphabets, digits or underscore.
- Other than underscore no special symbol is allowed to form the variable name.
- Variable name should not start with digit.
- In a program variable name must be unique, Variables are case sensitive so the area, AREA and Area considered as different variables.
- A variable should not contain any comma or blank space. Variable name should not same as of the keywords otherwise compiler will generate an error.
Declaring variables
- It is mandatory to declare a variable which is going to be used in the program.
- Variable is declared with its data type so that the compiler will know how much memory it will require and what type of data is going to be stored in it.
Syntax of declaring variable:--
Example:-int marks;
char ch;
float salary;
Types of variable
- There are following types of variables in c program
 |
Types of Variable
|
- 1. Local variable The local variables are declared and initialized within function. The scope of these variables is within the function where it is declared and initialized and cannot be accessed outside of the function.
- 2. Global variable The global variables are declared outside the main function. The scope of these variables is throughout the program. Every function in the program can access them.
you can join my Telegram group and Learn C programming
Comments