Sunday, April 21, 2013

C-Programming(Tokens)


Tokens: - The smallest indivisible unit of any program is called tokens.
These tokens are.
1.      Identifiers.
2.      Keywords.
3.      Constants.
4.      Variables.
5.      Operators.
Identifiers: - Identifier is the name of variables, functions and arrays etc. that is made up from combination of alphabets, digits and underscore.

Keywords: - keywords are the system defined words. All keywords have fixed meanings that do not change and which are used only by compiler.
Rules:-
Ø  White spaces are not allowed in keywords.
Keyword may not be used as an identifier.
It is strongly recommended that keywords should be in lower case letters.
There are totally 32(Thirty Two) keywords used in a C programming.

int
float
double
long
short
signed
unsigned
const
if
else
switch
break
default
do
while
for
register
extern
static
struct
typedef
enum
return
sizeof
goto
union
auto
case
void
char
continue
volatile

Constants or Literals: - A constant is an entity that doesn't change during the execution of a program.
 Constants are two types:-
1.      Numeric constant.
2.      Character constant.
Ø  Numeric constant are two types:-
a)      Real constant.
b)      Integer constant.
Real constant:-
·         It must have at least one digit.
·         It must have a decimal point which may be positive or negative.
·         Use of blank space and comma is not allowed between real constants.
·         Example:
+194.143, -416.41
Integer constant:-
·         It must have at least one digit.
·         It should not contain a decimal place.
·         It can be positive or negative.
·         Use of blank space and comma is not allowed between real constants.
·         Example:
1990, 194, -394

Ø  Character constant are two types:-
a)      Character constant.
b)      String constant.

Character constant:-
·         It is a single alphabet or a digit or a special symbol enclosed in a single quotation marks.
·         Maximum length of a character constant is 1.
·         Example:
'T', '9', '$'
String constant:-
·         It is collection of characters enclosed in double quotes.
·         It may contain letters, digits, special characters and blank space.
·         Example:
"Techno well Web Solutions, Single"
Variables:-
a)      It is an identifier (entity) whose value can be change at the execution time of program.
b)      Variable is an identifier which can hold data.
c)      Variable is a name of memory location.
Rules:-
1) The length of variable cannot be more than 8 characters (Compiler dependent).
               Turbo c           8.
               Borland c++ 30
               Vc++             30
               ANSI C         15

Ex: -         int abcdefghijklmno;

2) The name of variable cannot start by digit but it should be letter or alphabet.
            int   1abc;
            int   a1bc ;
            int   abc1 ;
3)  Space is not allowed in variable name.
        int ab  c ;
        int   _abc;
        int   a_bc;
        int   abc_;
        int  ab-c;
 Note:


4) No any special symbols are allowed in variable declaration (except underscore)
     Exam : ‘@’ , ‘#’ ,’$’ ,’>’ , ‘<’ , ‘&’ , ‘!’ ,- - - - - - - - etc .
EX: -                int   ab#c; 
                       int   $abc ;

 5)  Keywords are not allowed.
               int if ; 
               int else ;
Data types: - Stored information is called data and the type of data id called data type.
1.      Data types are keyword in “C” library which is used to allocate the space for data in the main memory (RAM).
2.      Data type is a keyword used to identify type of data.
Ø  In “C” language data types are two types.
a)      Primary data type.
b)      Secondary data type.
Primitive data type:- These are the data type whose variable can hold maximum one value at a time.
These are:-
1.      char.
2.      int.
3.      float.
4.      double.
Keyword
Format Specifier
Size
Data Range
Char
%c
1 Byte
-128 to +127
Int
%d
2 Bytes
-32768 to +32767
Float
%f
4 Bytes
-3.4e38 to +3.4e38
Double
%lf
8 Bytes
-1.7e38 to +1.7e38

Secondary data type:-These are the data type whose variable can hold more than one value at a time. These are two types.
1.      Derived data type.
2.      User defines data type.
Derived data type: - These are the data type whose variable can hold more than one value of similar types.
These are: - arrays, pointers, etc.
User defines data type: - These are the data type whose variable can hold more than one value of dissimilar types.
These are: - structure, union etc.

