Tokens
  • Tokens in C language are the essential elements to be used in creating a program.
  • We can define the token in C as the smallest individual element.
  • For example, without using words, we cannot create a sentence; similarly, we cannot create a C program without using tokens. 
  • Therefore, we can say that the tokens in C are the building block or the essential component for creating a program.

Classification of tokens:

  • Keywords.
  • Operators.
  • Strings.
  • Constants.
  • Special Characters.
  • Identifiers.
Keywords
  • Keywords are reserved words used in programming that have special meanings to the compiler.
  • For example:

                                  int marks;

           Here, “int” is a keyword that indicates marks is a variable of type integer.

  • Keywords are part of the Syntax, and they can’t be used as an identifier.
  • All keywords in C must be written in lowercase because C is a case sensitive language.
  • Here is a table showing the list of all keywords allowed in ANSI 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

Operators
  • An operator is a symbol that is used to perform certain operations. 
  • The following are the different types of operators to perform various kinds of operations in the C language.
    • Assignment Operator.
    • Arithmetic Operator.
    • Increment Operator.
    • Decrement Operator.
    • Relational Operator.
    • Logical Operator.
    • Bitwise Operator.
    • sizeof Operator.

 Precedence of operators:

Operator

Associativity

( )

left-to-right

[ ]

left-to-right

+ –

left-to-right

* / %

left-to-right

<< >>

left-to-right

< <=

left-to-right

> >=

left-to-right

== !=

left-to-right

Assignment Operator

  • The assignment operator in C language is used to assign a value to a variable.

Syntax:

             variable name = expression;

  • Where an expression may be a single constant or complex as required.

Types of Assignment Operators:

(1) “=”: 

  • It is a simple assignment operator. 

         Example:

                              ch=’a’;

                              x=50;

  • It is used to assign the value to the variable present on the left side of the Operator.

(2) “+=”

  • It is the combination of two operators, i.e., ‘+’ and ‘=’. 

          Example:

                              (x += y) is same as (x=x+y)

  • It first adds the current value of the variable on the left side of the ‘+’ Operator to the value on the right side of the operator ‘=’.
  • It then assigns the result to the variable on the left.

(3) “-=”:

  • This Operator is the combination of two operators, i.e., ‘-‘ and ‘=’.

          Example:

                              (x-=y) is same as (x=x-y)

  • It first subtracts the current value of the variable on the left side of the ‘-‘ Operator to the value on the right side of the operator ‘=’.
  • It then assigns the obtained result to the variable on the left.

(4) “*=”:

  • This operator is combination of two operators i.e., ‘* ‘and ‘=’.

          Example:

                              (x *= y) is same as (x= x * y)

  • It first multiplies the current value of the variable on the left side of the ‘*’ Operator to the value on the right side of the operator ‘=’.
  • It then assigns the obtained result to the variable on the left.

(5) “/=”:

  • This Operator is the combination of two operators, i.e., ‘/’and ‘=’.

          Example:

                               (x /= y) is same as (x = x/ y)

  • It first divides the current value of the variable on the left side of the ‘/’ Operator to the value on the right side of the operator ‘=’.
  • It then assigns the obtained result to the variable on the left.

(6)”%=”:

  • This Operator is the combination of two operators, i.e., ‘% ‘and ‘=’.

          Example:

                               (x %= y) is same as (x = x% y)

  • It first divides the current value of the variable on the left side of the ‘%’ operator to the value on the right side of the operator ‘=’.
  • It then assigns the obtained remainder of the division to the variable on the left.

Example Program:

#include<stdio.h>
int main()
{
    int x=4, y=2;
    printf("value of x is %d\n",x);
    printf("value of y is %d\n",y);
    x+=y;
    printf("value of x is %d\n",x);
    x-=y;
    printf("value of x is %d\n",x);
    x*=y;
    printf("value of x is %d\n",x);
    x/=y;
    printf("value of x is %d\n",x);
    x%=y;
    printf("value of x is %d\n",x);
    return 0;
}

