C Syntax
Writing a C program in any other structure will hence lead to a Compilation Error.
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Line 1 : Header Files Inclusion:
A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. Some of C Header files:
Syntax:
#include <stdio.h>
- stddef.h – Defines several useful types and macros.
- stdint.h – Defines exact width integer types.
- stdio.h – Defines core input and output functions
- stdlib.h – Defines numeric conversion functions, pseudo-random network generator, memory allocation
- string.h – Defines string handling functions
- math.h – Defines common mathematical functions
Line 2 : Main Method Declaration:
The next part of a C program is to declare the main() function. The syntax to declare the main function is:
Syntax to Declare the main method:
int main()
{}
Line 3 : Variable Declaration:
The next part of any C program is the variable declaration. It refers to the variables that are to be used in the function. Also in a C program, the variables are to be declared before any operation in the function.
Syntax:
int main()
{
int a;
Line 4: Body
The body of a function refers to the operations that are performed in the functions. It can be anything like manipulations, searching, sorting, printing, etc.
int main()
{
int a;
printf("%d", a);
}
printf() is a function used to output/print text to the screen. In our example it will output "Hello World".
Line 5: Return Statement:
The return statement refers to the returning of the values from a function. This return statement and return value depend upon the return type of the function.
Line 6:
Do not forget to add the closing curly bracket } to actually end the main function.