Javatpoint Logo
Javatpoint Logo

Variables

In this article, we are going to discuss the variables and also discuss their types and examples.

What are the Variables?

Variables are the names that assign to computer memory locations where values are stored in a computer program. A variable is a named data unit with a value assigned with it. Variables are utilized in almost all programming languages and may take many several forms specified by the script or software programmer. The name of the variable denotes the type of data it holds. Variables are so named because the information represented by them can change while the operations on the variable remain constant. In general, a program should be written in a Symbolic notation so that a statement is always true symbolically.

Variables can represent any type of data, such as Booleans, names, sounds, scalars, texts, integers, arrays, images, or any item or class of objects supported by the computer language. Compilers and interpreters replace the symbolic names of variables with the real data location. During execution, data in locations changes, but locations and names remain constant.

The key point to remember before declaring variables

There are various key points that we need to remember before declaring the variables in the program. Some of them are as follows:

  1. There are various programming languages like Java, Python, C++, PHP, Perl,, that do not allow us to define the data type when declaring variables. So, we may store the value in integer, float, or long without declaring their data type.
  2. In contrast to the C programming language, variables should be declared before they are used. We cannot utilize a variable name in the program without creating it, although some programming languages, such as python, allows us to do so.
  3. We may declare a variable any name that we want, such as age, gender, salary, year, or anything else. However, most computer languages allow just a fixed number of characters in variable names. For the time being, we recommend that we use just ...z, A....Z, 0....9 in the variable names and that we begin their names with alphabets rather than numerals.
  4. A variable name may store a single type of value. For example, if we declare a variable int type, it stores only an integer value.
  5. A variable name can only be used once in the program. For example, if we define a variable to hold an integer value, we can't use it to store any other sort of value.
  6. Almost no programming languages allow variable names to begin with a digit, so the year is not a legitimate variable name, while year are legitimate variable names.

Creating Variables

In C programming, it is also known as declaring variables. Many programming languages have various methods to declare variables inside the program.

For example:

Let's take a simple example of C programming that shows how we may declare a variable in the program.

In the above example, we have declared two variables with the names a and b to reserve two memory locations. The int keyword was used to describe the variable data type, indicating that we wish to store integer values in these two variables. We may also build variables to store the value of long, float, char, or any other data type.

For example:

Now, we see that we may declare any type of variable according to the programming language.

Store Values in Variables

As we have seen in the above example, we learn that how we may declare the variable in the program. Now, we are going to discuss how we may store the value in the variables.

For example:

The above program contains two extra statements in which we store 15 in variable a and 18 in variable b. Almost all programming languages provide a similar method for saving values in variables in which we keep the variable name on the left-hand side of an equal sign = and the value we wish to store in the variable on the right-hand side.

We have now accomplished two steps: we established two variables and then stored the required data in those variables. As a result, variable a now has a value of 15, and variable b now has a value of 18. To put it another way, when the preceding program runs, memory location a will hold 15, and memory location b will retain 18.

Access Stored values in the variable

If we do not store the values, no use in creating variables and store values in them. We will take two variables, a and b, that hold the values 15 and 18. Now, let's see how we can print the values saved in these two variables.

Example:

Let's take an example to show how we may access the stored values in the variables.

Output: After executing this above code, we will get the output as shown below:

The Output is:

Value of a = 15
Value of b = 18

We may remember that we used the printf() method to print the value of any data type. Now, we're going to use it to print the variable's values. In printf() commands, we use %d, which is replaced with the values of the provided variable. Thus, we may print both values with a single printf() statement, as seen below.

Example:

Let's take an example of C programming to understand how we may use this method.

Output: After executing this above code, we will get output as shown below:

Value of a = 15 and value of b = 18

In C programming, we must use %f instead of %d if we wish to use a float variable, and we must use %c if we want to print a character value. Similarly, % and characters can be used to print distinct data types.

Variables in Java

Here, we are going to discuss that how we may declare the variable in Java Programming. So, let's take a program in which we will create two variables, a and b, that are similar to the C programming and will assign 15 and 18 to these variables.

For example:

Output: After executing this code, we will get the output as shown below:

The output is:

Value of a = 15
Value of b = 18
Value of a = 15 and value of b = 18

Variables in python

Now, we are going to discuss that how we may declare the variables in python. Let's take a program in which we may use the two variables a and b, and we assign the values 15 and 18 in those variables.

Python does not require us to declare the data type when creating variables, and there is no requirement to do so ahead of time.

For example:

Output: After executing the above code, we will get the output as shown below:

The output is:

Value of a = 15
Value of b = 18
Value of a = 15 and value of b = 18






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA