Part 2
Part 2
Executable Statements:
It contains various executable statements that perform a specific task.
The above two parts must be placed in a pair of braces ({ and }).
Note that each and every statement in a ‘c’ program must end with semicolon except the pre-processor
statements.
Sub-Functions:
This section contains user defined functions.
In general, these functions are placed immediately after the main function.
Logical Operator:
When we want to take a decision basing on 2 (or) more conditions then we will use logical operators.
In three logical operators available in ‘C’ are
Operator Purpose
&& and
|| or
! not
Logical and: (&&)
Logical And results in true if both the conditions are true otherwise the result is false.
Truth Table:
A B A&B
T T T
T F F
F T F
F F F
Truth Table:
A !A
T F
F T
Relational Operators:
Relational Operators are used to perform comparison between two values.
These operators’ returns true (1) if the comparison condition is true otherwise false (0).
The operators used for comparison in ‘C’ are listed below.
Operator Purpose
< Less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= Not equal to
Assignment Operators:
This operator is used to assign a constant value (or) the result of an expression to a variable.
In this operation Right hand side expression is evaluated first and then the result is assigned to left hand side
variable.
Operator Meaning
= Assign Right Hand value to LHS value
+= Value of LHS add to value of RHS and assign it back to the variable in LHS
Eg: a+=2 => a = a+2
-= Value of RHS variable will be subtracted from the value of LHS and assign it back to the
variable in LHS.
Eg: a-=2 => a = a-2
*= Value of LHS variable will be multiplied to the value of RHS and assign it back to the
variable in LHS.
Eg: a*=2 => a=a*2
/= Value of LHS variable will be divided by the value of RHS and assign it back to the
variable in LHS.
Eg: a/=2 => a=a/2
%= The Remainder will be stored back to the LHS after integer, division is carried out
between the LHS variable and RHS variable.
Eg: a%=2 => a=a%2
>>= Right shift and assign the value to the LHS.
Eg: a>> =2 => a=a>>2.
<<= Left shift and assign the value to the LHS.
Eg: a<<=2 => a=a<<2.
&= Bit wise AND operation and assign the value to the LHS.
Eg: a&=b => a=a&b.
|= Bit wise OR operation and assign the value to LHS.
Eg: a|=b => a=a | b
~= Bit wise complement and assign the value to LHS.
Explanation:
In above expression-1, c= ++a two operations namely assignment and increment are involved. Since the
operator ++ is used as prefix, first we perform incrimination i.e., a=a+1 and then this value is assigned to
‘C’.
In the expression-2, c= a++ two operations namely assignment and increment are involved. Since the
operator ++ is used as postfix, first we assign the value of ‘a’ to ‘c’. i.e., c=a => a=5 and then the value of
‘a’ is incremented i.e., a=a+1=6
Bitwise Operators:
Bitwise operators are one of the salient features of ‘C’ language.
These are specially designed to manipulate the data at Bit level.
The Bitwise operators are not applicable for float (or) Double data type.
The following are the some of the Bit wise operators available in ‘C’ language.
Operator Purpose
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive OR
~ Bitwise 1’s component
<< Left Shift
>> Right Shift
Bitwise AND: (&)
To generate a ‘1’ bit in the result, Bitwise AND need a ‘1’ in both the numbers.
Bitwise AND operator is called “MASK” operator.
Eg: a=28, b=17
a =11100
b =10001
a&b =10000
a&b =16
Bitwise OR: (|)
The Bitwise OR result is ‘1’ if at least one of the numbers is ‘1’.
Eg: a=28,b=17
a= 11100
b= 10001
a|b =11101 => a|b = 29
Bitwise Exclusive OR: (^)
It is similar to task Bitwise OR and the result ‘1’ is produced if 1 is present in either but not both.
Eg: a=28, b=17
a= 11100
b= 10001
a^b=01101 => a^b =13
Bitwise 1’s complement: (~)
The complement operator switches all the Bits in a binary pattern i.e., all 0’s becomes 1’s and all 1’s
becomes 0’s.
Eg: i) Bitwise complement of 12 is -13
ii) Bitwise complement of 0 is -1.
Shift Operations:
The shift operations take binary patterns and shift the bits to the left (or) right.
Keeping the same number of bits by dropping the shifted bits at the end and filling in with 0’s at other end.
C-language provides 2 types of shift operators namely left and right shift.
Left Shift: {<<}
This operator is used for left shifting .
Eg: a=00011100
a<<1 = 00111000
a<<2 = 01110000 => a<<2 =112
Right Shift: {>>}
This operator used for right shifting.
Eg: a = 00011100
a>>1 = 00001110
a>>2 = 00000111 => a>>2 = 7
Unary Operator:
These operators require only one operant.
A Unary operator usually precedes their single operand.
Sometimes the unary operators may also follow the operands.
The following are the some of the unary operators available in ‘c’.
Operator Purpose
& Address of a variable
! Negation
++ Increment Operator
sizeof() Size of the subsequent data type in Bytes
Type forced type of conversion
Address Operator: (&)
The Address operator is used to get the address of another variable.
Eg: p= &a
Special Operators:
In addition to the above operators ‘C’ language supports some special operators like comma operator,
parenthesis for grouping expression, membership operators.
Comma Operator:
‘C’ language uses the comma operator is two ways.
The first use of the comma operator is as a separator in variable declaration.
Eg: int a, b, c;
Another use is as an operator in a ‘for’ looping construct.
Expressions:-
An expression is a combination of operators and operands which reduces to a single value. An operator
indicates an operation to be performed on data that yields a value. An operand is a data item on which an
operation is performed.
A simple expression contains only one operator.foe example 3+5 is a simple expression which yields a
value 8, -a is also a single expression . A complex expression contains more than one operator. An example of
complex expression is 6+2*7.
An expression can be divided into six categories based on the number of operators, positions of the
operands and operators, and the precedence of operator.
Expression
Primary Expressions
In C, the operand in the primary expression can be a Name, a Constant, or an Parenthesized Expression.
Name is any identifier for a variable. A constant is the one whose value cannot be changed during program
execution. Any value enclosed within parenthesis must be reduced to single value. A complex Expression can be
converted into primary expression by enclosing it with parenthesis. The following is an example
(3*5+8) ; (c=a=5*c);
Postfix Expressions
The postfix expression consists of one operand and one operator.
Example: A Function Call, The function name is operand and parenthesis is the operator.
The other examples are post increment and post decrement. In post increment the variable is increased by
1, a++ results in the variable increment by 1. Similarly in post decrement, the variable is decreased by 1, a--
results in the variable decreased by 1.
Prefix Expressions
Prefix Expressions consists of one operand and one operator, the operand comes after the operator.
Examples of prefix expressions are prefix increment and prefix decrement i.e., ++a,--a. The only difference
between postfix and prefix operators is, in the prefix operator, the effect take place before the expression that
contains operators is evaluated. It is other way in case of postfix expressions
Unary Expressions
A unary expression is like a prefix expression consists of one operand and one operator and the operand comes
after the operator.
Example: ++a; -b; -c; +d;
Binary Expressions
Binary Expressions are the combinations of two operands and an operator. Any two variables added,
subtracted, multiplied or divided is a binary expression
Example: a+b; c*d;
Ternary Expressions
Ternary Expressions is an expression which consists of a ternary operator pair “?:”
Example: exp1:exp2:exp3;
In the example, if exp1 is true exp2 is executed else exp3 is executed.
Type Conversion:
In some situations, some variables are declared as integers and while performing an operation on these
variables we require the result as floating point type. In such situations we use type conversion.
The type conversion is used to convert the set of declared data types to some other required types.
Conversion can be carried out in 2 ways
o Converting by using assignment (Implicit casting).
o Using cast operator (Explicit casting).
Converting by Assignment:
The usual way of converting a value from one data type to another is by using the assignment operator.
int a;
float b;
a=b;
In this method, if a larger data type value is assigned to smaller data type then the value will be truncated
{cut off}. This method is called “Narrowing”.
If a smaller data type is assigned to larger data type, this is no loss of data. This method is called
“Widening”.
Cast Operator:
The cast operator is a technique to forcefully to convert one data type to another.
The operator used to force this conversion is known as “Cast Operator” and the process is known as type
casting.
Syntax: x= (cast) expression
(or)
X= cast (expression)
Eg: int a, b;
float f;
f= (float) a/b;
(or)
f= float (a)/b;