C Basic Syntax

Previously, you have seen the basic structure of C language, so it will be easy to grasp other concepts of C programming.

Tokens in C:     

The smallest individual unit in program is called tokens. It can be a keyword, a constant, an identifier, a literal, or a symbol.

Example:

printf(“Welcome to  programming”);

The above statement contains following tokens:

  • Printf
  • (
  • “Welcome to programming”
  • )
  • ;

Semicolon:

The semicolon is used as a statement terminator in C programming. In C, the semicolon (;) is the end of statement and beginning of new statement.

Example:

printf(“Hello”);

return 0;

Here, Compiler takes one statement as printf(“Hello”); and execute it after it the next statement is return 0;

Keywords:

Keywords are those words whose meaning has been defined to C compiler (or in a broad sense to the computer). There are only 32 keywords available in C.

keywords in c languageRules:

The Keyword cannot be used as variable name because if we do so, we are trying to assign a new value to keyword which is denied by compiler. Compiler cannot let anyone to manipulate the in-built keywords.

Note:

Some compiler vendors (like Microsoft, Borland etc.) provide their own keyword apart from the ones mentioned in the above figure. C include extended keywords like near, far, asm, etc. Hence, it has been suggested by ANSI committee that every compiler-specific keyword should be preceded by underscores (as in _asm) .

Identifiers:

It is just a name of variable. It is used for many classes, methods, variables, objects, etc. in a C program. Basically identifiers are user-defined name of a variable. A C identifier starts with the letter A to Z or a to z, followed by underscore (_) or any digit 0 to 9.

C is a case sensitive language so the variable name ABC and abc are two different identifiers. In C, punctuation, any special symbols are not allowed as identifiers.

Rule: Identifiers cannot be started with any digit (0 to 9). It must start with any alphabet.

Example: abc, ABC, rahul1, ram_3, _one, etc.

COMMENTS:

This plays an important role while writing your code.

A good programmer needs to write a very descriptive code, to write the code in descriptive manner, one should use comments.

Syntax for Comments:  Comments are of two type:

  1. Single line Comments
  2. Multi line comments

The “// ” used for single line comment and “ /*……..*/ ” for multi line comment.

Example: 1. Single Line Comment

#include<stdio.h>

int main()

{

printf(“Hello World”); //This will print Hello world

return 0;

2. Multi Line Comment

#include<stdio.h>

int main(void)

{

printf(“Welcome to the world ”);

/* Hello User

This syntax is for multi line Comment*/

return 0;

}

Whitespaces in C:

Whitespaces in C used to increase the readability of user by providing blanks, tabs, new line, etc. Using whitespace is a good practice of a programmer and it enhances the understanding of code.

There must be at least one whitespace present between two consecutive words, so that compiler can be able to distinguish between keyword and identifier or something else.

Example:

int id;

There is one space present between int and id, if space would not present then the statement will become “intid” which isn’t a keyword so compiler would take it as an identifier.

Literals:

Literals in C are nothing but the sequence of character that represents the constant values to be stored in variable.

In C, constant refers to the fixed values that the program may not change after execution, called Literals.

These are following literals in C:

  1. Integer literal
  2. Floating-point literal
  3. Character literal
  4. String literal

C Syntax rule:

  1. C is a case-sensitive language so all the instructions must be written in small case.
  2. All C statement must end with a semicolon.
  3. Whitespaces is used in C to describe blanks and tabs.
  4. Variable name must start with a character.