If you are starting with C programming, this is usually the first code to run. It confirms that the build process and code execution are working. Execution of a C program starts from the main() function. The printf() function writes the string “Hello World” to standard output, in this case the screen, and \n moves the cursor to the next line.
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
Output:
Hello World
Observe this second example to understand the use of \n. The printf() function prints the string “Hello World” first, then the cursor moves to a new line, and the second string “Hello Buddy” is printed on the screen.
#include <stdio.h>
int main()
{
printf("Hello World\n");
printf("Hello Buddy\n");
return 0;
}
Output:
Hello World
Hello Buddy
At the start of a C program, a #include statement is used. It is a preprocessor directive which allows inclusion of standard or user-defined libraries. In this case, the stdio.h header is included. It contains declarations of functions like printf, scanf, etc.
A function also returns a value after execution. Here, 0 is returned to indicate that the program executed successfully.
This is the minimum structure of a program.