Basic syntax of C


August 23, 2021, Learn eTutorial
1643

In this tutorial you will master the basic structure of C programs in a precise manner with examples.

Basic Syntax of C Program

Let’s start with the Hello World program which  has been traditionally illustrated as the basic program. If you run this program it just displays a message “Hello World”. This program is very useful in understanding the basic programming structure of any language.

Syntax of C Language

Comments in C

The 1st line of the above program is called comment. Comments are texts enclosed inside     '/*' and ' */' to make the code more readable.Here in our case from the first line itself we understand, this is a ‘Hello World Program’. If you write anything between ' /*' and ' */'it will not be executed, rather compiler will overlook whatever is written between these two notations. The comments section is not for the front-end user but the programmers. It helps them to keep track of every section of the program without any interference with the programming structure.Since comments are not a programming statement they can appear anywhere in the program but are optional.

In C we can write comments in two ways:

  • Single line  comments : are comments that begin with // and its life exists only in one line and ends when the cursor moves to the next line.
  • Multi-Line comments : are comments that start and end with /* and */ respectively. it can be a single line or multi line.

Preprocessor Directives

In C it is mandatory to begin a program with preprocessor directives as these directives contain files which  performs some specific functions.In the second line of the coding, '#include' is the preprocessor directive which tells the compiler to read the contents of the source file (here stdio.h) which is mentioned within '<' and '>' notation. To be specific a  preprocessor directive calls the header file.

The stdio stands for 'standard input-output'. It is the header file, which always ends with .h extension. In this file coding of some library functions are already written. For example, to display something you just have to use the command 'printf', nothing else because the coding of the entire 'printf' function has been pre-written in stdio.h.

You can place more than one header file in a program depending on the necessity. For  instance you can add a math.h header file along with stdio.h  if you want to perform some arithmetic and logical operations.


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

Functions in C

main() is a mandatory part of a program. Compiler executes only those commands which are written inside the body of the main function. So each and every C program must contain a main function. As you can see, it is written as main() and everything inside it must be written inside a starting brace '{' and closing brace '}'. Simply you can say that in the C compiler's source code it is predefined that when it will find 'main', it will start executing whatever is written inside this.

'printf' is another function which is already defined in 'stdio.h'. In this library function's coding, it is already written to display whatever is written inside the (). However, the compiler will only print the matter written inside “ commas and neglect the rest. If you write printf(“hello” world), the display will show only 'hello'.

Statements in C

Statements are instructions to the compiler when a program starts to execute. A statement in C must always  end with a semicolon as it tells the compiler that the statement reaches its end.

Finally the output of the above program is just the text inside the printf function. That is nothing other than Hello World.