What is a Variable?
In our daily life, the variable plays an important role in programming as well as in mathematics for storing data or information. Most mathematicians tend to assume variables for solving problems. In programming languages like C, JAVA, C++, etc., the variable has different definitions, but the use and application are the same.
For Example, let’s assume that the variable is kind of like a square box shown below with a label. In this case, the label is X; this is a special square box that holds a single number within it. We can't see what number is inside the square bucket. We will need a clue to figure out what that number is. Here is an example of what a clue might look like.
This above clue is called an equation because of the equal sign. The square tends to save the value 2 in the box. This is the role of a variable and also the best example to understand.
Now we are going to discuss the variables in different programming languages.
In research, we also use a variable, which is referred to as it is a logical set that has some attributes. In research, attributes are closely related to that variables, and in the programming aspect, also we relate attributes to a variable.
Variable in C Language
The name of the memory location is known as a variable where the data is stored. We can alter the value of a variable. In C, we have the address of the variable, and an address is different from the value. A variable is a symbolic representation in terms of programming language.
Example
#include<stdio.h>
int main(){
int a;
int b;
a=4;
b=a+4;
printf(“the value of a=%d”, a);
printf(“the value of b=%d”,b);
return 0;
}
Output:
the value of a=4
the value of b=8
The above example can explain how a variable works and how we store the value in the variable.
There are different types of variables as below:
1. Local Variable
2. Global Variable
3. Static Variable
4. Automatic Variable
5. External Variable
Local Variable: Variable that is declared or initialized inside the functions are called Local Variable.
Example:
#include<stdio.h>
void main(){
int a=3; #this is the local variable initialized after declaring the variable
}
Global Variable: The variables declared outside the functions are called Global Variables.
Example:
#include<stdio.h>
int a=5; #this is the variable declared outside the main function
int areaS(){
int area;
area=a*a;
return area;
}
void main(){
printf(“the area of the Square: %d”,areaS());
}
Output:
The area of the Square: 25
Static Variable: Variables declared with a keyword static are static variables whose lifetime is throughout the program run time.
Example:
#include<stdio.h>
Void sum(){
Int a=3;
Static int b = 4;
a=a+2;
b=b+2;
printf(“the value of a=%d and b=%d”, a, b);
}
Output:
The value of a=5 and b = 6
Automatic Variable: Variables declared with auto keywords are known as automatic variables.
Example:
#include<stdio.h>
void main(){
int a = 3;
auto int b = 4;
printf(“a=%d, b=%d”,a,b);
}
Output:
a=3, b=4
External Variable: Eternal variables are used to share the variables among the multiple C files.
Example:
Program1: save.h
#include<stdio.h>
Extern int x=1, y=3;
Program 2: intro.c
#include<save.h>
#include<stdio.h>
Void main(){
Int sum;
Printf(“x=%d\n”, x);
Printf(“y=%d\n”,y);
sum=x+y;
printf(“sum=%d”, sum);
}
Output:
x=1
y=3
sum=4
The explanation for the above program:
You need to save the file as save.h in a separate folder and write the code as given. Create another file as intro.c, which is a c file, and write #include<save.h>, which will import the external file.
Each object in object-oriented programming includes the data variables of the class of which it is an instance. The methods of the object are made to deal with the actual values that are presented to it when it is utilized.
Rules that should be followed while naming a variable in C:
- Only letters (including uppercase and lowercase characters), numbers, and underscore are permitted in variable names.
- A variable's first letter must either be a letter or an underscore.
- There is no restriction on the length of a variable name or identifier. However, if the variable name is larger than 31 characters, you can have issues with some compilers.
This is all about variables in C language, where we came to know the different types of variables and their programs. We have the explanation for the codes and the rules for writing the variables.
Variable in JAVA
In JAVA, we need to define the variable in a different manner, and A variable is a container that is used to store the data values in it. It is a case-sensitive language. In Java, there are three two types of datatypes for initializing the variables that are listed below:
- Primitive
- Non-Primitive
In Java, there are different types of datatypes to initialize the variable that are:
- int
- string
- float
- char
- boolean
Compared to C language, it has two different datatypes, that are string and Boolean, and string is used to initialize a text or group of characters, whereas Boolean has two statements, True and False.
In Java, we also have a final variable that can be initialized, and we cannot change the value.
In Java, the variable has three classifications that are listed below:
- Local variable
- Static variable
- Instance variable
Local Variable:
Local variables are variables that are declared inside the method body. Only that method may utilize this variable, and the other methods in the class are completely unaware of its existence.
Static Variable:
Static variables are those that have been explicitly designated as static. It can't be regional. The static variable can be created once and shared across all of the class instances. When the class is loaded into memory, static variable memory is only allocated once.
Instance Variable:
An instance variable is a variable that is declared inside a class but outside the method's body. It has not been designated as static. Because its value is instance-specific and not shared across instances, it is known as an instance variable.
For example:
public class A
{
static int a=20;
void method()
{
int b = 30;
}
Public static void main(String arg[])
{
int c=40;
}
}
The explanation for the above program:
In the above program, we can see static int a = 20, which is declared, and that is a static variable that is not local. int b = 30 comes under the local variable, which can be utilized only under the void method() function. int c = 40 is a variable that is initialized outside of the method body.
In Java, if we have a memory, we need to create space for creating a variable where it utilizes that space. The area which we have created to store the value can be altered or can be changed.
Variable in C++
A simple variable definition consists of a type specifier followed by a list of one or more variable names separated by commas and ends with a semi-colon.
C++ language is the same as that C language in that we have the same classifications as that C language. In this, the keywords changes from C.
For example:
#include<iostream>
using namespace std;
int main(){
int a=30;
cout << a;
return 0;
}
Output:
30
Variable in Python
There is no requirement to create variables in advance while using Python, nor does it want you to define the data type at the time of variable creation.
There are 4 major datatypes that are used to initialize the variable:
- Numbers
- String
- List
- Tuple
Rules for defining a variable in Python:
- The underscore character or a letter must come first in a variable name.
- No number may begin a variable name.
- Only underscores (A-z, 0-9, and _) and alphanumeric characters are permitted in variable names.
- Names of variables are case-sensitive (alphabet, Alphabet, and ALPHABET are three different variables)
For example:
a=3
b=”Hello, world!”
print(a)
print(b)
Output:
3
Hello, world!