Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Tokens

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

C Language

PAVANI
C Tokens

 A token in C can be defined as the smallest individual element of the C programming


language that is meaningful to the compiler. It is the basic component of a C program.
 Types of Tokens in C
 The tokens of C language can be classified into six types based on the functions they are
used to perform. The types of C tokens are as follows:

1.Keywords
2.Identifiers
3.Constants
4.Strings
5.Special Symbols
6.Operators
Keywords

 Keywords
 The keywords are pre-defined or reserved words in a programming language.
Each keyword is meant to perform a specific function in a program.
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 volatile
do if static while
Identifiers

 Identifiers
 Identifiers are used as the general terminology for the naming of variables, functions, and
arrays. Rules for Naming Identifiers
 Certain rules should be followed while naming c identifiers which are as follows:
• They must begin with a letter or underscore(_).
• They must consist of only letters, digits, or underscore. No other special character is
allowed.
• It should not be a keyword.
• It must not contain white space.
• It should be up to 31 characters long as only the first 31 characters are significant.
• Identifiers are case-sensitive so names like variable and Variable will be treated as
different.
 Constants
 The constants refer to the variables with fixed values. They are like
normal variables but with the difference that their values can not be
modified in the program once they are defined.
 Constants may belong to any of the data types.
const int c_var = 20;
const int* const ptr = &c_var;
Strings

 Strings
 Strings are nothing but an array of characters ended with a null
character (‘\0’). This null character indicates the end of the string.
Strings are always enclosed in double quotes. Whereas, a character is
enclosed in single quotes in C and C++.
char string[20] = {‘P’, ’a’, ‘v’, ‘a’, ‘n’, ‘i’, ‘s’, ‘r’, ‘i’;
char string[20] = “pavanisri”;
char string [] = “pavanisri”;
Special Symbols

 The following special symbols are used in C having some special meaning and thus, cannot be used for some other purpose.
Some of these are listed below:
• Brackets[]: Opening and closing brackets are used as array element references. These indicate single and multidimensional
subscripts.
• Parentheses(): These special symbols are used to indicate function calls and function parameters.
• Braces{}: These opening and ending curly braces mark the start and end of a block of code containing more than one
executable statement.
• Comma (, ): It is used to separate more than one statement like for separating parameters in function calls.
• Colon(:): It is an operator that essentially invokes something called an initialization list.
• Semicolon(;): It is known as a statement terminator. It indicates the end of one logical entity. That’s why each individual
statement must be ended with a semicolon.
• Asterisk (*): It is used to create a pointer variable and for the multiplication of variables.
• Assignment operator(=): It is used to assign values and for logical operation validation.
• Pre-processor (#): The preprocessor is a macro processor that is used automatically by the compiler to transform your
program before actual compilation.
• Period (.): Used to access members of a structure or union.
• Tilde(~): Used as a destructor to free some space from memory.
Operators

 Operators are symbols that trigger an action when applied to C variables and other objects. The data
items on which operators act are called operands.
Depending on the number of operands that an operator can act upon, operators can be classified as
follows:
• Unary Operators: Those operators that require only a single operand to act upon are known as unary
operators.For Example increment and decrement operators
• Binary Operators: Those operators that require two operands to act upon are called binary
operators. Binary operators can further are classified into:
• Arithmetic operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Bitwise Operator
• Ternary Operator: The operator that requires three operands to act upon is called the ternary operator.
Conditional Operator(?) is also called the ternary operator.
The precedence and associativity of
C operators is given below:
Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right

Additive +- Left to right

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == != Left to right

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %=>>= <<= &= Right to left


^= |=

Comma , Left to right


Arithmetic Operators

 Arithmetic Operators are used for performing mathematical calculations like


addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).

Operator Description #include <stdio.h>


+ Addition void main()
- Subtraction {
* Multiplication
int i=3,j=7,k; /* Variables Defining and
Assign values */ k=i+j;
/ Division
printf("sum of two numbers is %d\n",
% Modulus k);
}
Increment and Decrement Operators

Increment and Decrement Operators are useful operators generally used to minimize
the calculation, i.e. ++x and x++ means x=x+1 or -x and x--means x=x-1.

But there is a slight difference between ++ or -- written before or after the operand.
Applying the pre-increment first adds one to the operand, and then the result is
assigned to the variable on the left,
whereas post-increment first assigns the value to the variable on the left and then
increments the operand.
Operator Description
++ Increment
−− Decrement
Example

#include <stdio.h> //stdio.h is a header file used for input.output purpose.


void main() {
//set a and b both equal to 5.
int a=5, b=5;
//Print them and decrementing each time.
//Use postfix mode for a and prefix mode for b.
printf("\n%d %d",a--,--b);
printf("\n%d %d",a--,--b);
printf("\n%d %d",a--,--b);
printf("\n%d %d",a--,--b);
printf("\n%d %d",a--,--b);
}
Relational operators

 Relational operators are used to compare two quantities or values.

Operator Description
== Is equal to
!= It is not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
logical operators

 C provides three logical operators when we test more than one condition
to make decisions. These are: && (meaning logical AND), || (meaning
logical OR) and ! (meaning logical NOT).
Operator Description

&& And operator. It performs logical


conjunction of two expressions. (if
both expressions evaluate to True, the
result is True. If either expression
evaluates to False, the result is False)
|| Or operator. It performs a logical
disjunction on two expressions. (if
either or both expressions evaluate
to True, the result is True)
! Not operator. It performs logical
negation on an expression.
bit operation

C provides a special operator for bit operation between two variables.

Operator Description
<< Binary Left Shift Operator
>> Binary Right Shift Operator
~ Binary Ones Complement Operator
& Binary AND Operator
^ Binary XOR Operator
| Binary OR Operator
Assignment operators

 Assignment operators are applied to assign the result of an expression


to a variable. C has a collection of shorthand assignment operators.

Operator Description

= Assign
+= Increments then assign
-= Decrements then assign
*= Multiplies then assign
/= Divides then assign
%= Modulus then assign
<<= Left shift and assign
>>= Right shift and assign
&= Bitwise AND assign
^= Bitwise exclusive OR and assign
|= Bitwise inclusive OR and assign
conditional operator

C offers a ternary operator, which is the conditional


operator (?: in combination), to construct
conditional expressions.

Operator Description
?: Conditional Expression
special operators

C supports some special operators

Operator Description #include <stdio.h>


sizeof() Returns the size of a memory void main()
location. {
& Returns the address of a memory int i=10;
location. // Variables Defining and Assign
values
* Pointer to a variable.
printf("integer: %d\n", sizeof(i));
}

You might also like