Wednesday, September 24, 2014

2.3 TOKENS

Character Set

The characters that can be used to form words, numbers and expressions depend upon the computer on which the program is run.

The characters in C are grouped into the following categories:
1.      Letters
2.      Digits
3.      Special characters
4.      White spaces

Letters
A-Z, a-z
Digits
0-9
Special Characters
!@#$%^&*()<>:”{}][/.,
White Spaces
Tab, New Line

 
NOTE: the compiler ignores white spaces unless they are a part of a string constant. White spaces are prohibited between the characters of keywords and identifiers.

 
C Tokens
In a passage of text, individual words and punctuations marks are called tokens. Similarly, in a C program the smallest individual units, which convey complete meaning, are known as tokens. C has six types of tokens.

Fig 02: Tokens

Keywords
§  Keywords serve as the basic building blocks for program statements. They have fixed meaning and these meanings cannot be changed.
§  The C language has 32 keywords.

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

 
Identifiers
§  Identifiers are the names which are given to elements of a program such as variables, arrays and functions.
§  Identifiers are user-defined and are applicable in program logic.
§  Rules for defining identifiers:
o   First character can be composed of letters (both uppercase and lowercase letters) or underscore '_' only.
o   An identifier name can consist of alphabets, digits or underscore.
o   Keywords cannot be used as identifier.
o   There is no rule for the length of an identifier. However, the first 63 characters of an identifier are discriminated by the compiler. So, the first 63 letters of two identifiers in a program should be different.

Difference between identifier and keyword

Identifier
Keyword
Identifiers are the names given to various program elements, such as variable, functions and array.
Keywords are reserved words that have predefined meaning in C.
Identifiers consists of letters, digits and underscore.
Keywords consists of only letters.
It can use both uppercase and lowercase letters.
Only lowercase letters are used.
e.g. X, sum, main, area etc.
e.g. auto, short, long etc.

 
Constant
§  Constants in C refer to fixed values that do not change during the execution of a program.

                                                               Fig 03: Constants


§  Integer constants
Rules
o   It must have at least one digit.
o   It must not have a decimal point.
o   It can be either positive or negative.
o   If no sign precedes an integer constant, it is assumed to be positive.
o   No commas or blanks are allowed within an integer constant.

§  Real constants
Rules
o   Default sign is positive.
o   No commas or blanks are allowed within a real constant.
 
§  Character constants
Rules
o   A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas.
o   The maximum length of a character constant can be 1 character.
o   e.g.: ‘A’           ‘a’        ‘57’      ‘&’

§  String constants
Rules
o   A string constant is a sequence of characters enclosed in double quotes.
o   The characters may be letters, numbers, special characters and blank space.
o   e.g “Hello”      “1981”             “5+3”              "X”

NOTE: A character constant (‘X’) is not equivalent to the single character string constant (“X”).

NOTE: a single character string constant does not have an equivalent integer value while a character constant has an integer value.

 
Backslash Character Constant
§  C supports some special backslash character constants that are used in output functions.
§  Each constant represents one character, although they consist of two characters. These character combinations are known as “escape sequence”.

Escape Sequences
Character
\b
Backspace
\f
Form feed
\n
Newline
\t
Horizontal tab
\v
Vertical tab
\\
Backslash
\’
Single quotation mark
\”
Double quotation mark
\?
Question mark
\0
Null character
 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.