C - Basic Syntax



In C programming, the term “syntax” refers to the set of rules laid down for the programmer to write the source code of a certain application. While there is a specific syntax recommended for each of the keywords in C, certain general rules need to be followed while developing a program in C.

C Basic Syntax

A typical source code of a C program has the following elements −

Comments
/*Hello World program*/
Header file
#include <stdio.h>
Global declarations
int a=10;
main() function
int main()
Local variable
{
   char message[] = "Hello World";
   printf("%s", message);
   return 0;
}

Tokens − The smallest unit of a source code in C is called as token. The C compiler identifies whether the token is a keyword, identifier, comment, a literal, an operator, any of the other recognized special symbols or not. This exercise is done by the tokenizer in the first stage of the compilation process.

Identifiers − C prescribes certain rules to form names of variables, functions or other programming elements. They are not keywords. An identifier must start with an alphabet or underscore character, and must not have any other character apart from alphabets, digits and underscore.

Keywords − The most important part of C language is its keywords. Keywords are the reserved words having a predefined meaning with prescribed syntax for usage. In ANSI C, all keywords have lowercase alphabets. The programmer needs to choose the correct keywords to construct the solution of the problem at hand. To learn programming is basically to learn to correctly use the keywords.

Source code − The C program is a text file, containing a series of statements. The file must have a .c as its extension. The C compiler identifies only the .c file for compilation process. Only the English alphabets are used in the keywords and other identifiers of C language, although the string literals may contain any Unicode characters.

main() function − Every C program must have one (and only one) main function, from where the compiler starts executing the code. However, it is not necessary that the main() function should be in the beginning of the code in the .c file. There can be any number of functions in a C program. If a function calls any other function before its definition, there should be its forward declaration.

Header files − In addition to the keywords, a C program often needs to call predefined functions from the library of header files. The required header files are imported with the #include preprocessor directive. All the #include statements must be in the beginning of the source code.

Variable declaration − C is a statically typed language. It requires, all the variables appearing in a program to be declared before using. Variables can be declared globally i.e. outside any function, or locally within the scope of a function. The variable can store a value only of its declared type. This is one of the important rules of C.

Statements − Statements are the basic building blocks of the program. Statements in the main() function are executed in a top to bottom order by default. The sequence is controlled by conditionals or looping constructs. As a basic syntax rule, each statement must have semicolon (;) at the end.

White space − While compiling the source code, the compiler ignores the whitespaces. The space character, tab, line feed (Enter key), etc., are called whitespaces. While one can use them for better readability of the code, they have little significance for the compiler (unless they are a part of a string literal, appearing inside the double quote symbols).

Since the ; is only the delimiter symbol for a C statement, there may be more than one statements in a one physical line in a C program. Similarly, a single statement may span over more than one lines in the source code.

The following line is perfectly valid in C. In one line, there are multiple statements.

int a=10; if (a>=50) printf("pass"); else printf("fail");

The code below is also valid even if a statement spills over multiple lines.

if
   (a>=50)
   printf("pass");
else printf("fail");

Compound statements − Often, you need to define a cohesive block of statements as a single unit of programming logic. For example, you may want more than one statements to be executed if a certain logical expression is true, or multiple statements in a looping block. Similarly, a user-defined function may have more than one statements. In such cases, statements are grouped together to form a compound statement. C uses curly brackets for such grouping.

{
   Statement1;
   Statement2;
   . . . 
   . . . 
}

In the following code, the if and else parts have a block each of statements.

int marks = 40;

if (marks<50) {
   printf("Result: Fail\n");
   printf ("Better Luck next time");
}
else {
   printf("Result: Pass\n");
   printf("Congratulations");
}

Curly brackets are also used in function definition.

float area_of_square(float side)
{
   float area = pow(side,2);
   return area;
}

Defining a custom type with struct, union or enum also requires clubbing more than one statements together with curly brackets.

struct student 
{   
   char name[20];   
   int marks, age;   
};

The curly brackets also declare an array as follows −

int marks[ ]={50,56,76,67,43};

As mentioned earlier, each keyword in C has a well-defined syntax in addition to the basic syntax explained in this chapter. The usage of each keyword will be explained in the subsequent chapters.

Advertisements