Advice On Computer Concepts - Programming Using C : Lesson 4
posted on 08/04/2009
In this article we will see our first C program.
The first C program - Hello Advice.Com
#include <stdio.h>
void main (void)
{
printf ("\n Hello Advice.Com");
}
As simple as it goes. Our first program did not take much to write and is very simple to understand as well. We have included stdio.h as we wanted to make use of printf. The only new thing to learn here is the \n that is used in the printf. It is the newline character and will print this on a new line in the console.
C program for Addition
In this program we will go a step ahead and try to declare some variables and you them for some purpose. An addition program might not be very interesting but we have to deal with it since it highlights powerful concepts in simple terms.
#include <stdio.h>
void main (void)
{
int num1, num2, sum;
printf ("Enter first number : ");
scanf ("%d", &num1);
printf ("Enter second number : ");
scanf ("%d", &num2);
sum = num1 + num2;
printf ("\n The sum of %d and %d is : %d",num1,num2,sum);
}
C allows us to use variables only after declaring them. In this example we have declared 3 variables. num1, num2 and sum are all declared as integers. Then we get 2 values in num1 and num2 using scanf. These are input statements. Then we do a processing. We find sum as addition of the numbers. Finally we print it all of them out. Below we see how the program is used.
Sample Output
-----------------------
Enter first number : 5
Enter second number : 3
The sum of 5 and 3 is : 8
Now that we have had some good introduction to see we will continue with some powerful programs, file operations, data structures and graphics in the upcoming articles.



Comment on this article
You must be logged in to post comments.
Previous Comments