C Basic Syntax

So far, we have already seen the basic syntax of C program in the previous Hello World program. Every piece of C code maintains the basic syntax probably. Let’s break down the basic syntax of C program that helps you to avoid syntax error while writing code in C.

Every programming language has its own basic syntax. The basic syntax is the base of the building block of source code of a programming language. Like all other programming languages, C follows some basic syntax and based on the C compiler design.

c basic syntax

If you avoid a single basic syntax rule, then the compiler won’t compile your source code. Because making a syntax mistake does not make any sense to the compiler.

In this lesson, we will learn the most basic syntax of the C programming language. It will make you aware of appropriating the basic syntax while writing source code in the C program. The length of this lesson may be long. Because we will discuss in details

Basic C Program Structure

In general, in a C program source code, most basic components are header files, the main function with the return type, code for execution in the main function, other function and self-defined function after the main function. The raw structure below.

header files1
return_type3 main( function parameters)2  {
code for execution;4

return parameter;5
other function/ self-defined function goes here;6

The above raw syntax is not clear, right? Ok, let’s use the raw syntax to implement real C syntax.

#include <stdio.h>  int main(void) {
    int x = 10, y = 20, total;
    total = x + y;

    printf("Total is: %d", total);

return 0;


programming Basic Syntax

Now compare the c program with the above. Let’s discuss all of the basic syntaxes now:

Header files1

C header files are the library functions defined in the specific module with a specified name. For example, in the above example, we used the header file “stdio.h”. It is the header file for basic input/output function like printf used in the example above.

The “stdio.h” file also defines the main() function of a c program. That means, if you want to use the main function and printf function for displaying output then you must use the “stdio.h” header file. But the main function is an essential part of a C program for executing. So including the “stdio.h” header file is a must.

There is some other header file also exists in C like “math.h”, “limits.h”, “signal.h”, etc. Every header file has some predefined function. When we need a specific function we basically include the corresponding header file,

The syntax for including header files in C:

#include <header_file_name>


#include <stdio.h>
#include <math.h>
#include <limits.h>

Note: A single source file can contain multiple header files like the above. Header files must be placed at the top of the code.

The main function2

Every C program must have the main function for final execution. Even all the other codes outside of the main function finally called by the main function and also executes by the main function. The syntax of the main function in C program:

int main() {
    /* All the codes here part of
    the main function */

    return 0;

The above syntax is the definition of the main function. Here int indicates the return type3, main() is the function definition, and the curly brac pair contains all the code of main function.

Return type3

The return type of C is defined before implementing the main function Like int main(), void main(), int main(void), float main(), etc. Here the int, void, float is the return type. It means, what kind of data will return the main function.

Basically in the C program, when we use int main() then the main function returns the integer value. And for the absence of function parameter, at the end of the main function we use return 0. So keep in mind that when you will use int return type then you must write the return 0; statement before the end of main function.

void main() indicates hat by default the main function has no return type and function parameter. If you have no function parameter in your main function then you can use void return type for your main function but in this case, return 0 statement not required. Because by default it does not return any value.

Semicolons in C;

Semicolon after every statement is very important in C programming. In C program, every statement ends with a semicolon ( ; ). We will learn more about statement is a later lesson.

In case if you avoid a semicolon after ending a statement, the compiler won’t understand the endpoint of the statement. And that’s why the compiler gets back with an error. This kinds of error called syntax error.

Whitespaces in C

In C programming all the whitespaces are ignored by the C compiler expect spaces in a string. In C programming, whitespace means an empty line, tab, single spaces, newline, etc. These all are ignored by the C compiler.

But the only exception is the whitespaces in a string is not ignored by the compiler. We will learn more about whitespaces in later lessons.

« Previous Next »