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++:
- A variable must not start with a digit.
- A variable can begin with an alphabet or an underscore.
- Variables in C and C++ are case-sensitive which means that uppercase and lowercase characters are treated differently.
- A variable must not contain any special character or symbol.
- White spaces are not allowed while naming a variable.
- Variables should not be of the same name in the same scope.
- A variable name cannot be a keyword.
- 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
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.
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.
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.
In this way, we can implement a global variable in C++
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.
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
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.
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:
Post a Comment