C Basic Syntax

Let’s have a quick glance at the basic syntax of C programming language. Syntax of C programming language is a set of rules, principles, and processes that govern the structure. C was the first widely successful high-level language to develop portable operating systems.

C Basic Syntax

C program has the following components (usually found in this order):

  • Preprocessor Commands
  • Type Definitions
  • Function Prototypes
  • Variables Functions
Structure of a C Program:
#include<stdio.h> //header
int main(void) //every C program must have a main function.The main function indicates the compiler to tell from where to start the program execution.
int a=1, b=2, sum=0; //variable declaration
printf("Sum of a & b=%d",sum);
return 0;

Header Files

Header files support your C program file, which includes descriptions and related variables of the various functions that must be imported into your C program using preprocessor #include statements. An extension with a C function declaration and macro-defines is applicable to all header files. In other words, a preprocessor directive #include may be used to request the header files. The stdio.h file with the C compilation is the default header.

Including a header file means that using the content of the header file in your source program. A straightforward practice while programming in C or C++ programs is that you can keep every macro, global variables, constants, and other function prototypes in the header files. The basic syntax of using these header files is:

Syntax of Header Fies:
#include <file>

Check List of Header Files in C.



.h file in the above program imports some external libraries/functions which required in our program .file contains functions such as scanf() and printf().


Keywords are predefined. Keywords are part of the syntax and they cannot be used as an identifier.


int var;

Here, int is a keyword that indicates var is a variable of type int (integer).

Return statement

return 0 the statement is the “Exit status” of the program.


return 0;



the semicolon is the terminator it is placed after every single line of code.


Single line comment


/*This is an example of single line comment*/

Comments are not read by the compiler it is used by the compiler to keep the track of the flow of the program written by him/her.

Multiline Comment :


//This is an example of a multi-line comment.

Variable Declaration

Variables are usually declared at the top of files and

#include <stdio.h>
int myglobalinteger;
int main() {
int mylocalinteger;
// do something


  • In C, a variable can be declared as constant.
  • The value of a constant is initialized when the variable is
    declared. That value cannot be changed.
  • An optimizing compiler can use the constant declaration
    to simplify and optimize the code.
    int const a = 1;
    const int a = 2;

Arithmetic Operations

  • C provides the basic arithmetic operations : + – * /
  • For efficiency purposes, it also provides an increment and decrement operator: ++ and —
  • The modulus (%) operator is also provided. Note that / operation for float and integer is very different. Unless both operands are float, the division will be integer-based.
float a, b;
a = 3.0 / 2; // a = 1.0
b = 3.0 / 2.0; // b = 1.5

Comparison Operators

C provides the following comparison operators:

== : equality
!= : not equal
< : smaller than
> : greater than
<= : smaller or equal than
>= : greater or equal than

Logical Operators

C provides the following logical operator:

&& : AND
|| : OR
! : NOT

These can be used with the comparison operators:

if ( (a == 5) || (a == 6) ) // a = 5 or 6
if ( (a == 3) && (b == 4) ) // a = 3 and b = 4
if ( !(a == 5) ) // a is not 5
if ( a != 5 ) // a is not 5

If statement

If statements in C are identical to if statements in Java.

if (expression) {
} else if (expression) {
} else {

? operator

The ? operator is a designed to replace small if
statements. Its syntax is as follows:

(expression) ? (statement if true) : (statement if false)

The following example calculates the absolute value of an integer.

int a, b;
a = some random int value;
b= (a > 0) ? a : -a;

Switch statement

A switch statement allows testing of a variable under multiple conditions:

switch(variable) {
case constant1:
case constant2:
case constant3: