Using datatypes and variables

Let us write a simple program for using data types, which were explained in the previous few posts. Have a look at the simple program to add 2 numbers.

Code
#include<stdio.h>
main()
{
/* Declaration of Variables (Input) */
int iSum;
int iNum1=23;
int iNum2=33;

/* Processing Input */
iSum = iNum1 + iNum2;

// Display Output
printf("Sum of %d and %d is %d\n",iNum1,iNum2,iSum);

/* Changing inputs */
iNum1 = 10;
iNum2 = 23;

/* Process Again */
iSum = iNum1 + iNum2;

// Display Output Again
printf("Sum of %d and %d is %d",iNum1,iNum2,iSum);

getch();
}
The above program introduces many new things to you. First thing is Comments. To make the program look more understandable, we can use comments to describe certain blocks of code. So we use comments. Comment Lines are ignored by compiler while compiling. There are two types of comments - Single line comment and Multiline comment. Single Line comment can be made by starting the line with // and multiline comment can be made by starting the comment with /* and ending the comment with */

Next thing that is introduced here is variables and data types. Recall the third paragraph in this Post. We can store data/values in memories and can give those memory locations as input to the add function. So if we need to change the value for which we need to find sum, we simply change the value in those memory location and call the sum function with the same address location.

But as explained earlier, C program makes things easy for us. We don't need to specify where to store our data. It simply finds a free memory location and puts our value in it. So how do we know where our value is stored ? How can we change the value in that memory location ? That is where Variables comes into picture. We just name the memory location (whatever it might be). We use that variable name to identify the memory location. Internally C will map the variables names to the memory location using a Table maintained by it.

Thus when we DECLARE a variable(int iSum;), a new memory location is allocated and the address is mapped to the variable name iSum. So even without knowing the memory location, we would be able to change the values in that memory location. Hence the name "Variables". Now when we INITIALIZE a variable(int iNum1=23;), inaddition to declaration, the program also stores the value 33 in the memory allocated.

Now I know you will have a question. What will be present in the Variable which is not initialized? At any point of time, a memory location will have random combination of 0s and 1s or some value which was previously stored during execution of someother program. So it will contain some random number which we can't guess. We call those values GARBAGE VALUES.

Ok I have said it will allocate memory for the variable. But how many bytes of memory will it allocate ? Now comes datatype into picture. As explained here, Int means integer and the memory size for it is 4 bytes (depending on the compiler/system). So when we specify "int a", 4 bytes of memory is allocated. (Now I have a question again, have a look at the next post for what it is)

Now the statement for processing the input(iSum = iNum1 + iNum2;), It is something like 2E [2000] [2004] (as explained here). Instead of giving the memory address [2000],[2004] we specify the variable names. Instead of specifying 2E, we specify the operator +. Now we can also specify where to store the result through iSum =

Now the output will be something like this

Sum of 23 and 33 is 56
Sum of 10 and 23 is 33

Let us see about the statement Printf in a new Post.

0 comments:

Post a Comment

 
Template designed using TrixTG