In the previous article, I have written simple C program. Will try to understand the code line by line.
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
#include<stdio.h>
#include is a preprocessor directive. It will include the file which is given within the angle brackets into the current source file. stdio.h is a file, which come along with any C compiler. It has valid C source statements. It is a standard input and output library, which has the necessary information to include the input, output related function in our program, such as printf, scanf. So, if you want to use these functions, then you must include stdio.h in the source code.
stdio.h is a system file. We have not written this file content. So, need to use the parenthesis <>. So that the compiler will know this is a system file and it will search the file in a specific location.
main()
This is the entry point of a C program. It is a point at which execution of a program starts. main is a special function in C that gets invoked when you run the program. Inside a function you can write block of code.
Syntax of function is:
returnType FunationName()
{
Write valid C code here.
}
returnType can be void, int (Will see other return type in later part of application)
void : This method does not return.
int : This method returns integer value.
sample main functions
void main()
{
}
int main()
{
return 0;
}
printf(“Hello World”);
printf is a predefined function, which is declared in the file: stdio.h I think now you might have clear that why we have written #include<stdio.h> since we are using the function printf. This function name is case sensitive. You have to write in lowercase only. This function is used to print the given statements to the console.
; Semicolon are end statements
return 0;
The function is returning zero. This is the end of the function.
You can also write main function with void statement. Use the below code. It works fine. Meaning of void means: function will not return any value.
void main()
{
printf(“Hello World”);
}