Range of data type:-








Type qualifiers\ modifiers: - these are the keyword in “C” language which is used to change the current property of data types.
When qualifier is applied to the data type then it changes its size or its size.
Size qualifiers: short, long.
Sign qualifiers: signed, unsigned.
Short: - in general integer data type occupies different sizes based on O.S and based on programming language.

“C” language àwindow (O.S) àint à2bytes.
“C” language àUnix\Linux (O.S)àint à4bytes.
“Java” language àwindow (O.S)àint à4bytes.
Ø  Whenever int data type is preceded by short keyword which allocated two bytes memory space in every O.S and any programming language.
“C” language àwindow (O.S) àshort int à2bytes.
“C” language àUnix\Linux (O.S) àshort int à2bytes.
“Java” language àwindow (O.S)àshort int à2bytes.
Syntax:-
                <short> <data-type>< variable-name>;
Example:-
short int a;àshort a;
Long: - it is used to increase 2 bytes of memory space for the current data type.
Syntax:-
                <long> <data-type>< variable-name>;
Example:-
long int variable-name;
long double variable-name;
Unsigned: - it is used to accept only positive (+) value. Unsigned keyword can be used before any primitive data type.  
Syntax:-
                <unsigned> <data-type>< variable-name>;
Example:-
unsigned int a;
unsigned char a;
unsigned float a;
unsigned double  a;
Signed:- it is default type modifier for any primitive data type which accept both + ve as well as –ve.



Keyword
Format Specifier
Size
Data Range
Char
%c
1 Byte
-128 to +127
unsigned char
<-- -- >
8 Bytes
0 to 255
Int
%d
2 Bytes
-32768 to +32767
long int
%ld
4 Bytes
-231 to +231
unsigned int
%u
2 Bytes
0 to 65535
Float
%f
4 Bytes
-3.4e38 to +3.4e38
Double
%lf
8 Bytes
-1.7e38 to +1.7e38
long double
%Lf
12-16 Bytes
-3.4e38 to +3.4e38

Variable declaration: - it is a process of allocating sufficient space for the data in the memory in terms of variable.
Syntax:-
<Data-type><variable-name>;
·         Data type can be any primitive data type.
·         Variable name can be any user defined name.
Example:-
int a;
·         Memory location is identified with unique name called name.
·         Every memory location is identified with a unique address. These address be any +ve given by O.S.
·         If no user defined value given then by default compiler stores a value in memory location called “garbage value”.
·         Garbage value is a main disadvantage of variable declaration. This problem can overcome using variable initialization.
Variable initialization:- it is a process of storing user defined value in the memory location.
·         The process of giving initial values to variables is called initialization.
Syntax:-
<Data-type><variable-name>=content;

Example:-
int x=20;
·         One variable can be initialized only once in the program.
int x=10;
int x=20;
int b=30;
·         To overcome the disadvantage of variable initialization we can go for variable assignment.
Variable assignment:-it is a process of assigning a value to a variable. Value can be assigned to variable using the assignment operator (=).
Syntax:-
Variable name= expression\variable\constant;
·         Before assigned either variable should be declared or initialized.
·         Variable initialization can be done once in the program.
·         Variable assign can be done multiple times in the program.
Example: -
 int a=20;
int b;
b=30;
b=49;
b=a+7;
Operator: - it is a special symbol used to perform an operation on operands. "Operator is a symbol that is used to perform mathematical operations."
·         In “C” language operator can be classified into following types.
1.      Unary.
2.      Binary.
3.      Ternary.
4.      Special.
Unary operator: - It operates on a single operand. These are classified into three categories.
Ø  The operand must be a variable but not a constant or an expression.
·         Unary minus (-):-it is used to change the current sign of current value.
Syntax:-
Data type Vname=-(value);
·         Unary increment operator (++):-it used to increment or adds one value (1) to current operand or variable. These are two types.
a.      Pre-increment:- whenever increment operator exists before a variable known as pre-increment (prefix) operator.
Syntax:-
               ++ Variable name;
