You are on page 1of 15

C TOKENS

ARCHANA MENON P
archananirmal1414@gmail.com
Agenda
• Tokens in C
 Keywords
 Identifiers
 Strings
 Constants
 Operators
 Special Characters

• Variables
Tokens in C
• Tokens are the smallest elements of a program, which are meaningful to the compiler.
Keywords
 Keywords are predefined, reserved words in C and each of which is associated
with specific features.
 These words help us to use the functionality of C language.
 They have special meaning to the compilers.
 There are total 32 keywords in C.
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned
Identifiers
• Each program element in C programming is known as an identifier.
• They are used for naming of variables, functions, array etc.
• These are user-defined names which consist of alphabets, number, underscore ‘_’.
• Identifier’s name should not be same or same as keywords.
• Keywords are not used as identifiers.
Identifiers
Rules for naming C identifiers −
• It must begin with alphabets or underscore.
• Only alphabets, numbers, underscore can be used, no other special characters,
punctuations are allowed.
• It must not contain white-space.
• It should not be a keyword.
• It should be up to 31 characters long.
Strings
• A string is an array of characters ended with a null character(\0).
• This null character indicates that string has ended.
• Strings are always enclosed with double quotes(“ “).

String declaration in C language −


• char string[20] = {‘s’,’t’,’u’,’d’,’y’, ‘\0’};
• char string[20] = “demo”;
• char string [] = “demo”;
Constants in C
• A constant is a value assigned to the variable which will remain the same
throughout the program, i.e., the constant value cannot be changed.
• There are two ways of declaring constant:
o Using const keyword
o Using #define pre-processor
Types of constants in C
Constant Example
Integer constant 10, 11, 34, etc.
Floating-point constant 45.6, 67.8, 11.2, etc.

Octal constant 011, 088, 022, etc.


Hexadecimal constant 0x1a, 0x4b, 0x6b, etc.
Character constant 'a', 'b', 'c', etc.
String constant "java", "c++", ".net", etc.
Operators in C
• Operators are special symbols used to perform the functions.
• The data items on which the operators are applied are known as operands.
• Operators are applied between the operands.

Depending on the number of operands, operators are classified as follows:

Unary Operator
• A unary operator is an operator applied to the single operand. For example: increment
operator (++), decrement operator (--), sizeof, (type)*.

Binary Operator
• The binary operator is an operator applied between two operands. For example: Arithmetic
Operators, Relational Operators, Logical Operators
Special characters in C
Some special characters are used in C, and they have a special meaning which cannot be used for
another purpose.
• Square brackets [ ]: The opening and closing brackets represent the single and multidimensional
subscripts.
• Simple brackets ( ): It is used in function declaration and function calling. For example, printf() is a
pre-defined function.
• Curly braces { }: It is used in the opening and closing of the code. It is used in the opening and
closing of the loops.
• Comma (,): It is used for separating for more than one statement and for example, separating
function parameters in a function call, separating the variable when printing the value of more than
one variable using a single printf statement.
• Hash/pre-processor (#): It is used for pre-processor directive. It basically denotes that we are using
the header file.
• Asterisk (*): This symbol is used to represent pointers and also used as an operator for
multiplication.
• Tilde (~): It is used as a destructor to free memory.
• Period (.): It is used to access a member of a structure or a union.
Variables
• A variable is a storage place that has some memory allocated to it.
• It is used to store some form of data and retrieve it when required.
• Different types of variables require different amounts of memory and have some specific set of
operations that can be applied to them.
• A variable name can consist of alphabets (both upper and lower case), numbers, and the underscore
‘_’ character.
• The name must not start with a number.

• C Variable Declaration
type variable_name;
type variable1_name, variable2_name, variable3_name;

Eg:- int a;
float x,y,z;
a=5;
Int s=0;
Difference between Constant and Variables
Constant Variables

A constant is a variable or value that cannot be altered once


A variable is a name associated with some memory location.
defined.

A constant is used to hold the fixed values which we can A variable is used to hold some value that can be changed
retrieve later but cannot change. according to the requirement.

The constants are generally stored in the text segment as The variables are stored inside a data segment, heap, or
they are read-only stack depending on the environment it is declared in.

We can only assign a value to the constant while defining it. We can assign value to the variable anytime.

A constant can be defined by A variable can only be defined using the standard variable
using #define or const keyword. definition syntax.

Example: #define pi 3.14 Example: int var = 25;


const int pi = 3.14; var = 10;
References
• https://www.tutorialspoint.com/c-program-to-detect-tokens-in-a-c
THANK YOU

You might also like