×

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.

What is a Variable

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:

  1. int
  2. string
  3. float
  4. char
  5. 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:

  1. Local variable
  2. Static variable
  3. 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:

  1. Numbers
  2. String
  3. List
  4. 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!

Related Topics

What is EHCI?

EHCI stands for "Enhanced Host Controller Interface." It is a specification that defines the interface between a host controller (such as a computer or other device) and a device controller...

4 minutes read.

What is a Ring Topology?

The word topology defines the relationship or arrangement of its component elements. A ring network is an architecture of a network where each node is connected to precisely two other...

4 minutes read.

What is Select All?

Selecting everything in a list or display means choosing all of the text, files, or other items. Ctrl+A selects (highlights) everything in the active window in the majority of apps....

3 minutes read.

What is the difference between multiprocessor and distributed systems

Multiprocessor A Multiprocessor is a PC framework with at least two focal handling units (CPUs) share full admittance to a typical RAM. The primary goal of utilizing a multiprocessor is to...

3 minutes read.

What is a Username?

As we know that technology is increasing day by day, so we must have to aware about this. In this modern world, everyone is using computer and the internet. So,...

4 minutes read.

My computer is running slow, what steps can i do to fix it?

It’s your time to work on your computer, and it just remains hanged, not letting you switch to other programs; in other words, your computer is in a state of...

4 minutes read.

What is Mesh Topology?

Mesh topology is a kind of systems administration in which every computer is interconnected to each other. In Mesh Topology, the connections between devices occur randomly. The connected hubs can...

5 minutes read.

How to enable or disable the preview pane of Microsoft Outlook

Preview Pane lets you preview the file's contents in the viewing pane. Users can quickly scan the contents of a selected file in the preview window without actually opening it....

3 minutes read.

Update an Antivirus

What is an Antivirus Program? An antivirus program is a specifically designed software that allows the user to detect any harmful or infected application or file. It not only detects any...

7 minutes read.

What is Data Manipulation?

Data Manipulation is the process in which the developer organizes the data making it more understandable by properly arranging it in the proper structure. For example, you have gathered the...

9 minutes read.

What is flash memory?

Flash memory, commonly known as flash storage, is a non-volatile memory. In electronics products and enterprise software, flash memory is frequently used for storage and transfer. In-memory processing devices use...

6 minutes read.

What does Alt+B do?

Generally we can do all the operations with the help of mouse and keyboard. Whatever we want to do, mouse is essential part of the computer system. But it takes...

3 minutes read.

How does a Computer work?

How does a Computer work? As humans we always built tools that helps up to solve our problems. All the early inventions unlike wheelbarrow, hammer, printing press, or a tractor, helped...

8 minutes read.

Computer Fundamentals Tutorial

What is a Computer? "The computer is an electronic device that store, retrieve and process the data in binary form according to our need. It takes some input, process it, and...

10 minutes read.

What is a Digital Assistant?

It is a voice assistant or a predictive chatbot that is programmed in such a manner that it can answer the queries of the user or can perform certain simple...

8 minutes read.

What is Video Card?

It is a component in the personal computer that is used to connect with the motherboard in the CPU. It is also called the graphic card or video control; as...

13 minutes read.

How can I test how many words I can write a minute

The most used metric for measuring typing speed is WPM, in words per minute. On average, the "word" has 5 characters. To calculate WPM, multiply the number of words typed...

7 minutes read.

What Are Bottom Row Keys?

The bottom row keys, also known as the "home row" keys, are important for several reasons: The bottom row keys are where the fingers rest when not typing and are the...

7 minutes read.

What does Alt + X do?

Alt + X is used for displaying the character’s Unicode character code and is a widely used keyboard shortcut. Usage of Alt  + X keyboard shortcut For using the Alt + X...

3 minutes read.

What is the svchost.exe file used for in Windows?

Svchost.exe is a crucial Windows file that loads the DLL files necessary by Microsoft Windows and other programs that operate under Windows on your computer. Multiple Windows services can share...

8 minutes read.