Operation: - in pre-increment (prefix) the value of the variable increment first letter assign will be done.
Example:-
int x=1,b;
b=++x;
b.      Post increment (++):-whenever increment operator exists after a variable known as post-increment (postfix) operator.
Example:-
Variable-name++;
Operation:-the value of variable is assign first, letter increment will be done. 
Example:-
int x=1,b;
b=x++;
·         Unary decrement (--):-it used to decrement or subtracts one value (1) to current operand or variable. These are two types.
1)      Pre-decrement:- whenever increment operator exists before a variable known as pre-decrement (prefix) operator.
Syntax:-
               -- Variable name;
Operation: - in pre-decrement (prefix) the value of the variable decrement first letter assign will be done.
Example:-
int x=1,b
 b=--x;
2)      Post decrement:-whenever increment operator exists after a variable known as post-decrement (postfix) operator.
Example:-
Variable-name--;
Operation:-the value of variable is assign first, letter operation will be done. 
Example:-
int x=1,b;
            b=x--;
Binary operator: - these operators operate on two operands. These are classified into six categories.
a.       Arithmetic operator: - It is used to perform arithmetical operations.
These are:-
+ Addition
-Subtraction
* Multiplication
/ Division
% Modulus


Note:- modulus operator used only on a integer but cannot used on a float.
             -5%2=-1
              5%-2=1

void main()
{
               int a,b,c,d,e,f,g;
               clrscr();
               printf("\n\t Enter First Number :");    // 5
               scanf("%d",&a);
               printf("\n\t Enter Second Number :");   // 2
               scanf("%d",&b);
               c = a + b;
               printf("\n\n\t Addition is : %d",c);   // 7
               d = a - b;
               printf("\n\n\t Subtraction is : %d",d);  // 3
               e = a * b;
               printf("\n\n\t Multiplication is : %d",e);  // 10
               f = a / b;
               printf("\n\n\t Division is : %d",f); // 2
               g = a % b;
               printf("\n\n\t Modulus is : %d",g); // 1
               getch();
}
b.      Relational operator: - It is also used to check conditions. These operators return one (1) if condition is true otherwise 0.
These are:-
< is less than
>is greater than
<=is less than or equal to
>=is greater than or equal to
== is equals to
!=is not equal to
Ø  == Operator always checks only value but not data type of variables.


void main()
{
               int a=6, b=2;
               clrscr();
               printf("\n\n A<=B : %d",(a<=b)); // 0 - False
               printf("\n\n A>B : %d",(a>b)); // 1 - True
               printf("\n\n A!=B : %d",(a!=b)); // 1 - True
               getch();
}
c.       Logical operator:-Sometimes, we have to check more than one condition at a time then it is operator which is primarily used to check more than two conditions. This operator returns one (1) if condition is true otherwise 0.
These are:-
&&à logical AND operator
||à Logical OR operator
! à Logical NOT operator

Truth table for &&                              Truth table for | |                Truth table for! 
Ex1
Ex2
 R
 1
  1
 1
 1
  0
 1
 0
  1
 1
 0
  0
 0
Ex1
Ex2
 R
  1    
  1
  1
  1
  0
  0
  0
  1
  0
  0
  0
  0
   
Ex1
 R
  1
 0
  0
 1

                                    int x=12, y=5, z=7  ;                     

( x > y && z != 15 )                           ( x > y  || z != 15 )              !( x > y )
( x > y && z != 7  )                            ( x > y  ||  z != 7 )               ! ( x == y )     
( x== y && z != 15 )                          ( x== y || z != 15 )              ! ( ! ( x != y ) )   
( x== y && z != 7 )                            ( x== y || z != 7 )                ! ( ! ( z != y ) )
Note: - Logical AND does not checks II exp if I exp is false
            Logical OR does not checks II exp if I exp is true


void main()
{
               int no1=2, no2=5;
               clrscr();
               printf("\n\n %d",(no1 && no2));   // returns 1
               printf("\n\n %d",(no1 || no2));   // returns 1
               getch();
}

Ø  Whenever we are working with logical OR (||) operator, if left side expression is true then right side expression will ignore.
Ø  If the left side expression is false then right side expression will evaluated.


