Elements of C Language
C Tokens
Tokens are the smallest individual unit known as tokens. C recognises five type of tokens. C programs are written using these tokens and the syntax of the language.
Following are the C Tokens: 1.Keywords (Words defined by the language) 2.Identifiers (Names) 3.Literals (Constants) 4.Operators 5.Seperators
1. Keywords: – Keywords are the reserved words whose meaning has already been explained to the C compiler. These words are defined in the language itself. There are 32 keywords in C.
auto
else
register
union
continue
goto
static
double
long
typedef
const
for
sizeof
while
int
switch
char
float
signed
volatile
struct
case
extern
short
void
do
break
enum
return
unsigned
default
if
2. Identifiers: – Identifiers are the names given to variables, arrays, functions, pointers, structures. Using this identifier we can access that variable. All other names in a C program such as array name or a function name are also known as identifiers.
Variables:
An entity that may vary during program execution is called a variable. Variable names are names given to locations in memory. These locations can integer, real or character constants.
All the variables that are used in the program should be declared i.e. typed at the top with their respective data types.
The variable declaration tells two things:
a) It tells the compiler the name of the variable.
b) It also tells the type of value the variable will hold.
Eg: int a;
float b;
char c;
Here a is an integer variable, b is a float variable and c is a character variable.
; is called as termination symbol or end of statement.
Primary type declaration: A variable can be used to store a value of any data type. That is the variable name does not have anything to do with variable name.
E.g. int a,b,c;
Here a,b,c are the names given to the variables. All the variables of the same data type are separated by a comma. The declaration statement must end with a semicolon.
3. Literals/ Constants:-
In C there are 4 types of constants:
a) Integer constants: – They are a sequence of digits. There are three types of integers:
decimal ,octal, hexadecimal.
i)Decimal numbers contain set of digits 0 to 9 which can be positive (+) or negative (-).
E.g.: 6, 46, 398, 658736, -89, +598 are all integers
ii)Spaces, comma or special symbols are not allowed.
E.g.: 12 897, 364, 897, $56, 78@56 are all invalid or wrong numbers.
iii)Octal numbers consist of any combination of digits from 0 to 7, with a leading 0.
E.g.: 037, 0435, 0551.
iv)Hexadecimal numbers has 16 digits 0 to 9 and alphabets A to F (10-15), the sequence of digits is preceded by 0x or 0X.
E.g.: 0X2, 0x9f, 0xbcd.
b) Floating or Real constants: – Integer constants are not sufficient to represent all the Integer constants are not sufficient to represent all the quantities such as price, distance, temperature, etc. These numbers have decimal point (.)
i.e.fractional parts like 65.987, 0.000569, 89.36.
A floating number can also be represented as exponential or scientific notation for e.g.
0.0000123 can be written as 1.23*10 -5 or 1.23e-05.
Thus the general form is:
mantissa e exponent So, 1.23 is called mantissa and -05 is called as exponent.
c) Character constants: – A single character alphabets in uppercase (‘A’ to ‘Z’ ) or lowercase (‘a’ to ‘z’) or digits (0 to 9) or a special symbol@,#,$,%,*,&, +, -, ., :, / etc.) which are enclosed in single quotes (‘ ’).
E.g.: ‘a’ or ‘A’ or ‘7’ or ‘+’ are all character constants.
Note: all escape sequences are also considered characters.
d) String constants: – A string constant is a single character or a group of characters enclosed in double quotes (“ “).
Like “hello”, “4598”, “hello235”, “5+3”.
Note: The character constant ‘7’ is not the same as digit 7. Every character constants has an equivalent integer number.
‘A’ to ‘Z’ has ASCII 65 to 90. i.e. ‘A’ has ASCII 65 ‘B’ has ASCII 66 and so on.
‘a’ to ‘z’ has ASCII 97 to 122
‘0’ to ‘9’ has ASCII 48 to 57
‘ ‘ (space) has ASCII 32.
☺ ( smiling space) has ASCII 1.
Enter has ASCII 13
Esc has ASCII 27
4. Operators: – Operators can be used to perform the required computations on the values.
Types of Operators: There are three types of operators:
a) Unary Operator: Operators which work on one operand only.
b) Binary Operator: Operators which work on two operands.
c) Ternary Operator: Operators which work on three operands.
The Rule of Precedence: – Each operator in C has precedence associated with it. This precedence is used to determine how an expression involving more than one operator are evaluated. There are distinct levels of precedence and an operator may belong to one of the levels. The operators at the higher level of precedence are evaluated first.
The Rule of Associativity: – The operators of the same precedence are evaluated either from left to right or from right to left, depending on the level. This is known as the associativity property of an operator. There are 45 operators in C.
Classes of Operators: Operators are divided into mainly 9 classes:
1. Arithmetic operator
2. Unary operators
3. Relational operators
4. Assignment operators
5. Equality operators
6. Logical operators
7. Conditional operators
8. Bitwise operators
9. Comma operators
5) Comma operator: – , Operator works from left to right but returns the right most value. This operator is generally used in the for loop. The expressions separated by comma operator are solved from left to right. On using the comma operator the value and the type of right most operand is returned.
For e.g. in the assignment statement below:
I = (j = 1, k = 2, 3, 4);
The expression j = 1, k = 2, 3, 4 are evaluated first. Then the value of the expression is returned as 4 and assigned to i.