Unit 2 Notes
Unit 2 Notes
OPERATORS IN C
1. Arithmetic operators:
Used for: It is used for arithmetic calculations like addition, subtraction,
multiplication, division etc.
List of operators: +, -, *, /, % (modulus or mod operator)
2. Relational operators
Used for: Comparing two or more values
List of operators:
< less than
<= less than equal to
> greater than
>= greater than equal to
== is equal to
!= not equal to
3. Logical Operators
Used for: Combining two or more relational expressions to reach a particular decision
List of operators:
&& logical AND binary operators
|| logical OR
! logical NOT unary operator
Logical AND and Logical OR are binary operators, whereas Logical NOT is a
unary operator
These operators also evaluate to either true or false i.e.1 or 0
4. Assignment operator
= is called the assignment operator
It is used to assign value to variables
Syntax: variable=exp
eg: c=a
x=5
z=(a>b)+(5*b)
It is a binary operator
Stores the value of the expression on R.H.S. to the variable on L.H.S
Shorthand representation can be done
like: If a=a+1 then it can be written as
a+=1
Q. What is the difference between prefix and postfix increment and decrement operators?
Ans:
1. When prefix increment or decrement operator is applied to a variable in an expression,
its value is incremented or decremented first and then it is used in the expression.
2. When postfix increment or decrement operator is applied to a variable in an
expression, its value is used first in the expression and then it is incremented or
decremented.
Eg:
int x=5, y=4, z;
z=x++; z=5 x=6
z=--y; z=3 y=3
z=--x; z=5 x=5
z=y++;z=3 y=4
printf(“%d\t%d\t%d”,x,y,z); 5 4 3
6. Conditional operators
It is a ternary operator
The conditional operator is ?:
Used for decision based problems ( if –then-else)
c= (a>=b)?a:b;
7. Bitwise operators
C has a collection of bitwise operators for manipulation of data at bit level
These operators are used for testing the bits, performing AND operation at bit
level, performing OR operation at bit level, shifting them right or left etc.
These operators can only be applied on integer variables.
List of operators:
& bitwise AND
| bitwise OR
^ bitwiseXOR(AB’+A’B)
<< bitwise shift left
>> bitwise shift right
~ bitwise complement unary operator
Eg:
W. A.P to illustrate the use of bitwise operators
/* bitwise operators */
void main()
{
int a=11, b=9, /* a=00001011, b=00001001 */
c; c=a&b;
printf(“%d”,c); /*c=00001001 =9 */
c=a|b;
printf(“%d”,c); /*c=00001011=11*/
c=a^b;
printf(“%d”,c); /*c=00000010=2 */
c=a<<2;
printf(“%d”,c); /*c=00101100= 44 */
c=b>>2;
printf(“%d”,c); /*c=00000010 =2*/
c=~a;
printf(“%d”,c); /*c= 11110101=-12*/
getch();
}
8. Special Operators
List of operators: sizeof() operator, comma (,) operator and pointer operators( &, *)
sizeof() operator -when used with an operand return the number of bytes occupied by the
operand in computer’s memory. The operand can be a data type, a variable or a constant value.
eg:
char a;
c=sizeof(int); 2
c=sizeof(5.4); 4
c=sizeof(a); 1
comma (,) operator – evaluates the list of expression from left to right and the value of the
rightmost expression becomes the value of the combined expression.
Eg: c= a=5,b=6,a+b;
Precedence- Precedence of operators gives the priority of operators i.e tells which operator
should be evaluated first in an expression.
Associativity- Associativity gives the order of evaluation when there are different operators of
same precedence in the expression i.e. tells the order in which to evaluate a expression either
left to right or right to left
Operator Precedence Associativity
() 1 Left to right
++, --, ! 2 Right to left
*, /, % 3 Left to right
+, - 4 Left to right
<,<=,>,>= 5 Left to right
==, != 6 Left to right
&& 7 Left to right
|| 8 Left to right
?: 9 Left to right
Some examples:
expressions: 1. X=
2*3/4+4/4+8-2+5/8
2. K=3/2*4+3/8+3
3. K=9-12/3+3*2-1
4. K=9-12/(3+3)*(2-1)
5. K=9-(12/(3+3)*2)-1
6. K=9-((12/3)+3*2)-1
Solutions:
1. X=2*3/4+4/4+8-2+5/8
X= 6/4+4/4+8-2+5/8
X=1+4/4+8-2+5/8
X=1+1+8-2+5/8
X=1+1+8-2+0
X=2+8-2+0
X=10-2+0
X=8+0
X=8
2. K=3/2*4+3/8+3
K=1*4+3/8+3
K=4+3/8+3
K=4+0+3
K=4+3
K=7
3. K=9-12/3+3*2-1
K=9-4+3*2-1
K=9-4+6-1
K=5+6-1
K=11-1
K=10
4. K=9-12/(3+3)*(2-1)
K=9-12/(6)*(2-1)
K=9-12/6*(1)
K=9-2*1
K=9-2
K=7
5. K=9-(12/(3+3)*2)-1
K=9-(12/(6)*2)-1
K=9-(2*2)-1
K=9-4-1
K=5-1
K=4
6. K=9-((12/3) +3*2)-1
K=9-(4+3*2)-1
K=9-(4+6)-1
K=9-10-1
K=-1-1
K=-2
Q What is Typecasting?
Ans: Typecasting is a way to convert a variable from one datatype to another data type. This is
also called explicit type conversion.
Syntax: (datatype)variable;
Ans: Type conversion – is the conversion of a variable of one datatype to another for
evaluation of a particular expression. It is of two types:
1. The type conversion that is done by The type conversion that is done by the
the compiler itself is called implicit user deliberately is called explicit type
or automatic type conversion conversion or typecasting
int main() {
printf("%d", number);
return 0;
}
// Output: 34
Run Code
Here, we are assigning the double value 34.78 to the integer variable number.
In this case, the double value is automatically converted to integer value 34.
This type of conversion is known as implicit type conversion. In C, there are
two types of type conversion:
1. Implicit Conversion
2. Explicit Conversion
Implicit Type Conversion In C
As mentioned earlier, in implicit type conversion, the value of one type is
automatically converted to the value of another type. For example,
int main() {
return 0;
}
Run Code
Output
The above example has a double variable with a value 4150.12. Notice that
we have assigned the double value to an integer variable.
int number = value;
int main() {
// character variable
char alphabet = 'a';
printf("Character Value: %c\n", alphabet);
return 0;
}
Run Code
Output
Character Value: a
Integer Value: 97
The code above has created a character variable alphabet with the value 'a'.
Notice that we are assigning alphabet to an integer variable.
int number = alphabet;
Here, the C compiler automatically converts the character 'a' to integer 97.
This is because, in C programming, characters are internally stored as integer
values known as ASCII Values.
ASCII defines a set of characters for encoding text in computers. In ASCII
code, the character 'a' has integer value 97, that's why the character 'a' is
automatically converted to integer 97.
If you want to learn more about finding ASCII values, visit find ASCII value of
characters in C.
#include<stdio.h>
int main() {
return 0;
}
Run Code
Output
Integer Value: 35
Double Value: 35.00
We have created an integer variable named number with the value 35 in the
above program. Notice the code,
Here,
int main() {
return 0;
}
Run Code
Output
Integer Value: 97
Character Value: a
We have created a variable number with the value 97 in the code above. Notice
that we are converting this integer to character.
Here,
1. Type Casting:
In typing casting, a data type is converted into another data type by the
programmer using the casting operator during the program design. In typing
casting, the destination data type may be smaller than the source data type
when converting the data type to another data type, that’s why it is also called
narrowing conversion.
Syntax/Declaration:-
destination_datatype = (target_datatype)variable;
UNIT 2
If statement
There are certain situations where the flow of control of the program is based on a decision. In
such situation decision control structures can be used.
In C, we have the following decision control structures:
1. if statement
2. switch-case-default
3. goto
4. conditional operator
if statement-
if-else
if
nested if-else
else if ladder
if-else
Syntax: if(condition)
{
block of statements;
}
else
{
block of statements;
}
/*leap year*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int y;
clrscr( );
printf(“Enter a year\n”);
scanf(“%d”,&y);
if(y%100= =0) /*century year*/
{
if( y %400 ==0)
printf(“Leap year”);
else
printf(“not a leap year”);
/*uppercase or lowercase*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
char ch; clrscr( );
printf( “ Enter an alphabet \n”);
scanf(“%c”,&ch);
if ( (ch>=65) && (ch<=90) printf(“
Uppercase”);
else if ( (ch>=97) && (ch<=122) printf(“
Lowercase”);
else
printf(“ Not an alphabet “);
}
9. W.A.P to enter an alphabet and find whether it is uppercase or lowercase and change
the case of the alphabet
/*roots of quadratic
equation*/ #include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
float a,b,c,r1,r2,x; clrscr( );
printf(“enter coefficients a,b,c\n”);
scanf(“%f%f%f”,&a,&b,&c); if(a= =0)
printf(“not a quadratic equation”); else
{
x=(b*b)-(4*a*c);
if(x= =0)
{
r1= -b/(2*a);
printf(“Equal roots=%f”,r1);
}
else if(x<0)
printf(“Imaginary roots”);
else
{
r1=(-b+sqrt(x))/(2*a);
r2=(-b-sqrt(x))/(2*a);
printf(“Real roots are r1=%f\n r2=%f”,r1,r2);
}
}
getch( );
}
11. W.A.P to find largest among three numbers using conditional operator
12. W.A.P to find whether a year is leap year or not using conditional operator