0% found this document useful (0 votes)
27 views5 pages

C Tokens

A token is the smallest unit in a C program. Keywords are reserved words with fixed meanings, there are 32 keywords. An identifier names elements like variables and functions. A variable stores values that can change, while a constant's value never changes. Different types of constants include integers, characters, strings, and real numbers.

Uploaded by

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

C Tokens

A token is the smallest unit in a C program. Keywords are reserved words with fixed meanings, there are 32 keywords. An identifier names elements like variables and functions. A variable stores values that can change, while a constant's value never changes. Different types of constants include integers, characters, strings, and real numbers.

Uploaded by

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

What is Token in C?

TOKEN 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
(tokens) and proceeds to the various stages of the compilation. A token is divided into six
different types, viz, Keywords, Operators, Strings, Constants, Special Characters, and Identifiers.

Tokens in C

Keywords and Identifiers


In 'C' every word can be either a keyword or an identifier.

Keywords have fixed meanings, and the meaning cannot be changed. They act as a building
block of a 'C' program. There are a total of 32 keywords in 'C'. Keywords are written in
lowercase letters.

Following table represents the keywords in 'C'-

auto double int struct

break else long switch

case enum register typedef


char extern return union

const short float unsigned

continue for signed void

default goto sizeof volatile

do if static while

An identifier is nothing but a name assigned to an element in a program. Example, name of a


variable, function, etc. Identifiers are the user-defined names consisting of 'C' standard character
set. As the name says, identifiers are used to identify a particular element in a program. Each
identifier must have a unique name. Following rules must be followed for identifiers:

1. The first character must always be an alphabet or an underscore.


2. It should be formed using only letters, numbers, or underscore.
3. A keyword cannot be used as an identifier.
4. It should not contain any whitespace character.
5. The name must be meaningful.

Summary
 A token is the smallest unit in a program.
 A keyword is reserved words by language.
 There are total of 32 keywords.
 An identifier is used to identify elements of a program.

What is a Variable?
A variable is an identifier which is used to store some value. Constants can never change at
the time of execution. Variables can change during the execution of a program and update
the value stored inside it.

A single variable can be used at multiple locations in a program. A variable name must be
meaningful. It should represent the purpose of the variable.
Example: Height, age, are the meaningful variables that represent the purpose it is
being used for. Height variable can be used to store a height value. Age variable can
be used to store the age of a person

A variable must be declared first before it is used somewhere inside the program. A variable
name is formed using characters, digits and an underscore.

Following are the rules that must be followed while creating a variable:

1. A variable name should consist of only characters, digits and an underscore.


2. A variable name should not begin with a number.
3. A variable name should not consist of whitespace.
4. A variable name should not consist of a keyword.
5. 'C' is a case sensitive language that means a variable named 'age' and 'AGE' are
different.

Constants
Constants are the fixed values that never change during the execution of a program.
Following are the various types of constants:

Integer constants
An integer constant is nothing but a value consisting of digits or numbers. These values
never change during the execution of a program. Integer constants can be octal, decimal
and hexadecimal.

1. Decimal constant contains digits from 0-9 such as,

Example, 111, 1234

Above are the valid decimal constants.

2. Octal constant contains digits from 0-7, and these types of constants are always
preceded by 0.

Example, 012, 065

Above are the valid decimal constants.

3. Hexadecimal constant contains a digit from 0-9 as well as characters from A-F.
Hexadecimal constants are always preceded by 0X.

Example, 0X2, 0Xbcd

Above are the valid hexadecimal constants.


The octal and hexadecimal integer constants are very rarely used in programming with 'C'.

Character constants
A character constant contains only a single character enclosed within a single quote (''). We
can also represent character constant by providing ASCII value of it.

Example, 'A', '9'

Above are the examples of valid character constants.

String constants
A string constant contains a sequence of characters enclosed within double quotes ("").

Example, "Hello", "Programming"

These are the examples of valid string constants.

Real Constants
Like integer constants that always contains an integer value. 'C' also provides real
constants that contain a decimal point or a fraction value. The real constants are also called
as floating point constants. The real constant contains a decimal point and a fractional
value.

Example, 202.15, 300.00

These are the valid real constants in 'C'.

A real constant can also be written as,

Mantissa e Exponent

For example, to declare a value that does not change like the classic circle constant PI,
there are two ways to declare this constant

1. By using the const keyword in a variable declaration which will reserve a storage
memory

#include <stdio.h>
int main() {
const double PI = 3.14;
printf("%f", PI);
//PI++; // This will generate an error as constants cannot be changed
return 0;}
2. By using the #define pre-processor directive which doesn't use memory for storage
and without putting a semicolon character at the end of that statement

#include <stdio.h>
#define PI 3.14
int main() {
printf("%f", PI);
return 0;}

Summary
 A constant is a value that doesn't change throughout the execution of a program.
 A variable is an identifier which is used to store a value.
 There are four commonly used data types such as int, float, char and a void.
 Each data type differs in size and range from one another.

You might also like