Lesson 2 : First C Program
Now Friends, that you are well equipped with the knowledge of keywords and identifiers, let us write a simple program to calculate the sum of two numbers entered by user.
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter two numbers");
scanf("%d %d " ,&a ,&b);
sum=a+b;
printf("Sum = %d ");
}
this program will have a output like this
Enter two numbers 10 20
Sum = 30
Here in this program you can see few things, let us look at them line by line.
1. The first line contains "#include<stdio.h>" this is known as preprocessor. It says the compiler to include library file "stdio.h". ".h" represents a header file. This file contains the details for functions like printf, scanf and other input-output functions.
2. The 2nd line have void main() this is where our compiler starts reading the program. Every C program must contain one and only one main() function because this is were the compiler starts reading and executing the program. The void before the main declares that the program will not return anything. We'll get to it later.
3. Every program is written within a pair of curly braces. This defines the area as well as the limit for a program.
4." int a,b,sum; " this is a declaration statement. It means that variable a, b and sum has been declared and all of them are integers.
5. printf is a output functions whose work is to display or we can say print the characters written within the pair of inverted commas.
6. scanf is another C function which will take the value entered by the user and store it in variable a, and b respectively.
7. "sum=a+b;" this is an arthematic expression defined by us. It tells the computer to add a and b and store its value in sum. In C, there are many arthematic operators , they are " +, -, * , / ,% ". You might be familiar with the first four operators but the last one you might not be knowing. Well, its called modulus operator. what it does is return the remainder of the divided number. For example "3%2" will give 1 as an answer.
8. I think you might now have probably guess what the last statement is doing. It is printing the value of sum on the output screen.
You might have noticed that after some lines i have put an semicolon (;), this represents a C statement. Every C statement ends with a semicolon. Its like ending a sentence with full stop. Try making a program that will subtract one number from other till next class.
Enjoy :)
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter two numbers");
scanf("%d %d " ,&a ,&b);
sum=a+b;
printf("Sum = %d ");
}
this program will have a output like this
Enter two numbers 10 20
Sum = 30
Here in this program you can see few things, let us look at them line by line.
1. The first line contains "#include<stdio.h>" this is known as preprocessor. It says the compiler to include library file "stdio.h". ".h" represents a header file. This file contains the details for functions like printf, scanf and other input-output functions.
2. The 2nd line have void main() this is where our compiler starts reading the program. Every C program must contain one and only one main() function because this is were the compiler starts reading and executing the program. The void before the main declares that the program will not return anything. We'll get to it later.
3. Every program is written within a pair of curly braces. This defines the area as well as the limit for a program.
4." int a,b,sum; " this is a declaration statement. It means that variable a, b and sum has been declared and all of them are integers.
5. printf is a output functions whose work is to display or we can say print the characters written within the pair of inverted commas.
6. scanf is another C function which will take the value entered by the user and store it in variable a, and b respectively.
7. "sum=a+b;" this is an arthematic expression defined by us. It tells the computer to add a and b and store its value in sum. In C, there are many arthematic operators , they are " +, -, * , / ,% ". You might be familiar with the first four operators but the last one you might not be knowing. Well, its called modulus operator. what it does is return the remainder of the divided number. For example "3%2" will give 1 as an answer.
8. I think you might now have probably guess what the last statement is doing. It is printing the value of sum on the output screen.
You might have noticed that after some lines i have put an semicolon (;), this represents a C statement. Every C statement ends with a semicolon. Its like ending a sentence with full stop. Try making a program that will subtract one number from other till next class.
Enjoy :)
Comments
Post a Comment