In this article, I’m gonna discuss the different parts of C Program with an Example. So keep reading this article till the end.
In order to clear all basics of C Programming you can visit my All in one C Programming course.
Explanation of C program with Example
C program Example:
printf(“Welcome to www.dheerajpatidar.com”);
Look at the above example, and understand each of part of C program using the all the stuff mentioned below.
First of all have a look at all the parts of C Program in short:
- Header File Inclusion
- Main Function
- Return Statement
- Printf() function
- Scanf() function
Different parts of C Program
1. Header File Inclusion:
As you see in above example, we have #include <stdio.h> in first line.
Here in this part of the C Program, #include is the predecessor directive, used to include or import header files like stdio.h, conio.h, string.h, and many more.
stdio.h is the header file used to enable the standard input output functions. All the Input-output functions have written in this header file.
Not only this file, In order to use more functionalities, there are lots of header files in C like graphic.h header file for using predefined graphics stuff.
2. main function
The main function of the C Program play very important role. The Execution of any C program starts with the main function.
We can use the other functions of c program by calling them into the main function.
The main function may contain any number of statements. These statements are executed sequentially in the order in which they are written.
In C, the function prototype of the ‘main’ is one of the following:
int main(); //main with no arguments
int main(int argc, char *argv); //main with arguments
3. return statement
As we have seen in above example, the main function return something which is 0. Now why we use this?
Basically, return statement is used to indicate the complete execution of the main function or program.
If in case main function doesn’t return anything that means something is going on wrong.
In addition, return statement can specify a value to be returned by the function. A function may contain one or more return statements.
We should have equal number of parameters in function declaration, if we want to return 1 or more value.
We can also use only return;, in case, where we use void return type.
4. printf() function
printf() is the standard input/output function of stdio.h header file, used in order to take input & print output to the console.
In case of printing output we only have to pass the String or any variable.
printf() function uses following formate specifiers in order to print anything on console:
- %c for a character value
- %d for an integer value
- %f for floating-point values
- %s for a sequence of characters or string
char str = “Hello World”;
5. scanf() function
scanf() function of the C Program is used to store values entered by user in variables.
In order to store values in variables, we also use all format specifiers in scanf() function.
scanf() accepts values through the control sequences placed within its two double quotes and stores them in the corresponding variables.
scanf(“%d“,x); //accepts integer values, given x is an integer
scanf(“%d%d“,&a, &b); //accepts Two integer values, given a and b is are integers
Program to display Information Entered by user
int x, y; //Declaration of x and y
printf(“Enter 2 integer numbers”); //Prints a message on a screen
scanf(“%d%d“, &x, &y); //Accepts 2 integer inputs, stores in x and y
printf(“x = %d and y = %d“, x, y); //Prints the value of x and y
This is all about Different Parts of C program. If you have any query about this article, then you can ask me through comment.
You can visit All in one C Programming Course which I have publish on this blog.
I hope you have got useful content here.
Thanks for visit!
< Previous Post
Next Post >