Output:

value of x is 4
value of y is 2
value of x is 6
value of x is 4
value of x is 8
value of x is 4
value of x is 0

 Arithmetic Operator

           An arithmetic operator performs everyday mathematical operations such as addition, subtraction, multiplication, division and modulus on numerical values (constants and variables).

Different types of Arithmetic Operators:

(1)  ‘+’:

              It performs the addition of two values.

              Example:

                                  x + y

(2) ‘-‘:

            It performs the subtraction of one value with another.

            Example:

                                x – y

(3) ‘*’:

             It performs the multiplication of two values.

             Example:

                                 x * y

(4) ‘/’:

             It performs the division of one value with another.

             Example:

                                 x / y

(5) ‘%’:

              It provides the Remainder after dividing two values.

              Example:

                                  x % y

Example Program:

#include<stdio.h>
int main()
{
    int x=4, y=2, z;
    printf("x is %d and y is %d\n",x,y);
    z = x + y;// addition
    printf("x+y is %d\n",z);
    z = x - y;// subtraction
    printf("x-y is %d\n",z);
    z = x * y;// multiplication
    printf("x*y is %d\n",z);
    z = x / y;// division
    printf("x/y is %d\n",z);
    z = x % y;// modulus
    printf("x%y is %d\n",z);
    return 0;
}

Output:

x is 4 and y is 2
x+y is 6
x-y is 2
x*y is 8
x/y is 2
xy is 0

Increment Operator

  • C includes increment operator “++” to simplify common operation.
  • The increment operator “++”, adds 1 to its operand and its value get modified.

         Example:

                             x = x+1;

                             is the same as

                             ++x;

  • Increment operator may be either precede (prefix) or follow (postfix) the operand.

          Example:

                              x = x+1;

                              can be written

                              ++x; or x++;

  • However, there is a difference between the prefix and postfix forms of increment operator in the case of a larger expression.
  • When an increment operator precedes its operand, the value of the operand.
  • is used in the expression after incrementing it.
  • If the increment operator follows its operand, the value of the operand is obtained before incrementing it.
  • Mainly, the C compilers produce swift and efficient object code for increment and decrement operations, i.e., code that is better than that generated by using the equivalent assignment statement.
  • For this reason, we should use the increment and decrement operators when we can.

Example Program:

#include<stdio.h>
int main()
{
    int x=10, y=15;
    printf("x is %d\n", x++);
    //x is displayed before incrementing it
    printf("y is %d\n", y++);
    //y is displayed before incrementing it
    printf("x is changed to %d\n", x++);
    //previously incremented value of x is shown
    return 0;
}

Output:

x is 10
y is 15
x is changed to 11

Decrement Operator

  • C includes decrement operator “–” to simplify common operation.
  • The decrement operator “–” subtracts 1 to its operand, and its value gets modified.

          Example:

                              x = x-1;

                              is the same as

                             –x;

  • Decrement operator may be either precede (prefix) or follow (postfix) the operand.

          Example:

                              x = x-1;

                             can be written

                             –x; or x–;

  • However, there is a difference between the prefix and postfix forms of decrement operator in the case of a larger expression.
  • When a decrement operator precedes its operand, the value of the operand is used in the expression after decrementing it.
  • If the decrement operator follows its operand, the value of the operand is obtained before decrementing it.
  • Mainly, the C compilers produce swift and efficient object code for increment and decrement operations, i.e., code that is better than that generated by using the equivalent assignment statement.
  • For this reason, we should use the increment and decrement operators when we can.

Example Program:

#include<stdio.h>
int main()
{
    int x=10, y=15;
    printf("x is %d\n", x--);
    //x is displayed before decrementing it
    printf("y is %d\n", y--);
    //y is displayed before decrementing it
    printf("x is changed to %d\n", x--);
    //previously decremented value of x is shown
    return 0;
}

Output:

x is 10
y is 15
x is changed to 9

