C Basic Syntax

The C basic syntax specifies the rules for things to be written in a specific sequence to form meaningful instructions for the compiler. For example, how a variable should be declared, how to start a line of code, how to end it, where to put quotes, where to put braces, where to put semicolons etc.

Understanding the basic building blocks of C –

Tokens –

C program consists of various tokens. A token is either a keyword, an identifier, a symbol, a constant or a string literal. For instance, the following C statement consists of various tokens-

printf("Test");

The tokens in the above statement are

printf()

(

“Test”

)

Comments –

A comment is a text that is ignored by the compiler during code execution. A comment is either used to specify the working  of a chunk of code or to temporarily disable a part of the code. C language supports line as well as multiline comments.

Single line comment – A single line comment is started by putting a statement started with double slashes (//) and it ends with line break.

For example-

#include<stdio.h>
int main()
{
    //The below statement prints a message.
    printf("This is a test");
    return 0;
}

Multiline comments –

Multiline comments in C starts with /* and ends with */.

For example-

#include
int main()
{
    /*This is a multiline comment
    The below statement prints a message.*/
    printf("This is a test");
    return 0;
} 

Note : The concept of multiline comments is used in various programming languages but it was first introduced in the C programming. That is why it is sometimes called as C-style comment. 

Identifiers

An identifier in C is any name used to specify a variable, a function, a structure, an array or any other user defined entity. An identifier can start with a letter (A to Z or a to z), or an underscore (_) and can contain digits or underscore. An identifier name in C is case sensitive. That means Car and car are two different identifiers.

Note : You cannot use an identifier starting with a number. You also cannot use any special symbols (@,%,$) within an identifier.

Keywords

The keywords are the reserved words that are only meant for the compiler. The keywords cannot be used as an identifier name. Below given are the predefined keywords in C language.

auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

Semicolons

In C programming, a semicolon acts as a statement terminator. That is each individual statement should be ended with a semicolon, failing which results in an error.

Below given are some individual statements-

int a;
a = 10;
printf("%d", a);
printf("This is a test");

Structure of a C program

A C program is consists of preprocessor commands, statements and expressions, variables, functions and comments. Below given is a simple C program that would print a message “This is a test” on execution.

#include <stdio.h>
int main()
{
    //The below statement will print a message.
    printf("This is a test");
    return 0;
}

Let us look at different parts of the above program.

  • The first line is a preprocessor command which is used to include the library stdio. stdio stands for standard input and output. This file contains all the essential predefined functions for taking inputs and generating outputs. In general this type of file is called a header file. The reason behind that is these files always included at the top.
  • Next line is the starting of the main function. Everything in a C program that is going to be executed should be written inside the main function.
  • The next line is a comment which specifies that the program would output a simple message upon execution.
  • The next line is the actual print statement which will display the message on the screen.