Understanding the program in C




1) /*
2) Program:
3) Addition of two numbers
4) */
5) //Here it starts
6) #include<stdio.h>
7) #include<conio.h>
8) Void main()
9) {
10) int a, b, c;
11) /*Assign the values to the variable a and b */
12) a = 5;
13) b = 4;
14) /*Perform addition of a and b */
15) c = a + b;
16) printf(“\nAddition of a and b is %d”, c);
17) }

Let us learn the above program in detail.
Line 1 thru 4:
This is the comments in the program. It is called multiline comment. The text written within /*...*/ are ignored by the compiler.
Line 5:
This is another way to comment a text or statement. It is called single line comment. The entire line following the tag // is ignored by the compiler.
Line 6 and 7: #include is known as pre- processor directives. It tell the compiler to include text from another file, stuffing it right into your source code.
is a file name enclosed within angle brackets. The whole statement #include tells the compiler to take text from the file stdio.h and stick it into your source code before the source
code is compiled.
Line 8:
This is the start of the main function. The program execution starts from this function. main() is the name given to a set of statements. This name has to be main(). The statement that belongs to main() are enclosed within a pair of braces {}.
Line 10: Any variable that we need to use must be declared before using it.
Line 12 thru 15:
Here the values are getting assigned to the variables.
Line 16:
Once the value of a+b is assigned to c, it needs to be displayed on the screen. We can use readymade library function to display the value on the screen. One such function is printf().
The general form of the printf() function is:
printf(“”,);
can contain,
%d for printing integer values
%f for printing real values
%c for printing character values
\n is a newline character. It takes the cursor to the new line. It is one of the several escape sequences available in C.
Line 9 and 17:
The function body should be enclosed in opening and closing braces.

Note:
Though comments are not necessary, it is a good programming practice to include comments in the program. Adding comments in the program increases the readability of the program.
WE can include as many comments as possible.
A comment can be split over more than one line as shown in the above program (Line 1 thru 4).
Any C statement always ends with a ;

Receiving Input
In the above program, we have assigned the value 5 and 4 to a and b respectively. But, every time we cant do so. A situation might arise wherein we need to take input from the user. Here we will learn how to receive input from the user.
scanf() – It is a standard library function in C. This function will accept the input from the user and stores it in a particular user defined variable.
The syntax of this function is:
scanf( “”, &);
can contain,
%d for printing integer values
%f for printing real values
%c for printing character values

‘&’ before the variable name is a must. ‘&’ is the ‘address of’ operator. It gives the address used by the variable in memory.

Let us consider an example
scanf(“%d”,&a);
When we execute this statement, it will ask for input. Suppose the input given is 5. 5 will be stored as integer at the address of variable a. we can print the value stored at that address using simple printf() statement as shown below.
printf(“%d”, a);

Example:
1) /*
2) Program to accept three numbers and prints the sum of these numbers
3) */
4) #include<stdio.h>
5) #include<conio.h>
6) Void main()
7) {
8) int a, b, c;
9) /*Assign the values to the variable a and b */
10) scanf(“%d %d”, &a, &b);
11) /*Perform addition of a and b */
12) c = a + b;
13) printf(“\nAddition of a and b is %d”, c);
14) }


Responses

1 Respones to "Understanding the program in C"

John kerry said...

It is fascinating to join such a forceful and genuine discourse with perusers of this blog and additionally perusers of different web journals or sites who truly thing that they can express more identified with this theme. Buy Refurbished monitors


August 14, 2020 at 11:14 AM

Post a Comment

 

Total Pageviews

Return to top of page Copyright © 2011 | Kuppam Engineering College Converted into Blogger Template by Mohan Murthy