0% found this document useful (0 votes)
26 views4 pages

C Tokens

C programming

Uploaded by

aniketrawat5942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

C Tokens

C programming

Uploaded by

aniketrawat5942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C-Tokens

In a passage of text, individual word and punctuation marks are called tokens. Similarly, in a
C program the smallest individual units are known as tokens. Tokens are the input symbols
that the parser (of the compiler) processes at a time. A C program consists of various tokens:

a) Keywords (eg. if, while, float etc.)


b) Identifiers (eg. num1, sum etc.)
c) Constants (e.g. 107, -132.1 etc.)
d) Strings (eg. “Hello!”, “year” etc.)
e) Special symbols (eg. { } ( ) etc.)
f) Operators (eg. + - % < > etc.)

KEYWORDS
Keywords are reserved words having pre-defined meaning in C and therefore they may not be
used as constant name, variable name or any other identifier name. Eg. if, else, do, while, for,
char etc... There are 32 keywords in ANSI C.

Some compilers may use additional keywords that must be identified from the C manual, e.g.
near, far etc...

IDENTIFIERS
Identifiers refer to the names of variables, functions or other user-defined names. It consists
of a sequence of letters and digits, with a non-digit as the first character. Both uppercase and
lowercase letters (alphabets) are permitted, although lowercase letters are commonly used for
identifier names other than constant names. The underscore character is also permitted in
identifiers.

Rules for writing an identifier

1) First character must be a non-digit.


2) Must consist of only alphabets, digits and underscore.
3) Cannot be a keyword.
4) Must not contain whitespaces or special symbols other than underscore.
5) C is case sensitive language so the identifier names are also case sensitive i.e.
identifier num is different from identifier Num or NUM

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

Integer Constants: An integer constant refers to a sequence of digit representing an integral


value. There are 3 types of integer constants, namely, decimal integer, octal integer and
hexadecimal integer.
 Decimal Integer Constants: consist of a set of digits, 0 through 9, preceded by an
optional – or + sign. Eg. 123, -321, 0, 654321, +78 etc... Spaces, commas & non-digit
characters are not permitted between digits. E.g. Of illegal numbers:
o 15 750
o 20,200
o $1000 etc...
 Octal Integer Constants: An octal integer consists of any combination of digits from
the set 0 through 7 with a leading 0. E.g. of octal integer constants are: 062, 023 etc...
 Hexadecimal Integer Constants: A sequence of digits preceded by 0X or 0x is
considered as hexadecimal integer constant. They may also include alphabets A
through F or a through f. The letter A(or a) through F(or f) represents the numbers 10
through 15. Eg. 0X2, 0x9f, 0XBCD etc...

The largest integer value that can be used as integer constant is machine-dependent.

 16-bit: 32767
 32-bit: 2147483647

For larger integer constants qualifiers U, L or UL can be appended to the constants. E.g.

 56789U (unsigned integer)


 987612347UL (unsigned long integer)
 9876543L (long integer)

Real Constants: They are shown in decimal notation, having a whole number followed by a
decimal point and the fractional part. Either of the whole number or fractional portions are
optional i.e. examples of valid real constants are: 0.0083, -0.75, +247.0, 215, +.5, -.71 etc...

A real constant may also be expressed in exponential (or scientific) notation. E.g. 215.65 may
be written as 2.1565e2 or 2.1565E2. In general it is written as mantissa e exponent

Mantissa is either a real number expressed in decimal notation or an integer. The exponent is
an integer number with optional plus or minus sign.

Exponent notation is useful for representing numbers that are either very large or very small
in magnitude. E.g. -0.000000368 is equivalent to -3.68E-7 and 7500000000 is equivalent to
7.5E9

Floating point constants are normally represented as double-precision quantities. However,


the suffix F may be used to force single-precision. Suffix L can be used to make it a long
double constant.

Character Constants: A character constant contains a single character enclosed within a pair
of single quote marks. E.g. ‘5’, ‘X’, ‘;’, ‘ ’ (blank space)

Character constants have integer values known as ASCII values.


printf(“%d”, ‘a’); prints 97

printf(“%c”, 97); prints a

ASCII value Character

65-90 A-Z

97-122 a-z

48-57 0-9

Since each character constant is represented by an integer value, it is also possible to perform
arithmetic operation on character constants.

String Constants or String Literals: A string constant is a sequence of characters enclosed in


double quotes. The character may be letters, numbers, special characters and blank spaces.
Eg. “Hello!”, “1876”, “5+3”, “X” etc...

A character constant (eg. ‘x’) i not equivalent to the single character string (eg. “x”). A null
character (‘\0’) gets appended automatically with each string literal. Further, a single
character string constant does not have an equivalent integer value while a character constant
has an associated integer value. A character constant always occupies 1 byte of memory
where are a string constant’s size in memory depends on its length i.e. the numbers of
characters in the string.

Backslash character constant: C supports special backslash character constants that are
mainly used in output functions. E.g.

‘\n’ - new line character

‘\t’ - horizontal tab

‘\0’ – null

‘\\’ - backslash

Note that each one of them represents one character, although they consist of two characters.
These character combinations are known as escape sequences.

Defining Constants in C

Constants are required to be defined so as to increase the modifiability and readability of the
program. There are two ways of defining constants in a C program.

1) #define: The #define directive is a pre-processor directive. The pre-processor replaces


the macros by their body before the compiler even sees it. Note that #define only does
textual substitution. The contents of a #define can be arbitrarily complex but the
easiest is to use #define for defining constant values. Eg. #define PI 3.14

Note: A little complex macro could be: #define SQR(x) (x) *(x)

A multi-line macro could be:


#define POLY(x) { \
x = (x)*(x); \
x = 2*(x); \
}
The backward slash is used for writing multi-line macro.’ \’ tell the pre-processor
that the macro continues to the next line.

2) const Keyword: A const variable declaration declare an actual variable in the


language, which is used well like a real variable i.e. it has a memory location assigned
to it, its address can be taken, it can be passed to a function as parameter etc... The
const qualifier tells the compiler that the value of the variable must not be modified
by the program. However, it can be used on the right hand side of an assignment
statement like any other variable. Eg.

const int CLASS_SIZE = 40;

The compiler needs to diagnose and disallow explicit attempts to change const
objects.

You might also like