Friday, 31 August 2012

Chapter 4 - Implementing the Int(integer)

Yes its Int and not IMP.
Int Data Type is one of the commonly used data types and used to store integer values. In the previous chapter i explained to you what an integer can store with examples. So by now it is clear what kind of data we want to store in our first int variable. So lets get coding.

There are 3 steps in using a variable in any program.Variable declaration, where the compiler is told you are declaring a new variable of type INT.
  1. Variable initialization, where you store some value in the variable.
  2. Actual use of variable.
Example: The example code with give u a clear picture of how the three steps are used in the program and how we ca store and retrieve values from our (int) variables.
/*  
 Author : Ryan Sequeira  
 Date : 31st August 2012  
 Title : prints the value of a variable  
*/  

#include<stdio.h>  
#include<conio.h>  

void main ()  
 {  
  int x;  
  x =0;  
  clrscr();  
  printf("The value of x is : x");  
  getch();  
}    
Download code

Line 1 is implements variable declaration.
Line 2 implements variable initialization.
Line 3 makes use of the value of x .

Run the code before you read further.
As you may have noticed this code doesn't print any number. This is because x is treated as a part of a sentence and not as a variable. Hence we need to tell the compiler to reserve that space for an integer which is done by using a special word which is %d . So now the actual printf statement is

 printf("The value of x is : %d", x);    

This can be translated as, reserve space for a int variable whose name is x. So the new code becomes:
/*  
 Author : Ryan Sequeira  
 Date : 31st August 2012  
 Title : prints the value of a variable  
*/  

#include<stdio.h>  
#include<conio.h>  
void main ()  
 {  
  int x =0;  
  clrscr();  
  printf("The value of x is : %d", x);  
  getch();  
 }
Download code

You ca see that i have made a few changes and here are some good practices that i want you all to follow to reduce errors. As you can see i have initialized the value of x where it is declared. The reason behind this is that if you forget to initialize your variable later it will create errors in your code and will take time to find the exact cause of error. Hence i recommend you to make a habit of initializing the variables when they are declared.

How to print Two int Variables ?
To print two variables we extend the same concept. Suppose that we have to variables

int x = 1, y =3;    

(Yes you can declare more than one variable of the same type is the same sentence but you have to use int keyword only once, i.e. at the start of the instruction)

So now the accompanying printf statement will be:

printf("The value of x is : %d \n The value of y is : %d", x, y);  

Note:

  • Changing the order of x and y in the print f statement will change the answer.
  • There number of %d should match with the number of integer variables.
  • The order in which different types of variables used inside " .... " must be the same outside. (You will understand what i mean later when we make use of different data types).


So the final code is:
/*  
 Author : Ryan Sequeira  
 Date : 31st August 2012  
 Title : prints the value of 2 variables in one printf  
*/  

#include<stdio.h>  
#include<conio.h>  

void main ()  
 {  
  int x =10, y=20;  
  clrscr();  
  printf("The value of x is : %d \n The value of y is : %d", x, y);  
  getch();  
 }
Download code
Activities

  1. Try to change the value of x after the first printf and print the new value.
  2. Try to print two or more variables each in a different printf.
  3. Try to print more than one variables in the same printf.

No comments:

Post a Comment