Let’s take a look at a basic example of C syntax:
#include <stdio.h>
int main() {
printf("Hello, world!");
return 0;
}
#include <stdio.h>: This line is a preprocessor directive that includes the standard input/output header filestdio.hin the program. This header file contains declarations for standard input/output functions likeprintfandscanf.int main() {: This line is the main function definition in C. Themainfunction is the starting point of any C program, and the program’s execution begins from the first statement inside this function. Theintin front ofmainspecifies the return type of the function, which is an integer. The empty parentheses indicate that themainfunction does not accept any parameters.printf("Hello, world!");: This line uses theprintffunction to print the string “Hello, world!” to the standard output. Theprintffunction is declared in thestdio.hheader file, which is included in the program in the first line.return 0;: This line is the return statement of themainfunction. It returns an integer value of 0 to the operating system, indicating that the program has completed successfully. Thereturnstatement is optional in themainfunction, but it is good practice to include it.
The main function and the printf function are essential parts of any C program, and understanding how to use them will help you get started with writing your own C programs.