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.
printf(“Welcome to programming”);
The above statement contains following tokens:
- “Welcome to programming”
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.
Here, Compiler takes one statement as printf(“Hello”); and execute it after it the next statement is return 0;
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.
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.
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) .
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.
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:
- Single line Comments
- Multi line comments
The “// ” used for single line comment and “ /*……..*/ ” for multi line comment.
Example: 1. Single Line Comment
printf(“Hello World”); //This will print Hello world
2. Multi Line Comment
printf(“Welcome to the world ”);
/* Hello User
This syntax is for multi line Comment*/
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.
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 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:
- Integer literal
- Floating-point literal
- Character literal
- String literal
C Syntax rule:
- C is a case-sensitive language so all the instructions must be written in small case.
- All C statement must end with a semicolon.
- Whitespaces is used in C to describe blanks and tabs.
- Variable name must start with a character.