a)                   void main
{
int a,b,c;
a=b=1;
c=++a>1||++b>1;
printf( “a=%d,b=%d,c=%d”,a,b,c);
}

b)                   void main
{
int a,b,c;
a=b=1;
c=a++>1||++b>1;
printf( “a=%d,b=%d,c=%d”,a,b,c);
}

c)                   void main
{   int a,b,c;
    a=b=1;
    c=++a>1||b++>1;
    printf( “a=%d,b=%d,c=%d”,a,b,c);
}

Ø  Whenever we are working with logical AND (&&) operator, if left side expression is true then right side expression will evaluated.
Ø  If the left side expression is false then right side expression not will evaluated.


a)      void main()
{
int a,b,c,d;
a=b=c=5;
d=a++<=1&&++b>=5;
printf(“a=%d,b=%d,c=%d,d=%d”,a,b,c,d);\\à6,5,5,0
}

b)     void main()
{
int a,b,c;
a=b=3;
c=a++>2&&++b>3;
printf(“a=%d,b=%d,c=%d”,a,b,c);\\à4,4,1
}

c)      void main()
{
int a,b,c;
a=b=3;
c=++a>2&&b++<3;
printf(“a=%d,b=%d,c=%d”,a,b,c);\\à4,4,0
}


d.      Assignment operator:-It is used to assign a value to variable. This is represented with “=”.
Syntax:-

Variable name=value\variable\expression;
void main()
{             int a,b;
               clrscr();
               a = 53;
               printf("\n\t Value of A : %d",a);      
               b = a;      // Interchange of value using assignment
               printf("\n\n\t Value of B : %d",b);   
               getch();
}
e.       Short hand operator:-these are subcategory of assignment. These are used to perform mathematical operations on operands and value can be assigned to a variable in terms of expression.
Ø  These are also known as compound assignment operators.
These are:-
int x=3,y=7;
Operators                                                                         Expressions
+=                                                                                    a+=bàa=a+b;à10
-=                                                                                     a-=bàa=a-b; 
*=                                                                                     a*=bàa=a*b;
/=                                                                                      a/=bàa=a/b;     
%=                                                                                    a%=bàa=a%b;

f.       Bitwise operator: - Bitwise operators are used for manipulation of data at a bit level.
These are:-
&     Bitwise AND
|       Bitwise OR
^      Bitwise exclusive OR
~      Bitwise NOT complement
>>   Bitwise Right Shift
<<   Bitwise Left Shift

Truth table for &                             Truth table for |                Truth table for~ 
P
Q
P|Q
 1
  1
 1
 1
  0
 1
 0
  1
 1
 0
  0
 0
P
Q
P&Q
  1    
  1
  1
  1
  0
  0
  0
  1
  0
  0
  0
  0
   
R
~R
  1
 0
  0
 1

      Truth table for P^Q                          
P
Q
P^Q
  1    
  1
  1
  1
  0
  0
  0
  1
  0
  0
  0
  0





P^Q= P.Q+P.Q


<< Bitwise Left Shift Operator: - it can be used to Shift number of bits to the Left side.
Syntax:-
Variable name<< no of shifting bits;
Example: -
void main()
{
 int x=85;
x=x<<3;
printf(“%d”,x);
}



0
1
0
1
0
1
0
1




1
0
1
0
1
0
0
0




>> Right Shift Operator: - it can be used to Shift number of bits to the Right side.
Variable name>> no of shifting bits;
Example: -
void main()
{
 int x=85;
x=x>>2;
printf(“%d”,x);
}



0
1
0
1
0
1
0
1




0
0
0
1
0
1
0
1




3. Ternary operator: - Conditional operator is also called as 'ternary operator.' It is widely used to execute condition in true part or in false part. It operates on three operands. The logical or relational operator can be used to check conditions.
? : (ternary operator)
Syntax:-
Ex1? ex2:ex3;
Ø  In the above syntax ex1 should be condition, ex2 and ex3 can be value\variable\expression\statement.
Ø  Whenever ex1 is true then, ex2 will be executed otherwise ex3 will be execute.
Examples:-

