VariablesIn 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 variablesThere are various key points that we need to remember before declaring the variables in the program. Some of them are as follows:
Creating VariablesIn 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 VariablesAs 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 variableIf 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 JavaHere, 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 pythonNow, 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 Next TopicHow to get Minecraft for free |