×

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

How to fix problems in Windows after installing new software

Sometimes users face problems after installing new software in the windows operating system. Go along with the below steps to correct and fix them. Uninstall the software program or reinstall it If...

4 minutes read.

Top 10 online services and applications

It can be difficult to select the best Internet service or application for your needs because there are tens of thousands of them. An online service is a business that...

4 minutes read.

What is Hybrid Topology?

A hybrid topology is also called network topology. Topology can be defined as the physical and logical arrangements of computer networks. We can say that topology is nothing, but how...

6 minutes read.

What is RDIMM?

Servers use the RDIMM (registered memory) kind of memory chip most frequently. By reducing the electrical strain on a memory controller, it helps systems running on a server be more...

3 minutes read.

Internet Explorer Shortcut Keys

While using internet explorer, when we use shortcut keys, our work will be done faster when compared to the job done without using them. The reason is internet explorer is...

23 minutes read.

Block Diagram of Computer

Computer Block Diagram System: Mainly computer system consists of three parts, that are central processing unit (CPU),Input Devices, andOutput Devices. The Central Processing Unit (CPU) is divided into two parts...

5 minutes read.

What is a CSV file?

A plain text file known as a "Comma Separated Values" (CSV) file holds data by separating data items with commas. When data has to be interoperable with a wide range...

5 minutes read.

Output devices of Computer

The output devices are the hardware parts which can create the whole computer system with the combination of all hardware parts. The user can get the desired output via output...

10 minutes read.

What is Device Driver?

Device Driver A device driver is a specific type of programming application that permits one equipment device (like a PC) to interface with another equipment device (like a printer). A device...

6 minutes read.

How to View or Change the Screen Resolution of a Monitor?

The images you see on your computer screen are made up of "pixels," millions of minimal components. These pixels produce the colors, patterns, and videos you see on your monitor....

4 minutes read.

What Does Alt + L Do?

The keyboard shortcut, "Alt+L"  is frequently used to open the Developer Tab in the Ribbon. Open the program in which you want to use the Developer tab. Press and hold the "Alt"...

3 minutes read.

What is Cyber Warfare?

Cyber warfare is the use of technology, which is used to attack the enemy state. It is a hacking technique mainly used by military forces to target foreign countries' key...

7 minutes read.

What is boot menu?

When we turn on our computer, the machine goes through a process known as booting that is carried out automatically. This could be accomplished by the CPU's circuitry, software, or...

3 minutes read.

What is SAN?

A storage area network, sometimes known as SAN, is a specialized, quick network that provides network access to storage devices. Hosts, switches, storage components, and storage devices make up standard...

7 minutes read.

What is BIOS?

BIOS stands for Basic Input/Output System. It is firmware that controls the basic functions of a computer, such as booting the system and communicating with the hardware. It is stored...

7 minutes read.

What is Asynchronous DRAM?

Asynchronous DRAM (Dynamic Random Access Memory) is a type of memory that operates independently of the system clock. It is not synced with the computer system clock; therefore, it is...

5 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.

What is AM?

AM may refer to any of the following: When referring to audio or a signal, amplitude modulation (AM) is a method of transmitting information using radio waves. In AM, the information...

4 minutes read.

What does Alt + T do?

Alt + T is one of the keyboard shortcuts mostly used to open the Tools present in the file menu. Usage of Alt + T Keyboard shortcut For using the Alt +...

4 minutes read.

What are the Disadvantages of Computers?

Although there are various advantages to using a computer, there are also hazards and disadvantages (like most things in life). Computers, if not used properly, can create a variety of...

6 minutes read.