Chapter 2: Syntax and Semantics
PART 1: SYNTAX (Form of expressions, statements and program units)
What is Syntax for C Language?
We know that smallest individual unit in any spoken language like English is its alphabet. We use alphabets to create words, words to create sentences then we can finally communicate using sentences.
Similarly in C Language we have lexemes, lexemes are smallest individual unit in C language. The C program is collection of various Tokens, tokens can also be called a class/category of various lexemes.
To follow right syntax in c language we need to follow all the rules of use and declaration of tokens.
While using any programming language we also deal with many other important aspects like whitespace, header files, preprocessors, comments etc…
Thus, we must follow all the rules for declaration and use of every aspect (including tokens) used in our program. Some basic syntax rules for C Language:
Structure of C program is defined by set of rules called protocol, to be followed by programmer while writing C program. All C programs are having sections/parts which are mentioned below.
1. Documentation section
2. Link Section
3. Definition Section
4. Global declaration section
5. Function prototype declaration section
6. Main function
7. User defined function definition section
Documentation section: We can give comments about the program, creation or modified date, author name etc in this section. The characters or words or anything which are given between “/*” and “*/”, won’t be considered by C compiler for compilation process.These will be ignored by C compiler during compilation.
Example :
/* For including any kind of Comments inside your code*/
Link Section: Header files that are required to execute a C program are included in this section.
Definition Section: In this section, variables are defined and values are set to these variables.
Global declaration section: Global variables are defined in this section. When a variable is to be used throughout the program, can be defined in this section.
Function prototype declaration section: Function prototype gives many information about a function like return type, parameter names used inside the function.
Main function: Every C program is started from main function and this function contains two major sections called declaration section and executable section.
User defined function section: User can define their own functions in this section which perform particular task as per the user requirement.
PART 2: SEMANTICS (Meaning of expressions, statements and program units)
1. Comments: Comments are not required, but the comments are as important as code. So it is first rule that you should remember to comment/document the code properly.
What you can comment:
Example:
//this line is for commenting
/* This
is
a multi-line Comment.*/
2. Whitespaces: In a C program, whitespace is generally a blank line. The C programming language uses whitespaces to describe blanks, newline characters, and comments.
Whitespace separates one statement from another statement for the compiler to identify.
For example in this statement:
int data;
If you don’t provide proper whitespace between "int" and "data", then the compilation will not be correct and you will not be able to get the desired output.
3. Preprocessor: In simple terms, a C Preprocessor is just a text substitution tool and it instructs the compiler to do required preprocessing before the actual compilation. All preprocessor commands begin with a hash symbol (#).
It must be the first nonblank character, and for readability, a preprocessor directive should begin in the first column.
Some commonly used preprocessor in c are as follow:
#define : Substitutes a preprocessor macro.
#include : Inserts a particular header from another file.
Example of use:
#define max_array_length 15
领英推荐
#include <stdio.h>
#include "myheader.h"
4. Header File: C language provides a series of predefined functions embedded in the header files in the C library. We use these functions to perform a specific task with the help of a header file.
For example, if you are using sqrt() function to find the square root of any number, you will have to include math.h header file.
Some commonly used header file in c are:
<stdio.h> : Standard input output - Standard Input /Output functions
<conio.h> : Console input output - Console Input /Output functions
syntax to include header files in C is as shown below:
#include<stdio.h>
#include"conio.h"
5. Main function: main() function is usually the entry point of any C program. It is the point at which execution of program is started. When a C program is executed, the execution control goes directly to the main() function. Mostly every C program have a main() function.
Syntax for main function is as shown below:
int main (void)
{ body }
In above syntax;
"void" is a keyword in C language, void means nothing, whenever we use void as a function return type then that function returns nothing. Here main() function do not return any value. In place of void we can also use int return type of main() function, at that time main() return integer type value.
"main" is a name of function which is predefined function in C library.
Example of a simple C program to print text using printf() function declared in <stdio.h> header file and main() :
#include<stdio.h> //used to include printf function.
#include<conio.h> //used to include clrscr() and getch() console functions.
void main() //use of main function in C.
{
clrscr(); //function to clear console screen.
printf("This is main function."); //for printing text as output.
getch(); //function to basically hold console screen.
}
Some IDEs can automatically use console functions like clrscr() and getch() for ease of programmers. However, IDEs like “Turbo C” requires these functions to be written.
Output of above program will be:
This is main function.
6. Lexemes & Tokens:
The compiler breaks any program into the smallest possible units called Lexemes. Lexeme can be called alphabets or building blocks of C Language.
Tokens are the classes/categories in which lexemes are divided.
In C language we have huge number of lexemes which can be differentiated using 6 major types of tokens as follow:
Identifiers, keywords, constants, strings, operators & separators (punctuators)
For example, if this is a line of code (LOC):
printf(“abc”);
Then the lexemes in this line are:
Tokens in this line are:
7. Statements: Statements are fragments of the C program that are executed in sequence. Statements are made by combining various tokens. Each statement which does not have its body must be terminated by the semicolon (;).
The statements which should be terminated:
The statements which should not be terminated:
There are five types of statements in C:
Expression statements, Compound statements, Selection statements, Iteration statements & Jump statements.
List of articles from the series:
CH 2) Syntax and Semantics
References:?Wikipedia, CMan(Bell Labs), GeeksforGeeks, guru99 and various other blogs, videos and sites on available internet.
If you find any errors or disinformation in this article, then please inform me.