Pages

Saturday, 7 March 2020

Tokens in C

Tokens : In C programs, each word and punctuation is referred to as a token. 
C Tokens are the smallest building block or smallest unit of a C program.
C Supports Six Types of Tokens:
  1. Keywords
  2. Identifiers
  3. Constants
  4. Strings
  5. Operators
  6. Special characters
Keywords : A keyword is a reserved word. You cannot use it as a variable name, constant name etc.
There are only 32 reserved words (keywords) in the C language.

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


Identifiers : An identifier is a name that is assigned by the user for a program element such as variable, type, template, class,function or namespace. 
It is usually limited to letters, digits, and underscores.
Identifiers are used to identify a program element in the code.

Rules for Naming Identifiers :
  1.  An identifier can only have alphanumeric characters (a-z, A-Z, 0-9) (i.e. letters & digits) and underscore( _ ) symbol.
  2. Identifier names must be unique.
  3. The first character must be an alphabet or underscore.
  4. You cannot use a keyword as identifiers.
  5. Only first thirty-one (31) characters are significant.
  6. Must not contain white spaces.
  7. Identifiers are case-sensitive.
Constants : Constants are like a variable, except that their value never changes during execution once defined.
C Constants is the most fundamental and essential part of the C programming language. 
Constants in C are the fixed values that are used in a program, and its value remains the same during the entire execution of the program.
Constants are also called literals and can be any of the data types.
const keyword defines a constant in C.

Strings : A string is a sequence of characters terminated with a null character "\0"
For example: char c[  ] = "c string";
c

s
t
r
i
n
g
\0
When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character "\0" at the end by default.

Operators : An operator is a symbol that operates on a value or a variable. 
For example: "+" is an operator to perform addition.C has a wide range of operators to perform various operations.
C programming language offers various types of operators having different functioning capabilities.
  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Increment and Decrement Operators
  6. Conditional Operator
  7. Bitwise Operators
  8. Special Operators
Arithmetic Operators : Arithmetic Operators are used to performing mathematical calculations like addition (+), subtraction (-), multiplication (*), division (/) and modulus (%).

Operator

Description

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulus


Increment and Decrement Operators : 
Increment operator ++ increases the value by 1 whereas decrement operator -- decreases the value by 1.

Operator  

Description

++

Increment

−−

Decrement


Relational Operators : 
Relational operators are used to comparing two quantities or values.

Operator

Description

    ==

Is equal to

     !=

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, 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.


Bitwise Operators : 
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 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

  sizeof()

Returns the size of a memory location.

    &

Returns the address of a memory location.

     *

Pointer to a variable.


Special Characters : There are some 
Special Characters in C Programming, which are as shown in the table.
,
<
>
.
_
(
)
;
$
:
%
[
]
#
?
&
{
}
^
!
*
/
|
-
\
~
+

No comments:

Post a Comment

Program for Largest Number among three numbers

Program for Largest Number among three numbers /* c program to find largest number among 3 numbers*/ #include <stdio.h>   in...