Relational Operators

  • Relational operators are used for checking the type of relationship between two values.

Different types of Relational Operators:

                    = =

                    !=

                    > 

                    < 

                    >=

                    <=

  1. ‘==’:
  •     It is known as “Equal to operator”.
  •     It checks whether the two given values are equal or not. If they are equal, then it returns true or else false. 

              Example:

                                   2==3 returns false.

                                   4==4 returns true.

  1. “!=”:
  • It is known as “Not equal to operator”.
  • It is the boolean complement of the “==” Operator.
  • It checks whether the two given values are equal or not. If so, then it returns true or else false.

          Example:

                              2!=5 returns true.

                              1!=1 returns false.

  1. “>”:
  • It is known as “Greater than operator”.
  • It checks whether the left side value of “>” is greater than the right side value of “>” or not. 
  • If they are greater, then it returns true or else false.

          Example:

                              2>5 returns false.

                              4>1 returns true.

  1. “<“:
  • It is known as “Lesser than operator”.
  • It checks whether the left side value of “< “is lesser than the right side value or not. 
  • If it is lesser, then it returns true or else false.

          Example:

                              5<2 returns true.

                              3<5 returns false.

  1. “>=”:
  • It is known as “Greater than or equal to operator”.
  • It checks whether the left side value of “>=” is greater than the right side value or not. 
  • If so, then it returns true or else false.

         Example:

                             3>=3 returns true.

                             2<=1 returns false.

  1. “<=”:
  • It is known as “Less than or equal to operator”.
  • It checks whether the left side value of “<=” is lesser than or equal to the right side value or not. 
  • If so, then it returns true or else false.

          Example:

                              2<=5 returns true.

                              5<=1 returns false.

Program:

#include<stdio.h>
int main()
{
    int x=2, y=2, z=5;
    printf("%d == %d is %d\n",x, y, x==y);
    printf("%d == %d is %d\n",x, y, x==z);
    printf("%d != %d is %d\n",x, y, x==y);
    printf("%d != %d is %d\n",x, y, x==z);
    printf("%d > %d is %d\n",x, y, x==y);
    printf("%d > %d is %d\n",x, y, x==z);
    printf("%d < %d is %d\n",x, y, x==y);
    printf("%d < %d is %d\n",x, y, x==z);
    printf("%d >= %d is %d\n",x, y, x==y);
    printf("%d >= %d is %d\n",x, y, x==z);
    printf("%d <= %d is %d\n",x, y, x==y);
    printf("%d <= %d is %d\n",x, y, x==z);
    return 0;
}

Output:

2 == 2 is 1
2 == 2 is 0
2 != 2 is 1
2 != 2 is 0
2 > 2 is 1
2 > 2 is 0
2 < 2 is 1
2 < 2 is 0
2 >= 2 is 1
2 >= 2 is 0
2 <= 2 is 1
2 <= 2 is 0

Logical Operator

  • Logical operators are used in decision making in C.
  • The C language supports three logical operators. 

           They are shown below:

                                        (1) &&

                                        (2) ||

                                        (3)!

  1. ‘&&’ operator:
  • It is known as Logical AND Operator.
  • It returns true when both the given conditions are satisfied. Otherwise, returns false.

           Example:

                               x && y returns true when both a and b are true or else returns false.

  1. ‘||’ operator:
  • It is known as a Logical OR Operator. 
  • It returns true even if one or both given conditions are satisfied. Otherwise, returns false.

           Example:

                                x || y returns true if x or y or both are true. Otherwise, returns false.

  1. ‘!’ Operator:
  • It is known as Logical NOT Operator.
  • The ‘!’Operator returns true if the given condition is not satisfied. Otherwise, returns false. 

          Example:

                               !x returns true if x is false. Otherwise, returns false.

Example Program:

#include<stdio.h>
int main()
{
    int x=2, y=2, z=8, res;
    res = (x == y) && (y > z);
    printf("result is %d\n", res);
    res = (x == y) && (y < z);
    printf("result is %d\n", res);
    res = (x == y) || (y < z);
    printf("result is %d\n", res);
    res = (x != y) || (y < z);
    printf("result is %d\n", res);
    res = !(x != z);
    printf("result is %d\n", res);
    res = !(x == z);
    printf("result is %d\n", res);
    return 0;
}

Output:

result is 0
result is 1
result is 1
result is 1
result is 0
result is 1

Bitwise Operator

  • Bitwise operators are used for testing, shifting, setting the actual bits in a byte or word.
  • It only corresponds to the standard char and int data types and variants.
  • We cannot use bitwise operations on a float, double, long double, void, or other more complex types.

           There are six bitwise operators in the C language.

           They are shown below:

                                        (1) & 

                                        (2) | 

                                        (3) ^ 

                                        (4) ~

                                        (5) >> 

                                        (6) << 

  1. ‘&’ Operator:
  • It is known as Bitwise AND Operator.
  • It takes two numbers as operands and performs AND operation on every bit of two numbers. 
  • If both the bits are 1, then the result of AND operation is 1.
  1. ‘|’ Operator:
  • It is known as Bitwise OR Operator. 
  • It takes two numbers as operands and performs OR operation on every bit of two numbers. 
  • If one of the two bits is 1, then the result of OR Operation is 1.
  1. ‘^’ Operator:
  • It is known as the Bitwise XOR operator.
  • It takes two numbers as operands and performs an XOR operation on every bit of two numbers.
  • If two bits are different, then the result of the XOR operation is 1.
  1. ‘<<‘ Operator:
  • It is known as the Left Shift operator. 
  • It takes two numbers as operands.
  • It left shifts the bits of the first operand, and the second operand defines the number of places to shift.
  1. ‘>>’ Operator:
  • It is known as the Right Shift operator.
  • It takes two numbers, right shifts the bits of the first operand, and the second operand defines the number of places to shift.
  1. ‘~’ Operator:
  • It is known as Bitwise NOT Operator.
  • It takes one number and compliments all bits of it.

Example Program:

#include<stdio.h>
int main()
{
    unsigned int x=65;
    unsigned int y=13;
    int z=0;
    z = x & y;
    printf("value of c is %d\n", z);
    z = x | y;
    printf("value of c is %d\n", z);
    z = x ^ y;
    printf("value of c is %d\n", z);
    z = ~x;
    printf("value of c is %d\n", z);
    z = x << 2;
    printf("value of c is %d\n", z);
    z = x >> 2;
    printf("value of c is %d\n", z);
}

Output:

value of c is 1
value of c is 77
value of c is 76
value of c is -66
value of c is 260
value of c is 16

Sizeof Operator

  • sizeof is a unary compile-time operator.
  • It is the most commonly used Operator in the C language.
  • It returns the length in bytes of the variable or parenthesized type specifier that it precedes.
  • To compute the size of a type, we must enclose the type name in parentheses.
  • sizeof is evaluated at compile-time, and the value it returns is treated as a constant within our program.
  • It can be used for any data type, float type and pointer-type variables.
  • When the sizeof() Operator is used with the data types, it simply provides the amount of memory allocated.

Example Program:

#include<stdio.h>
int main()
{
    int x=20;
    int y=16;
    printf("Size of variable x is: %d\n",sizeof(x));
    printf("Size of variable y is: %d\n",sizeof(y));
    printf("Size of int data type is: %d\n",sizeof(int));
    printf("Size of char data type is: %d\n",sizeof(char));
    printf("Size of float data type is: %d\n",sizeof(float));
    printf("Size of double data type is: %d\n",sizeof(double));
    return 0;
}

 

Output:  

Size of variable x is: 4
Size of variable y is: 4
Size of int data type is: 4
Size of char data type is: 1
Size of float data type is: 4
Size of double data type is: 8

Who is the course for?

Prerequisites before starting this course:

Questions 🙵 Answers.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

More Courses

You might also be interested in these courses