X = 10, Y = 5, Z;
Z= (X>Y)? 100: 200;


3.      Special operator: - there are the some additional special operators in “C” language to perform some particular type of operations.
These are:-
a.      “Address of” operator or Ampersand (&):- it can be used to get\retrieve the address of variable.
Syntax:-
& variable name;
Ø  If & used between two variables that can be treated as Bitwise AND operator.
Ø  If & used before a variable that can be treated as “Address of” operator.
b.      “Sizeof” operator: - it is a unary compile time operator that returns the length of variable in Bytes (integer value) form.
Syntax:-
sizeof(data type);
Ø  To compute the size of data type, you must enclose the data type name in parentheses.
Ø  This is not necessary for variable names.
Like   sizeof variable;
Example:-
void main()
{
   Float f;
printf(“%d”,sizeof f);
printf(“%d”,sizeof(int);
}
Important points:-
1.      Sizeof(int)à2Bytes
2.      Sizeof(12)à2Bytes
3.      Sizeof(40000)à2Bytes
4.      Sizeof(4000L)à4Bytes
5.      Sizeof(float)à4Bytes
6.      Sizeof()à2Bytes
7.      Sizeof(12.8)à8Bytes
8.      Sizeof(12.8F)à4Bytes
9.      Sizeof(12.8L)à10Bytes
10.  Sizeof(3.0)à8Bytes
11.  Sizeof(short)à2Bytes
12.  Sizeof(long)à2Bytes
13.  Sizeof(signed)à2Bytes
14.  Sizeof(unsigned)à2Bytes
15.  Sizeof(char)à1Bytes
Ø  The sizeof char is 1Bytes but the sizeof char constant return ASCII value i.e. an integer value.
int i=5;
ASCII value (American Standard Code for Information Interchange)
Definition: - numeric value of character is called the ASCII value

 ‘A’, ‘B’,   ‘C’, ‘D’ …‘Z’
 65    66     67    68                       90
 ‘a’,   ‘b’, ’c’,’d’ …’z’
  97    98   99 100                       122
  ‘0’, ‘1’, ‘2’, ‘3’………………….’9’    //digit character
 48    49    50    51                         57
  <enter>            13
  < esp>              27
  <space>           32
  <back space>   8
Note: - In case of int compiler directly convert int into binary no and stored in 2 bytes, where in case of char compiler convert the ASCII value of char in binary no and stored in 1 byte.
a.       Sizeof(i)à2Bytes
b.      Sizeof(i*2l)à4Bytes
c.       Sizeof(i/2)à8Bytes
d.      Sizeof(i/2.0f)à4Bytes

c.       Comma operator (,):- it can be used to separate list of variables.
Example:-
int x,y,z;
d.      Dot operator (.)Operator direct member access: - it can be retrieve (access) data members of a structure using structure variable name.
e.       (à) indirect member access operator: - it can be retrieve (access) data members of a structure using addresses.
f.       (*) value at address or asterisk operator: - it can used to identify a pointer variable.
g.      [ ] array index operator:-
h.      ( type) type casting operator:-
Expression evaluation:- while evaluating expression we need to follow associavity and priority.
Priority: - it represents which operator should be evaluating first in the given expression.
Associavity:- it represent the direction in which the expression should be evaluated ,where that is containing more than one operator with same priority.
Operator
Priority
associavity
 ( ),{ },[ ]
1
LàR
!,++,--
2
RàL
%,*,-
3
LàR
+,-
4
LàR
<,<=,>,>=
5
LàR
==,!=
6
LàR
&&
7
LàR
||
8
LàR
? :
9
RàL
=,+=,-=,*=,/=,%=
10
RàL




Delimiters:- A delimiter is a sequence of one or more character used to specify the boundary between separate , independent regions in plain text.
Colon (:)à used for level.
Semicolon (;)à terminates statements.
Parenthesis ( )à Used in expression and function.
Bracket [ ]àused for array declaration.
Curly braces {}àscope of statements.
Hash # àpreprocessor directive.
Comma (,)à Variable separator.
Angel brackets < >à include header files.













No comments: