Class: 10 Introduction to Computer Programming --- Complete Course for BSSE and BSCS Students Specially

 



Store Values in Variables

You have seen how we created variables in the previous section. Now, let's store some values in those variables −

#include <stdio.h>

int main() {
   int a;
   int b;
   
   a = 10;
   b = 20;
}

The above program has two additional statements where we are storing 10 in variable a and 20 is being stored in variable b. Almost all the programming languages have similar way of storing values in variable where we keep variable name in the left hand side of an equal sign = and whatever value we want to store in the variable, we keep that value in the right hand side.

Now, we have completed two steps, first we created two variables and then we stored required values in those variables. Now variable a has value 10 and variable b has value 20. In other words we can say, when above program is executed, the memory location named a will hold 10 and memory location b will hold 20.

Access stored values in variables

If we do not use the stored values in the variables, then there is no point in creating variables and storing values in them. We know that the above program has two variables a and b and they store the values 10 and 20, respectively. So let's try to print the values stored in these two variables. Following is a C program, which prints the values stored in its variables −

#include <stdio.h>

int main() {
   int a;
   int b;
   
   a = 10;
   b = 20;
   
   printf( "Value of a = %d\n", a );
   printf( "Value of b = %d\n", b );
}

When the above program is executed, it produces the following result −

Value of a = 10
Value of b = 20

You must have seen printf() function in the previous chapter where we had used it to print "Hello, World!". This time, we are using it to print the values of variables. We are making use of %d, which will be replaced with the values of the given variable in printf() statements. We can print both the values using a single printf() statement as follows −

#include <stdio.h>

int main() {
   int a;
   int b;
   
   a = 10;
   b = 20;
   
   printf( "Value of a = %d and value of b = %d\n", a, b );
}

When the above program is executed, it produces the following result −

Value of a = 10 and value of b = 20

If you want to use float variable in C programming, then you will have to use %f instead of %d, and if you want to print a character value, then you will have to use %c. Similarly, different data types can be printed using different % and characters.

Variables in Java

Following is the equivalent program written in Java programming language. This program will create two variables a and b and very similar to C programming, it will assign 10 and 20 in these variables and finally print the values of the two variables in two ways −

public class DemoJava {
   public static void main(String []args) {
      int a;
      int b;
   
      a = 10;
      b = 20;
   
      System.out.println("Value of a = " + a);
      System.out.println("Value of b = " + b);
      System.out.println("Value of a = " + a + " and value of b = " + b);     
   }
}

When the above program is executed, it produces the following result −

Value of a = 10
Value of b = 20
Value of a = 10 and value of b = 20

Variables in Python

Following is the equivalent program written in Python. This program will create two variables a and b and at the same time, assign 10 and 20 in those variables.

Python does not want you to specify the data type at the time of variable creation and there is no need to create variables in advance.

a = 10
b = 20
   
print "Value of a = ", a
print "Value of b = ", b
print "Value of a = ", a, " and value of b = ", b

When the above program is executed, it produces the following result −

Value of a =  10
Value of b =  20
Value of a =  10  and value of b =  20

You can use the following syntax in C and Java programming to declare variables and assign values at the same time −

#include <stdio.h>

int main() {
   int a = 10;
   int b = 20;
   
   printf( "Value of a = %d and value of b = %d\n", a, b );
}

When the above program is executed, it produces the following result −

Value of a = 10 and value of b = 20
Research Resources: Internet -- I am not owner of any thing!

Post a Comment

Previous Post Next Post