Basic Structure of C Program
In this tutorial, our focus will be towards the structure of our written program. As a beginner the concept might be difficult for you to understand but do not worry as it happens to everyone, but with time you will develop a good grasp on it. We will be discussing some of the structural parts of our code, such as:
- Pre-processor commands
- Functions
- Variables
- Statements
- Expressions
- Comments
Now we will divide our written program into a few lines of code and one by one we will go over the meaning of each and every keyword in the specific line.
So, let’s begin with Pre-processor commands.
#include <stdio.h>
#include <math.h>
It helps us to use the code from math.h file for the calculations in our programs.int main()
this is the 2nd line. main() is function here and we will see the detail about the function in later tutorials. Here int is the return type of function and the return type is according to the functions activity i.e. if it is giving an integer variable as a result then return type should be int.int main()
here we are initializing two variables as integers. Initializing with integer means that we can store integer values in it. If we would have initialized them with char then we could have stored character values in it such as a, b, c, d, etc. printf("Enter number a\n");
This is simply a print statement. Whatever we write in the brackets will be directly printed onto the screen. /n is the new line character here.scanf("%d", &a);
scanf is used to take inputs from the user. Here &a gives the address of variable “a” to store the user's given value. %d notifies that the value should be of integer type.printf("The sum is %d\n", a+b);
Here a+b is simply a mathematical addition and print statement is printing the result onto the screen.return 0;
//This is a comment
gcc-Wall-save-temps file_name.c -o new_file
- file_name.i
- file_name.s
- file_name.o
- file_name.c
- new_file.exe
#include <stdio.h>
int main()
{
int a, b;
printf("Enter number a\n");
scanf("%d", &a);
printf("Enter number b\n");
scanf("%d", &b);
printf("The sum is %d\n", a+b);
return 0;
}
Code main.c as described/written in the video#include
int main()
{
printf("Hello World\n");
return 0;
}