Chapter 5: Commands and Tokens
PART 1: COMMANDS
What are Commands?
Basic commands in C
#include <stdio.h> This command includes standard input output header file(stdio.h) from the C library before compiling a C program.
int main() It is the main function from where C program execution begins.
{ Indicates the beginning of the main function.
/*_some_comments_*/ Whatever written inside this command “/* */” inside a C program, it will not be considered for compilation and execution.
printf(“Hello_World! “); This command prints the output on the screen.
getch(); This command is used for any character input from keyboard.
return 0; This command is used to terminate a C program (main function) and it returns 0.
} It is used to indicate the end of the main function.
PART 2: TOKENS
A token is a group of characters having collective meaning: typically a word or punctuation mark.
A lexeme is an actual character sequence forming a specific instance of a token, such as num. The pattern matches each string in the set. Lexeme are the smallest elements of a program, which are meaningful to the compiler. LEXEME is the smallest unit in a 'C' program. It is each and every word and punctuation that you come across in your C program. The compiler breaks a program into the smallest possible units and proceeds to the various stages of the compilation.
For example, if this is a line of code (LOC):
printf(“abc”);
Then the lexemes in this line are:
Tokens in this line are:
In English language we can use only 26 alphabets, while in C language we can use huge number of lexemes which can be differentiated using 6 types.
There are six kinds of tokens in C:
identifiers, keywords, constants, strings, expression operators & other separators.
1. Identifiers (Names): An identifier is a sequence of letters and digits. Identifiers are used as the general terminology for the naming of variables, functions and arrays.?A special kind of identifier, called a statement label, can be used in goto statements.
Rules for Identifiers:
C bases the interpretation of an identifier upon two attributes of the identifier:
i) Storage class of identifier – The storage class determines the location and lifetime of the storage associated with an identifier.
There are four declarable storage classes:
ii) Type of identifier – The type determines the meaning of the values found in the identifier’s storage.
?
2. Keywords: Identifiers which are reserved for specific use and may not be used otherwise are keywords. You cannot re-define keywords. However, you can specify the text to be substituted for keywords before compilation by using C/C++ preprocessor directives.
C language supports 32 keywords which are given below:
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
Fun Fact: C++ has 63 keywords including 32 keywords from C and 31 additional keywords.
?
3. Constants: Constants are also like normal variables. But, the only difference is, their values cannot be modified by the program once they are defined. Constants refer to fixed values. They are also called literals. Constants may belong to any of the data type.
领英推荐
There are several kinds of constants, as follows:
BS??????\b
NL??????\n
CR??????\r
HT??????\t
ddd????\ddd
\?????????\\
The escape ‘‘\ddd’’ consists of the backslash followed by 1, 2, or 3 octal digits which are taken to specify the value of the desired character. A special case of this construction is ‘‘\0’’ (not followed by a digit) which indicates a null character.
Character constants behave exactly like integers (not, in particular, like objects of character type). In conformity with the addressing structure of the PDP11, a character constant of length 1 has the code for the given character in the loworder byte and 0 in the highorder byte; a character constant of length 2 has the code for the first character in the low byte and that for the second character in the highorder byte. Character constants with more than one character are inherently machinedependent and should be avoided.
?
4. Strings: A string is a sequence of characters surrounded by double quotes ‘‘ " ’’. A string has the type array-of-characters (see below) and refers to an area of storage initialized with the given characters. The compiler places a null byte (\0) at the end of each string so that programs which scan the string can find its end. In a string, the character ‘‘ " ’’ must be preceded by a ‘‘\’’ ; in addition, the same escapes as described for character constants may be used. Strings are always enclosed in double-quotes; a character is enclosed in single quotes in C and C++.
?
5. Expression operators: Operators are symbols that trigger an action when applied to C variables and other objects. The data items on which operators act upon are called operands. Depending on the number of operands that an operator can act upon, operators can be classified as follows:
Binary operators are classified into :
1.??Arithmetic operators
2.??Relational operators
3.??Logical operators
4.??Assignment operators
5.??Bitwise operator
?
6. Other separators:
Special Symbols: The following special symbols are used in C having some special meaning and thus, cannot be used for some other purpose.[] () {}, ; * = #
Brackets[]: Opening and closing brackets are used as array element reference. These indicate single and multidimensional subscripts.
Parentheses(): These special symbols are used to indicate function calls and function parameters.
Braces{}: These opening and ending curly braces mark the start and end of a block of code containing more than one executable statement.
Comma (, ): It is used to separate more than one statements like for separating parameters in function calls.
Colon(:): It is an operator that essentially invokes something called an initialization list.
Semicolon(;): It is known as a statement terminator.?It indicates the end of one logical entity. That’s why each individual statement must be ended with a semicolon.
Asterisk (*): It is used to create a pointer variable and?for the multiplication of variables.
Assignment operator(=): It is used to assign values and for the logical operation validation.
Pre-processor (#): The preprocessor is a macro processor that is used automatically by the compiler to transform your program before actual compilation.
List of articles from the series:
CH 5) Commands & Tokens
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.