×

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

Fifth Generation of Computer

The period of the fifth generation is 1980-onwards. The VLSI technology develops into ULSI (ultra large scale integration) technology in the fifth generation. These are used in the production of...

6 minutes read.

Computing Power

Computing power comes at how fast a machine can perform a task. When a command is given to any computer, it is broken down into mathematical series of problems. A...

3 minutes read.

How to view the HTML source code in Microsoft Word?

Using Microsoft Word, the document can be saved as a web page, but word does not support the feature which enables the user to view the source code in the...

3 minutes read.

What is an Icon?

The majority of people nowadays use the word "icon" to refer to a small, selectable or unselectable image that appears on a computer's graphical user interface (GUI) or the web...

3 minutes read.

What is a Memory Slot?

The memory slot, often known as a RAM slot, is what enables computer memory chips or sticks to be inserted in computer. It helps in determining the type of RAM to be used...

11 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 is a Dynamic Website?

There are two types of websites. First static and second dynamic website, but before we go for dynamic, we should know about the static website so that you can easily...

4 minutes read.

What is network Interface card (NIC)?

A network interface card is a hardware part of a computer, which is used to connect the internet in the system. The latest network interface card gives functionality to the...

6 minutes read.

Difference between File and folder

File: A file is a unit of Storage for comparative data. A document can be a picture, video, executable record, framework, or application document. Secondary storage options include DVDs, flash drives, SSD...

3 minutes read.

What is Scratch?

Scratch is a visual programming language that lets students build interactive tales, games, and animations. Students develop their creative thinking, logical reasoning, and teamwork skills as they create Scratch projects....

3 minutes read.

Use of Internet

The Internet, sometimes known as "the Net," is a global system of computer networks. It is a network of devices that allows users at any one computer to obtain information...

13 minutes read.

How to restart Microsoft Windows

Restarting resolves many errors and helps in running the windows smoothly, and if any new updates or new software is installed, restarting clears the memory and stops the tasks that...

4 minutes read.

What is a Software Suite?

A software suite is a grouping of computer programs and applications with comparable user interfaces that work together. It is a set of two or more software packages or programs...

3 minutes read.

High-Level Language in Computer

High-Level Language The high-level language is the programming language such as BASIC,C,C++,COBOL,FORTRAN,Java, Perl,PHP,Python,Ruby, Visual Basic, and Pascal, etc. These languages have strong abstraction, the style and the context which are more...

5 minutes read.

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

6 minutes read.

Is my computer 64 bit?

While using computers and smartphones, we have often heard the term ’32-bit’ or ‘64-bit’. But many of us do know what these terms mean. And the first question which arises...

5 minutes read.

What is REM?

Rem is a short form of the remark. In the system, a file remark is placed to skip line loading. By placing "REM" in front of a line, a remark...

3 minutes read.

Generation of Computer

History of computer The abacus was initially used for the mathematical tasks in the history of the computer. It is also known as counting frame and a calculating tool that was used in Europe, China,...

6 minutes read.

What is a hyperlink?

Nowadays, internet is the basic need of every human. As technology is increasing day by day, people use the latest advanced technology and try to connect worldwide in an easy...

6 minutes read.

What is Cache Memory? Types of Cache Memory

Cache Memory in Computer Cache memory is a semiconductor chip used to retrieve data from a computer's memory efficiently. Generally, cache memory is much costlier than main memory or any other...

4 minutes read.