First Program

Question: Write a C Program to take input for two numbers to calculate and print their sum?

/*
Sum of two numbers
*/
 #include<stdio.h>
 #include<conio.h>
 int main()
 {
     int a,b,c;
     clrscr();
     printf("Enter 2 nos ");
     scanf("%d %d",&a,&b);
     c=a+b;
     printf("Sum = %d\n",c);
    getch();
    return(0);
 }

Explanation Of above Program

Line :1,2 and 3

/*
Sum of two numbers
*/

These are remark statements that help to specify the objective of the program in hand. (this is optional but a good habit to write)

Lines: 4 and 5

#include<stdio.h>
#include<conio.h>

These are header files. These contain details (prototypes) of the function used in the program. if we use any function in the program whose details are not present in the included header files then an error will get generated.

What does header files contain?

A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that come with your compiler.

Line 6:

int main()

Each and every C program is made up of functions. there can be any number of functions in a single program. but there has to be at least one function in each and every C program. and if there is only one function in the program then it will be the function named main(). As this is the starting point of execution of any “C” program. The program is compiled from top to bottom but the execution of the program starts from the function named main().

If we change the name of the function main() it will generate an error.
Various forms of main() function can be.

Various forms of main() function can be.

void main()  
{  
   Statements;  
}
int main()  
{  
  Statements;  
  return(0);  
}
main()  
{  
  Statements;  
  return(0);  
}
main(void)  
{  
  Statements;  
  return(0);  
}
void main(void)  
{  
  Statements;  
}
int main(void)  
{  
  Statements;  
  return(0);  
}

* If required arguments can be passed to the function main(). Such arguments are called “command-line arguments”.
* “command-line arguments” help us to develop our own utilities. Once developed they can be used as and when required.
* By default the return type of any function is “int”.

Line 7 and 15:

* This defines the body of the function main.
* This also defines the logical starting and ending point of the program.
* There can be any number of the statement in the body of the function.
* Each statement is terminated by ; (semicolon).”;” (semicolon) is known as a statement terminator.

* “C” language is a case sensitive language. i.e. Upper case and lower case characters are treated differently. i.e. “A” and “a” is different.

Line: 8

int a,b,c;

On execution of this statement, three memory location named “a” ,”b” and “c” are created in the memory and it is also specified that these can store numeric value without decimal as the data type is given as “int”.

Line: 9

clrscr();

* This function helps us to clear the screen( Works in Turboc3, but does not work in code blocks).
Header file : conio.h

Line:10

printf(“Enter 2 nos “);

printf():
* this function helps us to display any text, value of a variable and/or value of an expression on standard output device(screen/monitor).
* Header file: stdio.h

Line: 11

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

scanf():
* this function helps us to take input for value of a variable through standard input device(keyboard).
* header file : stdio.h

%d: format specifier for “int”
& : address of

So the above statement says, take input for two values of “int” type and store them in the address of variables “a” and “b”.

Line : 12

c=a+b;

on execution of this statement sum of values stored in variables “a” and “b” is assigned to variable “c”.

Line :13

printf(“Sum = %d\n”,c);

displays the result as
(if values entered are a=10 and b=20)
Sum = 20
_

“Sum =”
is displayed as it is and value of variable “c” is displayed in place of “%d” further the cursor is taken to next line as ‘\n’ new character is used.

Line:14

getch();

this function helps us to take input for a single character and it is written at the end (in turboc3) so that after the execution of all the statements the computer should wait for a single character to be pressed and we can easily see the output.
We can also use Alt+F5 combination keys to view the output of the program after the execution of the program.

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects