Unit 2
Unit 2
include <stdio.h>
int main()
{
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
// calculate the sum
sum = number1 + number2;
printf("%d + %d = %d", number1, number2,
sum); return 0;
}
#include<stdio.h>
int main()
{
int num1,num2,div;
printf("\tEnter Two Numbers\n");
printf("---------------------------\n");
printf("Enter First Number : ");
scanf("%d", &num1);
printf("\nEnter Second Number : ");
scanf("%d",&num2);
div=num1/num2;
printf("\nDivision of %d & %d is =
%d",num1,num2,div);
return 0;
}
Modulus Example:
#include <stdio.h>
int main()
{
int x=10;
int y=3;
int result = 10 % 3;
printf("%d\n", result); // Output: 1 return
0; }
Operator Precedence and Associativity
in C:
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal
to
== Equal to
!= Not equal to
// Working of relational operators
#include <stdio.h>
int main() {
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0; }
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
// C program to demonstrate working of relational operators
#include <stdio.h>
int main()
{
int a = 10, b = 4;
// greater than example
if (a > b)
printf("a is greater than b\n");
else
printf("a is less than or equal to b\n");
// greater than equal to
if (a >= b)
printf("a is greater than or equal to b\n");
else
printf("a is lesser than b\n");
// less than example
if (a < b)
printf("a is less than b\n");
else
printf("a is greater than or equal to b\n");
// lesser than equal to
if (a <= b)
printf("a is lesser than or equal to b\n");
else
printf("a is greater than b\n");
// equal to
if (a == b)
printf("a is equal to b\n");
else
printf("a and b are not equal\n");
// not equal to
if (a != b)
printf("a is not equal to b\n");
else
printf("a is equal b\n");
return 0;
}
int main() {
int a = 5;
int b = 7;
int c = 5;
printf("a == b: %d\n", a == b); //use equal to
operator for a and b
printf("a == c: %d\n", a == c); //use equal to
operator for a and c
return 0;
}
Output:
a == b: 0
a == c: 1
Then, we define the main() function, which
serves as the entry point of the program.
Inside the main, we declare
three integer variables, a, b, and c, and assign
them the values 5, 7, and 5, respectively.
Next, as mentioned in code comments, we use
the equal to operator in expression a==b to
compare a and b and the printf() function to
print the comparison result.
We also use the %d format specifier to specify
that the value must be replaced with an integer
and the newline escape sequence to shift the
cursor to the next line once the output is printed.
The output is a == b: 0 because 5 is not equal to
7.
Similarly, we compare a to c and print the result
of the comparison, which is a == c: 1 since 5 is
equal to 5.
Output:a is greater than b
a is greater than or equal to b
a is greater than or equal to b
a is greater than b
a and b are not equal
a is not equal to b
#include <stdio.h>
int main() {
int a = 5;
int b = 7;
int c = 5;
printf("a != b: %d\n", a != b);
printf("a != c: %d\n", a != c);
return 0;
}
Output:
a != b: 1
a != c: 0
There are three integer variables, a,
b, and c, with values 5, 7, and 5, respectively.
We use the not equal to operator inside
the printf() function to compare a to b and
print the comparison result. The output is a !
= b: 1 because 5 is not equal to 7, and the !=
operator checks for inequality, resulting in a
true condition (1).
Similarly, we use the relational operator in
the expression a!=c and the printf() function
to compare and display the result. The output
is a != c: 0 since 5 is equal to 5, and the !=
operator checks for inequality and returns
false, i.e. 0.
The program culminates execution with a
return 0 statement.
Logical Operators
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
X Y X&Y X|Y X^Y
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
explain every function// C Program to demonstrate use of bitwise
operators
#include <stdio.h>
int main() {
// a = 5 (00000101 in 8-bit binary), b = 9 (00001001 in // 8-bit
binary) unsigned int a = 5, b = 9;
// The result is 00000001
printf("a = %u, b = %u\n", a, b);
printf("a&b = %u\n", a & b);
// The result is 00001101
printf("a|b = %u\n", a | b);
// The result is 00001100
printf("a^b = %u\n", a ^ b);
// The result is 11111111111111111111111111111010
// (assuming 32-bit unsigned int)
printf("~a = %u\n", a = ~a);
// The result is 00010010
printf("b<<1 = %u\n", b << 1);
// The result is 00000100
printf("b>>1 = %u\n", b >> 1);
return 0; }
The program uses bitwise operators on two unsigned
integers a and b. Here's a breakdown of the program:
Includes and Main Function:
#include <stdio.h> int main() {
This includes the standard input-output library to use printf.
The main function is the entry point of the program.
Variable Declaration:
unsigned int a = 5, b = 9;
Two unsigned integers a and b are initialized to 5 and 9,
respectively.
Bitwise Operations
Now, let's look at each bitwise operation used in the program:
AND Operator (&):
printf("a&b = %u\n", a & b);
Operation: a & b
Binary Representation:
a = 5 = 00000101
b = 9 = 00001001
Result:00000101
AND each bit: 0&0=0, 0&0=0, 0&1=0, 0&1=0, 0&0=0, 1&0=0,
0&1=0, 1&1=1
Result = 00000001 (1 in decimal).
OR Operator (|):
printf("a|b = %u\n", a | b);
Operation: a | b
Result:
00000101
00001001
OR each bit: 0|0=0, 0|0=0, 0|1=1, 0|1=1, 0|0=0, 1|0=1, 0|1=1, 1|
1=1
Result = 00001101 (13 in decimal).
XOR Operator (^):
printf("a^b = %u\n", a ^ b);
Operation: a ^ b
Result:
00000101
00001001
XOR each bit: 0^0=0, 0^0=0, 0^1=1, 0^1=1, 0^0=0, 1^0=1,
NOT Operator (~):
printf("~a = %u\n", a = ~a);
Operation: ~a
Result:
a (initially 5) = 00000101
NOT operation flips all bits: ~00000101 = 11111010 (in
binary).
Since a is unsigned, this represents 4294967290 (for a
32-bit unsigned int) as a result.
Left Shift Operator (<<):
c
Copy code
printf("b<<1 = %u\n", b << 1);
Operation: b << 1
Result:
Shifts the bits of b (9 or 00001001) to the left by 1 position:
00010010 (18 in decimal).
This effectively multiplies b by 2.
Right Shift Operator (>>):
c
Copy code
printf("b>>1 = %u\n", b >> 1);
Operation: b >> 1
Result:
Shifts the bits of b (9 or 00001001) to the right by 1 position:
00000100 (4 in decimal).
This effectively divides b by 2 (discarding the remainder).
Special operators
1. Comma operator ( ,)
2. sizeof operator – sizeof( )
3. Pointer operators – ( & and *)
4. In the C programming language, special
operators are used to perform specific
operations that cannot be done with
normal arithmetic or logical operators. These
operators are special because they have their
own unique syntax and functionality. In this
blog post, we will explore some of the most
used special operators in C, including
the ternary, bitwise, and comma
operators.mber selection operators – ( . and -
>)
EXAMPLE PROGRAM FOR & AND * OPERATORS
IN C:
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
50
Output: 50
EXAMPLE PROGRAM FOR SIZEOF() OPERATOR IN C:
#include <limits.h>
int main()
{
int a;
char b;
float c;
double d;
printf("Storage size for int data type:%d \n",sizeof(a));
printf("Storage size for char data type:%d \n",sizeof(b));
printf("Storage size for float data type:%d \n",sizeof(c));
printf("Storage size for double data type:%d\n",sizeof(d));
return 0;
}
OUTPUT:
3x2+2x+1 3*x*x+2*x+1
a a/b
b
a b c S=(a+b+c)/2
S=
2
Arithmetic Expressions
Algebraic expression C expression
area= s( s a)( s b)( s c) area=sqrt(s*(s-a)*(s-b)*(s-c))
Sin
b
sin(b/sqrt(a*a+b*b))
2 2
a b
x y
1 xy
2
tow1=sqrt((rowx-rowy)/2+tow*x*y*y)
2
y
2
tow1=sqrt(pow((rowx-rowy)/2,2)+tow*x*y*y)
1 x xy
2
2
y=(alpha+beta)/sin(theta*3.1416/180)+abs(x)
Precedence of operators
BODMAS RULE-
Brackets of Division Multiplication Addition
Subtraction
Brackets will have the highest precedence and
have to be
evaluated first, then comes of , then comes
division, multiplication, addition and finally
subtraction.
C language uses some rules in evaluating the
expressions
and they r called as precedence rules or sometimes
also
referred to as hierarchy of operations, with some
operators
// C Program to illustrate operator
precedence #include <stdio.h>
int main()
{
// printing the value of same expression
printf("10 + 20 * 30 = %d", 10 + 20 * 30);
return 0;
}
Output
10 + 20 * 30 = 610
// C Program to illustrate operator
Associativity #include <stdio.h>
int main()
{
// Verifying the result of the same expression
printf("100 / 5 % 2 = %d", 100 / 5 % 2);
return 0;
}
Output:100 / 5 % 2 = 0
Let’s evaluate the following expression,
100 / 5 % 2
According to the given table, the associativity
of the multiplicative operators is from Left to
Right. So,
(100 / 5) % 2
After evaluation, the expression will be
20 % 2
Now, the % operator will be evaluated.
0
Rules for evaluation of expression
1. First parenthesized sub expression from left to right are
evaluated.
2. If parentheses are nested, the evaluation begins with the
innermost sub expression
3. The precedence rule is applied in determining the order
of application of operators in evaluating sub expressions
4. The associatively rule is applied when 2 or more
operators of the same precedence level appear in a sub
expression.
5. Arithmetic expressions are evaluated from left to right
using the rules of precedence
6. When parentheses are used, the expressions within
parentheses assume highest priority
Hierarchy of operators