- Variable is known as memory location.
- It is used to store data.
- Its value can be changed and can reused many times.
Syntax
- type variable_list;
Example
- int a;
- char b;
- float c;
Rules for defining variables
- A variable can have alphabets, digits, and underscore.
- A variable name can start with the alphabet, and underscore only.
- It can't start with a digit.
- No whitespace is allowed within the variable name.
- A variable name must not be any reserved word or keyword, e.g. int, float, etc.
Types of Variables in C
- local variable
- global variable
- static variable
- automatic variable
- external variable
1. Local variable
- Variable that is declare inside the function or block is known as local variable.
- It is also known internal variable.
- You must have to initialize the local variable before it is used.
Syntax
- void function()
- {
- int a;
- }
2. Global variable
- Variable that is declare outside the function or block is known as global variable.
- It is also known as external variable.
- Syntax
- int a;
- void function()
- {
- a=10;
- }
3. Static variable
- Variable that is declare using static keyword is known as static variable.
- Static variables can be defined inside or outside the function.
- Static variable value can not be changed during the execution of the program.
- Syntax
- void function()
- {
- static int a=10;
- }
4. Automatic variable
- Variables that can be defined using keyword auto.
- Variables allocate memory automatically.
- Syntax
- void function()
- {
- auto int a=10;
- }
5. External variable
- Variable that is accessible in multiple c source file .
- We can declare external variable using external keyword.
- Syntax
- external_var.h
- external int a=20;
- Example
#include <external_var.h>
#include <stdio.h>
#include <stdio.h>
- void function()
- {
- printf("External variable %d",a);
- }
0 Comments: