FoodForFutureGeeks

Sunday 1 April 2012

Common Terminologies of C

Token:

A token is the smallest divisible unit of a c program, while processing a C program, the compiler divides the program into numerous meaningful tokens.Token may be a keyword, variable or operator.

ex:
int a; here there are three tokens int,a,;

Keyword:

keywords are those words which have a pre defined meaning, this meaning cannot be changed by the programmer.
ex:
int means an integer datatype, the programmer cannot change this meaning.
void this means the return type is null etc.

Variable:

a variable is the name given to a programmer defined value, the value can be used with this name, as the name suggests the value of a variable can change during the process of execution of a program.
ex:
int value=10;
printf("%d",value);

Operators:

operators are the are used to perform certain operations on variables, there are two types of operators:
  1. Unary operators.
  2. Binary operators.

Unary operators:

unary operators act upon a single variable/operand.
ex:


increment operators:
pre increment ++i; increases the value of i by 1
post increment i++; increments the value of i by 1 fter the execution of instruction.

Decrement operators:

pre decrement --i; decreases the value of i by 1
post decrement decreases the value of i by 1 after the execution of instruction.

Note: evaluate expressions like a+(++b); a+b++; ,a-k++;a-++k and see results in the below program:void main(){int a=0,k=10;printf("%d",a-k++);}

Binary Operators:

binary operators are those which can work on two operands/variables:ex:a+b, a-b

+,-,*,/ are the commonly used binary operators.
+:perform addition.
-:perform subtraction.
*:perform multiplication.
/:perform divison.


Intersting binary operators:

(<<) left shift:

shifts the given variable by given no places to left.shifts the given variable by given no places to left.ex:5<<2 =20 ie it is multiplied by 2power2.10<<2=40 ie 10 is multiplied by 4.
(>>)Right Shift:

shifts the given variable by given no of places to right.ex:4>>2=1 ie 4 is divide by 2square8>>3=1 ie 8 is divided by 2 cube.

Logical Operators:

bitwise and: &

performs the and operation after converting the variables to binary:ex:int k=1&0 result k=0if used on boolean variables it acts as logical and.

bitwise or: | pipe symbol, above enter key usually:

performs or operation after converting to binary
ex:int k=1|0; result k=1;if used on boolean variables it acts as logical or.
Logical And &:

should be used on boolean values or expressions,returns true if both right hand side and left hand side of the expression are true else it return false.ex:if(k>0 && i=10){{}}if will be executed if both k>0 and i=10.if used on variables acts as bitwise and.
Logical OR || :

should be used on boolean values or expressions,returns true if either right hand side orleft hand side of the expression are true, if both are false it returns false.ex:if(k>0 || i=10){}if will be executed if either k>0 or i=10.if used on variables acts as bitwise or.

Special Bitwise Operator( ?:) :
(<<) left shift:

var=(condition)?exp1:exp2
ex:evaluates the condition if condition is true, var is assigned the value mentioned in exp1, if condition is false var is assigned value mentioned in exp2.

k=(a>1)?0:10;

var=(condition)?exp1:exp2


ex:evaluates the condition if condition is true, var is assigned the value mentioned in exp1, if condition is false var is assigned value mentioned in exp2.
k=(a>1)?0:10;




sample program:

#include<stdio.h>
void main()
{
int a=1,k=0;
k=(a&gt;0)?10:0;
printf("%d",k);
}
here if a is greater than 0, k is assigned 10 else k is assigned value 0 as per expression.













 



No comments:

Post a Comment