C Tokens

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

C Tokens(Keywords,

Identifiers, Constants,
Variables)
Tokens in C

In a paragraph of text document, individual words and punctuation


marks are called tokens.

Similarly, the smallest individual unit in a C program is known as a


token or a lexical unit.

Different tokens in C can be classified as below:


• Keyword (e.g. int, for, if)
• Identifier (e.g. main, a, sum)
• Constant (e.g. 10, 3.14)
• String-literal (e.g. “hello”, “Ahmad”)
• Operator (e.g. +, /, *)
• Punctuator (e.g. (), {}, ;)
Keywords

• Keywords are pre-defined words in a C compiler.

• Each keyword convey special meaning and perform a specific


function in a C program.

• Since keywords are referred names for compiler, they can’t be used
as variable name.
There are following 32 keywords in C:

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 volaile
do if static while
Identifiers

• General terminology used for the names of variables, functions


and arrays.

• These are user defined names consisting of arbitrarily long


sequence of letters and digits.

• Identifiers may either a letter or the underscore(_) as a first


character.

• For example, x is a name given to integer variable.


Constants

• Data items that remains same i.e. their value during do not
changes during the program execution.

• Several types of C constants that are allowed are; integer constant,


real constant, character constant, string constant and others

• For example,
3
-6.4
String Literals

• Sequence of characters enclosed within double quotes. For example,


“hello” , “abc”.

• Every sting constant is automatically terminated with a special


character '\0' called the null character which represents the end of
the string.

• For example, “hello” will represent “hello\0” in the memory.

• Thus, the size of the string is the total number of characters plus one for
the null character.
Operators

• C operators are symbols that trigger an action and perform operation


when applied to C variables and other objects.
• The data items on which operators act upon are called operands.

• Operators can be classified as follows:


– Unary Operators: Those operators that require only single operand to
act upon are known as unary operators.
– Binary Operators: Those operators that require two operands to act
upon are called binary operators.
– Ternary Operators: These operators requires three operands to act
upon.
Punctuators

• It has some special meaning . Thus, cannot be used for some other
purpose [] () {} , ; : * … = # are examples of punctuators

• Braces{}: These opening and ending curly braces marks the start and
end of a block of code containing more than one executable statement.

• Parentheses(): These special symbols are used to indicate function


calls and function parameters.

• Brackets[]: Opening and closing brackets are used as array element


reference. These indicate single and multidimensional subscripts.
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 contain integer, real or character constants

• For example, an integer variable can hold only an integer constant,


a real variable can hold only a real constant and a character
variable can hold only a character constant
Rules for Constructing Variable Names
• A variable name is any combination of alphabets, digits or underscores.

• The first character in the variable name must be an alphabet or underscore.

• No commas or blanks are allowed within a variable name.

• No special symbol other than an underscore (as in gross_sal) can be used in a


variable name.
• Example:
• si_int
• m_hra
• Valid identifiers
• _abc
• Rectangle_length
• Delhi_24
• A55

• Invalid identifiers
• 4_abc
• 55Delhi
• Thanks

You might also like