Variable in C/C++ Local Global Static Automatic External

 https://data-flair.training/blogs/variables-in-c-and-c-plus-plus/

Naming a Variable in C/C++

You need to follow some rules, before naming a variable in C and C++:

  1. A variable must not start with a digit.
  2. A variable can begin with an alphabet or an underscore.
  3. Variables in C and C++ are case-sensitive which means that uppercase and lowercase characters are treated differently.
  4. A variable must not contain any special character or symbol.
  5. White spaces are not allowed while naming a variable.
  6. Variables should not be of the same name in the same scope.
  7. A variable name cannot be a keyword.
  8. The name of the variable should be unique.

Valid variable names 

ticketdata                                             
_ticketdata
ticket_data

Invalid variable names

56ticketdata
ticket@data
ticket data

Variable Definition

A variable definition in C and C++ defines the variable name and assigns the data type associated with it in some space in computer memory. After giving its definition, this variable can be used in the program depending upon the scope of that variable.

By defining a variable, you indicate the name and data type of the variable to the compiler. The compiler allocates some memory to the variable according to its size specification.

Defining Variables in C and C++ with Example

int var;

Variable Declaration

There is a huge difference between defining a variable and declaring a variable.

By declaring a variable in C and C++, we simply tell the compiler that this variable exists somewhere in the program. But, the declaration does not allocate any memory for that variable.

Declaration of variable informs the compiler that some variable of a specific type and name exists. The definition of variable allocates memory for that variable in the program.

So, it is safe to say that the variable definition is a combination of declaration and memory allocation. A variable declaration can occur many times but variable definition occurs only once in a program, or else it would lead to wastage of memory.

Variable Initialization

Variable initialization means assigning some value to that variable. The initialization of a variable and declaration can occur in the same line.

int demo = 23;

1. Local Variables

The scope of local variables lies only within the function or the block of code. These variables stay in the memory till the end of the program

#include <iostream>
using namespace std;
 
int main()
{
cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;
 
int result = 5; //local variable
cout<<"The result is: "<< result<<endl;
return 0;
}

2. Global Variables

A global variable has a global scope, that is, this variable is valid until the end of the program. These variables are available to all the functions in that program.

#include <iostream>
using namespace std;
 
int sumf();
int sum = 2; // global variable
int main ()
{
 
cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;;
int result =5; //local variable
sumf();
return 0;
}
int sumf()
{
cout<<"Sum is: "<< sum <<endl;
cout<<"The result is: "<< result <<endl;
}

In this way, we can implement a global variable in C++

#include <iostream>
using namespace std;
 
int sumf();
int sum = 2; //global variable
int result =5; //global variable
int main ()
{
cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;
sumf();
return 0;
}
int sumf()
{
cout<<"Sum is: "<< sum <<endl;
cout<<"The result is: "<< result <<endl;
}

3. Static Variables

Static variable retains its value within the function calls. ‘static’ keyword is used to define a static variable. A static variable can preserve its value, and you can not initialize a static variable again. 

#include <iostream>
using namespace std;
void statf();
int main ()
{
cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;
int i;
for(i = 0; i < 5; i++)
{
statf();
}
}
void statf()
{ 
int a = 20; //local variable 
static int b = 20; //static variable 
a = a + 1; 
b = b + 1; 
cout<<"Value of a: " << a << " ,Value of b: "<< b <<endl; 
}

With every function call, static variable uses the preserved value and increment the value of a variable, whereas a local variable re-initializes the value every time function calls takes place.

4. Automatic Variables

The variable declared inside a block is called an automatic variable.

The automatic variables in C are different from the local variables, automatic variable allocates memory upon the entry to that block and frees the occupied space when the control exits from the block.

‘auto’ keyword defines an automatic variable.

Example in C 

auto int var = 39;

5. External Variables

We use ‘extern’ keyword to increase the visibility of the program. An extern variable is available to other files too.

The difference between global and extern variable is, a global variable is available anywhere within the file or program in which global variable declares but an extern variable is available for other files too.

Note: Variables with keyword ‘extern’ are only declared; they are not defined. Also, initialization of extern keywords can also be considered as its definition.

extern int var;

It means that extern variable var is available or valid in the program and in the other files too.

Note: Declaration of var takes place and not the definition.




No comments: