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.h
in the program. This header file contains declarations for standard input/output functions likeprintf
andscanf
.int main() {
: This line is the main function definition in C. Themain
function is the starting point of any C program, and the program’s execution begins from the first statement inside this function. Theint
in front ofmain
specifies the return type of the function, which is an integer. The empty parentheses indicate that themain
function does not accept any parameters.printf("Hello, world!");
: This line uses theprintf
function to print the string “Hello, world!” to the standard output. Theprintf
function is declared in thestdio.h
header file, which is included in the program in the first line.return 0;
: This line is the return statement of themain
function. It returns an integer value of 0 to the operating system, indicating that the program has completed successfully. Thereturn
statement is optional in themain
function, 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.