Complete C Program for Addition of Two Integers


Let’s start from the beginning, the addition of integers. I think you surely made this program already but you did’t done this in your program. If you do so, So Congrats!! you are going by the right way. Look at the program below and make out what you forgot to include in your program. And clear that bug. ►►

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b;

printf(“Enter the value of A and B\n”);

scanf(“%d %d”,&a,&b);

printf(“Sum = %ld”,a+b);

getch();

}

In the above program. I did not use an extra variable for showing the sum because I don;t need it further in the program so why to store it, I printed it out directly in the printf statement.One more thing make sure to use %ld format specifier for the output. Because the range of the integers is -32768 to 32767 but in many operations our output may cross the limit and it will result in unpredictable negative result. 

As the output of 20,000 + 20,000 will equal to 40,000. %ld is a format specifier for long integers. So when your output cross the limit of integer, it will show by the long integer range. 

In programming there is a rule that make minimum number of variables as to keep program size less and avoid extra lines of code(try to do in short). Also make ensure that by doing so you don’t loose the program completeness.

Truly saying it’s not a rule :D, it’s a convention but it is good to keep it as a rule for becoming a good programmer.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s