BASIC C PROGRAMS
1.) Program to print HELLO WORLD.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“HELLO WORLD\n”);
getch();
}
Before understanding C program we must understand some rules of C language -:
- Each statement must be written in separate line.
- Program should be written in lowercase letter.
- Statement must be written in order in which we want them to be executed.
- After statements we must use semicolon(;). It act as statement terminator. It tells compiler that this statement is finished , proceed to next one.
Let’s understand this program step by step-:
1st part-:
#include(standard input output function)
#include(console input output function)
These are statements which tell compiler to insert contents of stdio at particular place.
#include is a preprocessor and stdio.h is called header files . We will study these later in deep. For now we must understand that-:
These files are responsible for all input/output statements. printf() , scanf() ,getch(), clrscr() and other statements run by function of these header files.
These statements run before the compilation has started .
2nd part-:
void main()
It’s a function . Function contain a set of statements. We must note that main() function is basic function while designing a program in C. It’s most important function , without it our programming is of no use . As said above function contain set of statements , here main() function contain all programming statements . Let’s understand by simple example in practical life .
Suppose many students are sitting in a room and its raining outside , Here room is acting like a main() function and all students are like statement which are nothing without room in rain .
Like we input numbers in calculator and it returns value , in same way main function return value in some compilers and does not return any value in some compiler . Here we have assumed that main function is not returning any value therefore void main is written. If main does return any value than we should write int main.
int main() ,when return integer value.
void main() , when return null value.
3rd part-:
{
clrscr();
printf(“HELLO WORLD\n”);
getch();
}
This is body of main function. In this all programming statements are written .
->{}
Indicate beginning and ending of main() function and statements enclosed in it .
->clrscr();
It is another function which is used to clear all outputs of program made earlier. If using compilers like Turbo C, Turbo C++ its good practise to use clrscr function.
->printf(“HELLO WORLD\n”);
As studied above #include statement is used to operate all input output statements . Here printf() is output function or we can say that any thing we want output is enclosed in this function . Here we want to print HELLO WORLD on output screen therefore printf(“HELLO WORLD\n”) is written . This is operated by #include statement.
->getch();
Working in Turbo C , Turbo C++ make a problem , there output screen not stay or we can say output is just visible for seconds and scree disappears. For overcoming this probem we use getch function . This function hold output screen as long as user want to .
Next we will study about learning program to sum two numbers.
2.)FINDING SUM OF TWO NUMBERS-:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,m,sum;
printf(“Enter the values of two numbers\n”);
scanf(“%d%d”,&n,&m);
sum=n+m;
printf(“Sum of two numbers is %d\n”,sum);
getch();
}
Here we need to find sum of two numbers which let be n and m .We need to declare n and m before using it because compiler will not understand what exactly m and n are . When we write int n,m,sum; , we are telling compiler that n, m and sum are three variables which are storing integer value . Then we need value of that two numbers. We can declare as int n=10,m=20; in declaring statement, but If we want to input different value at different time we execute it than we use scanf().
Here scanf(“%d%d”,&n,&m); is written . This function allow user to input values of declared variables when program run. %d represents part of memory as an integer value .
We must note that %d indicate integer , %f represent float values . For eg. if float n,m was written than we write scanf(“%f%f”,&n,&m); .
&n,&m represent address of value n and m . In memory value of n and m are stored at a particular address , when we &n,&m we are telling about address of n and m not the value.
scanf() function is used to take input from user .
Steps of Running of program-:
- First header files run ,then compiler enters in main function.
- Here first statement int n,m,sum; , in this variables are declared which are going to be used in Program.
- Then printf(“Enter the values of two numbers\n”); , in this we are telling compiler to print the statement .
- When statement ‘ Enter the values of two numbers’ is printed , user enter the value from keyboard which are operated by statement scanf(“%d%d”,&n,&m); .
- After that sum=n+m; run, in this compiler add two numbers entered by user .
- Then printf(“Sum of two numbers is %d\n”,sum); run, In this compiler is told to print sum of two number which is stored in sum .
- program ends.
Leave a comment