Sunday, 6 October 2013

Int Data Type

Int Data Type

Int Data Type is one of the most commonly used data types and used to store integer values. They can be negative, zero or positive.
For example: 2, 56, -8663 etc.

Lets look at an example on how integers can be used.

 Author : Ryan Sequeira  
 Date : 22nd September 2012  
 Title : Calculate the age difference between two people


int main(){
    //declaring 2 variables and initializing it with 0 
 int a = 0, b = 0;
 //prompting to enter age of first person and storing it in a
 printf("Enter age of first person: ");
 scanf("%d", &a);
 //prompting to enter age of second person and storing it in b
 printf("Enter age of second person: ");
 scanf("%d", &b);
 //printing the age difference
 printf("The age difference is: %d", abs(a-b));
 return 0;

In this simple example you can see how variables are declared, initialized, re initialized with users value and finally used to calculate new results.

Here we have initialized the variables with 0. It is always a good practice to initialize variables with appropriate values. For example if a program will use the variables to add or subtract, 0 is the value to be used and for multiplication and division operations you can use 1.

We have used the abs function in this example. The abs function is used to calculate the absolute difference between 2 values i.e it returns only the difference and not the positive or negative sign.

Int in a nutshell
Description Used to store integer values. They can be negative, zero and positive.
Examples -45, 38, 0, 1445, -9753
Size 4 Bytes (may change based on system)
Range -32768 to 32767
Syntax int variable ( = value) [, var1 = val1, ...] ;
Format Specifier %d

No comments:

Post a Comment