Chapter 5: Commands and Tokens
C Language(Basics & Fundamentals) - Chapter 5

Chapter 5: Commands and Tokens


PART 1: COMMANDS


What are Commands?

  • Commands are orders to computer program to perform particular task.
  • It is used to tell application or system to do something or perform task.
  • A command is a word or phrase that, when typed or spoken, causes the computer to perform a certain operation.
  • For example, at an MS-DOS prompt, when typing the "dir" command and pressing enter, the user would see a listing of directories and files in the current directory.
  • A program or operating system that supports commands can have dozens and sometimes even hundreds of different commands with different options and switches for each command.
  • When referring to a programming language, a command is a unique word used to perform a specific operation. For example, "print" is a command used to display text on the screen.
  • Commands are handled by part of operating system i.e. command interpreter.?
  • Computer without command is useless because users give command to computer to specific task and if there will be no user then computer is just a dump box.
  • Types of command include input program commands, utility commands, internal command, external command, etc.
  • It comes in different forms i.e. special words that program understand, function keys, buttons, etc.

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:

  1. printf
  2. (
  3. "abc"
  4. )
  5. ;

Tokens in this line are:

  1. Keywords - printf
  2. Separators - ();
  3. Strings - "abc"

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:

  1. The first character must be alphabetic.
  2. The underscore ‘‘_’’ counts as alphabetic.
  3. Upper and lower case letters are considered different.
  4. It should be up to 31 characters long as only the first 31 characters are significant.

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:

  • Automatic - Automatic variables are local to each invocation of a function, and are discarded on return.
  • Static - Static variables are local to a function, but retain their values independently of invocations of the function.
  • External - External variables are independent of any function.
  • Register - Register variables are stored in the fast registers of the machine. like automatic variables they are local to each function and disappear on return.

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:

  • Integer constants – An integer constant is a sequence of digits. An integer is taken to be octal if it begins with 0, decimal otherwise. The digits 8 and 9 have octal value 10 and 11 respectively.
  • Character constants – Character constant is 1 or 2 characters enclosed in single quotes ‘‘ ′ ’’. Within a character constant a single quote must be preceded by a backslash ‘‘\’’. Certain nongraphic characters, and ‘‘\’’ itself, may be escaped according to the following table:

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.

  • Floating constants – A floating constant consists of an integer part, a decimal point, a fraction part, an e, and an optionally signed integer exponent. The integer and fraction parts both consist of a sequence of digits. Either the integer part or the fraction part (not both) may be missing; either the decimal point or the e and the exponent (not both) may be missing. Every floating constant is taken to be double-precision.

?

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:

  • Unary Operators: Those operators that require only a single operand to act upon are known as unary operators. For Example - increment and decrement operators.
  • ?Binary Operators: Those operators that require two operands to act upon are called binary operators.

Binary operators are classified into :

1.??Arithmetic operators

2.??Relational operators

3.??Logical operators

4.??Assignment operators

5.??Bitwise operator

  • Ternary Operator: The operator that require three operands to act upon are called ternary operator. Conditional Operator (?) is also called ternary 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 1) Programming environment

CH 2) Syntax and Semantics

CH 3) Libraries & Header files

CH 4) Preprocessor & Objects

CH 5) Commands & Tokens

CH 6) Declaration & Statements

CH 7) Flow control & Functions

CH 8) Errors & Habits


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.

要查看或添加评论,请登录

Parth Dobariya的更多文章

  • Chapter 8: Errors and Habits

    Chapter 8: Errors and Habits

    PART 1: ERRORS Errors are the problems or the faults that occur in the program, which makes the behavior of the program…

  • Chapter 7: Flow control and Functions

    Chapter 7: Flow control and Functions

    PART 1: FLOW CONTROL Flow control statements determine the next statement to execute. In this sense, the statements…

  • Chapter 6: Declaration and Statements

    Chapter 6: Declaration and Statements

    PART 1: DECLARATION In programming, a declaration is a statement describing an identifier, such as the name of a…

  • Chapter 4: Preprocessor and Objects

    Chapter 4: Preprocessor and Objects

    PART 1: Preprocessor A program which processes the source code before it passes through the compiler is known as…

  • Chapter 3: Header files and Libraries

    Chapter 3: Header files and Libraries

    PART 1: LIBRARIES The Standard C Library provides a huge wealth of built-in functions and functionality that is…

  • Chapter 2: Syntax and Semantics

    Chapter 2: Syntax and Semantics

    PART 1: SYNTAX (Form of expressions, statements and program units) What is Syntax for C Language? Any language, be it…

  • Chapter 1: Programming Environment

    Chapter 1: Programming Environment

    Programming Environment: “A Programming Environment is the collection of tools used in the development of software.” In…

  • C Language: Basics & Fundamentals

    C Language: Basics & Fundamentals

    Through this series of articles, I hope to provide helpful information about the fundamentals of the C programming…

    2 条评论

社区洞察

其他会员也浏览了