Syntax of variable
If you want to declare a variable, you must also define its type.
For example:
//Declaring a single variable
type variable_name;
//Declaring multiple variable
type variable1,variable2,variable3;
Rules for declaring a variable
- Variable must begin with a character without space. Example: value.
- Variable can also begin with an underscore('_').Example: _value.
- Variable can be a combination of uppercase and lowercase letters. For example, suM and Sum are can both be used as variables.
- The variable should not be a language(C/C++) keyword.
- Blank spaces can not be used in variable names. For example, we are declaring two variables, "name" and "my name" here, we can use "name" as a variable, but we can not use "my name" as a variable.
- Variable names are case sensitive, so be careful using variables.
- Variable type can be int,float,char,bool etc.
Also, see Literals in C.
Types of variables based on the scope
- Local variables
- Global variables
- Static variables
Local variable
A variable that is declared inside the function or block is known as a local variable. We can access this type of variable only inside the function. If we use it outside the function, then it gives an error.
Code
#include <iostream>
using namespace std;
//initialising a function
void fun()
{
int val=10;
cout<<val<<endl;
}
int main() {
fun();
cout<<val<<endl; // here give an error that val is not declared
return 0;
}
In the above code, You cannot access 'Val' outside of the function. If you try to access it, you will get an error('Val' is not declared).
Global variable
Variables that are declared outside the function or block are called global variables. These types of variables can be accessed anywhere in the program. Global variables are usually declared on top of all functions. This is a less secure variable than local variables.
Code
#include <iostream>
using namespace std;
int k=15; // k is a global variable.
void fun()
{
int val=10;
cout<<k<<endl;
cout<<val<<endl;
}
int main() {
fun();
cout<<k<<endl;
return 0;
}
Output
15
10
15
In the above code, You can access the 'k' variable throughout the program
Static variable
A variable declared with a static keyword is known as a static variable. It retains its value between the multiple function calls. The static variable is created at the start of program execution and destroyed automatically when the program's execution ends. If a static variable is not initialized, then the default value of the static variable is 0.
Code
#include <iostream>
using namespace std;
void fun()
{
int k=15;
static int val=10; // val is a static variable
cout<<k<<endl;
cout<<val<<endl;
val++;
k++;
}
int main() {
fun();
fun();
return 0;
}
Output:
15
10
15
11
In the above code, you can see that the static variable 'Val' is maintaining its value throughout every function call. It happens because the Static variable is created at the start of program execution and destroyed automatically when the program's execution ends.
You can practice by yourself with the help of online c++ compiler.
Type of variable based on data types
Some basic variables based on data types are:
- Bool: this type of variable stores only true or false.
- Int: this type of variable stores integer type values such as 12 or -12 etc.
- Char: this type of variable stores characters such as 'a' or 'A' etc.
- Float: this type of variable stores single-precision floating-point values such as 12.4 or -12.4.
- Double: this type of variable stores dob precision floating-point values such as 12.42 or -12.42.
Also Read - C++ Interview Questions
Frequently Asked Questions
-
What is the use of the variable?
Ans: Variables are used to store the input and output during the program's execution.
-
What does the variable declaration mean?
Ans: variable declaration means we declare a variable before using the variable.
-
What do you mean by the scope of a variable?
Ans: scope of the variable means the part of the program where it can be directly accessible.
-
What are the static variables?
Ans: the variable declared with a static keyword is known as a static variable.
Key Takeaways
In this blog, we have learned about variables, the rules of declaring a variable, and the type of variables.
That's all about variables. This blog is over, but the thirst for learning is not over yet. Continue your journey of acquiring knowledge with our practice platform Coding Ninjas Studio to learn, attempt, read interview experiences, and much more. Till then, have a nice day and Happy Coding!