C – Basic Program
We are going to learn a simple “Hello
World” C program in this section. Also, all the below topics are explained in
this section which are the basics of a C program.
1. C Basic Program:
#include <stdio.h>
int main()
{
/* Our first
simple C basic program */
printf("Hello World! ");
getch();
return 0;
}
Output:
|
Hello World!
|
Explanation for above C basic Program:
Let’s see all the
sections of the above simple C program line by line.
|
S.no
|
Command
|
Explanation
|
|
1
|
#include <stdio.h>
|
This is a preprocessor command that includes standard input
output header file(stdio.h) from the C library before compiling a C program
|
|
2
|
int main()
|
This is the main function from where execution of any C
program begins.
|
|
3
|
{
|
This indicates the beginning of the main function.
|
|
4
|
/*_some_comments_*/
|
whatever is given inside the command “/* */” in
any C program, won’t be considered for compilation and execution.
|
|
5
|
printf(“Hello_World! “);
|
printf command prints the output onto the screen.
|
|
6
|
getch();
|
This command waits for any character input from keyboard.
|
|
7
|
return 0;
|
This command terminates C program (main function) and returns 0.
|
|
8
|
}
|
This indicates the end of the main function.
|
Write a program in C Program to find area and circumference of circle
#include<stdio.h> int main() { int rad; float PI=3.14,area,ci; printf("nEnter radius of circle: "); scanf("%d",&rad); area = PI * rad * rad; printf("nArea of circle : %f ",area); ci = 2 * PI * rad; printf("nCircumference : %f ",ci); return(0); }
Output :
Enter radius of a circle : 1 Area of circle : 3.14 Circumference : 6.28
Explanation of Program :
In this program we have to calculate the area and circumference of the circle. We have following 2 formulas for finding circumference and area of circle.
Area of Circle = PI * R * R
and
Circumference of Circle = 2 * PI * R
In the above program we have declared the floating point variable PI whose value is defaulted to 3.14.We are accepting the radius from user.
printf("nEnter radius of circle: "); scanf("%d",&rad);
Comments
Post a Comment