Basic Structure of C Program
Some structural part of the code
- Pre-processor commands
- Functions
- Variables
- Statement
- Expressions
- Comments
Code
#include <stdio.h>
int main()
{
int a, b;
printf("Enter no. a\n");
scanf("%d", &a);
printf("Enter no. b\n");
scanf("%d, &b");
printf("The sum is %d\n"; a+b);
return 0;
}
<stdio.h>
- #include <stdio.h>→ Preprosessor command
- Tells the compiler to include a file named 'stdio.h'
- Any line starting with # represents a preprocessing command.
- It tells our program that before its execution, it must include the stdio.h named file in it because we are using some of the commands or codes from this file.
- For example, if our program needs mathematical operations of high level then we must include: #include <math.h>
- It helps us to use the code from math.h file for the calculations in our programs.
int main()
- This is a function
- We use function to distribute a program in parts.
- Its a special type of func. from where the execution of program starts
- Every function has a return value → Its a (int) integer which main function will return
- → Whenever we call any function so we get a returning value
- → Now the 'main' function calls another functions and those functions returns a value which is used by main function
int a, b;
- Here we are initializing two variable 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 no. a\n")
- This is simply a print statement
- Its another function which we are using from stdio.h header file, we included 'stdio.h' just to use printf function.
- By using this function we can print desired output in console.
- '\n' is 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.
return 0;
- we need a return value for a function.
- The function we created was of int type so it is returning 0.
- Return 0 means that the function is working perfectly.
- Note: Return statement and function type should be same.
//This is a comment
- We write comments by using the double slash sign (//).
- Comments are to notify other programmers the working of the code at specific intervals or we write them for our-self.
- They do not have any effect on the written program.
The steps which are taken by compiler to compile a program
Preprocessing → compilation → assembly → linking
1- Preprocessing → Here the comments are removed from the program.
all the #include files which are written in a program there content are placed in separate file
→ macros expand
→ the preprocessed output is stored in '.i' file
2- Compilation → '.i' file is converted into assembly instruction and saved as '.s' extension
3- Assembly → '.i' file where assembly language is written should be converted into machine level instruction so that CPU can understand, '.o' extension file can't be opened.
4- Linking → All the assembly files are sumed up and made into a executable file.