0% found this document useful (0 votes)
13 views

LAB 4 Manual

The document introduces various operators in C programming such as arithmetic, relational, logical, and bitwise operators. It provides examples of using each operator type and describes their functionality. It also discusses operator precedence and the sizeof operator. The math.h library functions allow performing mathematical operations on variables in a C program.

Uploaded by

Vikash Shivani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

LAB 4 Manual

The document introduces various operators in C programming such as arithmetic, relational, logical, and bitwise operators. It provides examples of using each operator type and describes their functionality. It also discusses operator precedence and the sizeof operator. The math.h library functions allow performing mathematical operations on variables in a C program.

Uploaded by

Vikash Shivani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CL-1002 LAB - 04

Programming Introduction to operators


Fundamentals and math.h library functions

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES


Fall 2022

NNATIONAL
ATITIO ONALUNIVERSITY
VERSITOF
UNINIV COMPUTER
Y OFOF COMPUTAND
ER EMERGING
ER AND EMERGSCIENCES
INGG
SCICIENCESES
OPERATORS:
C Arithmetic Operators
There are many operators in C for manipulating data which include arithmetic Operators,
Relational Operators, Logical operators and many more which will be discussed
accordingly. Some of the fundamental operators are:
Operators Description Example

+ Adds two operands. A + B = 30

− Subtracts the second operand from the first. A − B = -10

* Multiplies both operands. A * B = 200

/ Divides numerator by de-numerator. B/A=2

% Modulus Operator and the remainder of after an B%A=0


integerdivision.

++ The increment operator increases the integer value by one. A++ = 11

-- The decrement operator decreases the integer value by one. A-- = 9

Example code:
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;

c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);

return 0;
}

C Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.

Relational operators are used in decision making and loops.

Operator Meaning of Operator Example

== Equal to 5 == 3 is evaluated to 0

> Greater than 5 > 3 is evaluated to 1

< Less than 5 < 3 is evaluated to 0

!= Not equal to 5 != 3 is evaluated to 1

INTRODUCTION OF OPERATORS AND MATH.H LIBRARY


2
FUNCTIONS
>= Greater than or equal to 5 >= 3 is evaluated to 1

<= Less than or equal to 5 <= 3 is evaluated to 0

Example code:
#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;
}

INTRODUCTION OF OPERATORS AND MATH.H LIBRARY


2
FUNCTIONS
C Logical Operators
An expression containing a logical operator returns either 0 or 1 depending upon whether the
expression results true or false. Logical operators are commonly used in decision making in C
programming.
Operato Meaning Example
r

&& Logical AND. True only if all If c = 5 and d = 2 then, expression


operands are true ((c==5) && (d>5)) equals to 0.

|| Logical OR. True only if either If c = 5 and d = 2 then, expression


one operand is true ((c==5) || (d>5)) equals to 1.

! Logical NOT. True only if the If c = 5 then, expression !(c==5) equals


operand is 0 to 0.

Example code:

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", result);

return 0;
}

INTRODUCTION OF OPERATORS AND MATH.H LIBRARY


2
FUNCTIONS
Bitwise Operators C
During computation, mathematical operations like: addition, subtraction, multiplication,
division, etc are converted to bit-level which makes processing faster and saves power.
Bitwise operators are used in C programming to perform bit-level operations.
Operators Meaning of operators

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

~ Bitwise complement

<< Shift left

>> Shift right

// C Program to demonstrate use of bitwise operators


#include <stdio.h>
int main()
{
// a = 5(00000101), b = 9(00001001)
unsigned char a = 5, b = 9;

INTRODUCTION OF OPERATORS AND MATH.H LIBRARY


2
FUNCTIONS
// The result is 00000001
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a & b);

// The result is 00001101


printf("a|b = %d\n", a | b);

// The result is 00001100


printf("a^b = %d\n", a ^ b);

// The result is 11111010


printf("~a = %d\n", a = ~a);

// The result is 00010010


printf("b<<1 = %d\n", b << 1);

// The result is 00000100


printf("b>>1 = %d\n", b >> 1);
return 0;
}
Output: a = 5, b = 9
a&b = 1
a|b = 13
a^b = 12
~a = 250
b<<1 = 18
b>>1 = 4

Comma Operator
Comma operators are used to link related expressions together. For example:
int a, c = 5, d;

The sizeof operator


The sizeof is a unary operator that returns the size of data (constants, variables, array,
structure, etc).

Example code:

include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));

return 0;
}

INTRODUCTION OF OPERATORS AND MATH.H LIBRARY


2
FUNCTIONS
Operators Precedence in C

Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has a higher precedence than the addition operator.

INTRODUCTION OF OPERATORS AND MATH.H LIBRARY


2
FUNCTIONS
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right

Additive +- Left to right

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == != Left to right

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left

INTRODUCTION OF OPERATORS AND MATH.H LIBRARY


2
FUNCTIONS
Comma , Left to right

REFERENCE SITE:
https://www.geeksforgeeks.org/operator-precedence-and-associativity-in-c/

Example code:

#include<stdio.h

main() {

int a =
20; int b =
10; int c =
15; int d =
5; int e;

e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n",
e );

e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" ,
e );

e = (a + b) * (c / d); // (30) * (15/5)


printf("Value of (a + b) * (c / d) is : %d\n",
e );

e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" ,
e );

return 0;
}
When compile and execute the above program, it produces the following result:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50

INTRODUCTION OF OPERATORS AND MATH.H LIBRARY


2
FUNCTIONS
Math library functions
Math library functions allow you to perform certain common mathematical calculations.
Functions are normally used in a program by writing the name of the function followed by a
left parenthesis followed by the argument (or a comma-separated list of arguments) of the
function followed by a right parenthesis. For example, to calculate and print the square root
of 900.0 you might write When this statement executes,
printf( "%.2f", sqrt( 900.0 ) );
the math library function sqrt is called to calculate the square root of the number contained in
the parentheses (900.0). The number 900.0 is the argument of the sqrt function. The
preceding statement would print 30.00. The sqrt function takes an argument of type double
and returns a result of type double. All functions in the math library that return floating-point
values return the data type double. Note that double values, like float values, can be output
using the %f conversion specification.

INTRODUCTION OF OPERATORS AND MATH.H LIBRARY


2
FUNCTIONS
Error-Prevention Tip Include the math header by using the preprocessor directive #include
when using functions in the math library
REFERENCE SITE: https://www.geeksforgeeks.org/c-library-math-h-functions/

Exercises

Q 1: Write a C program that accepts an employee's ID, total worked hours of a month and the
amount he received per hour. Print the employee's ID and salary (with two decimal places) of
a particular month.
Output:

INTRODUCTION OF OPERATORS AND MATH.H LIBRARY


2
FUNCTIONS
Input the Employees ID: 0342
Input the working hrs: 8
Salary amount/hr: 15000
Expected Output:
Employees ID = 0342
Salary = 120000.00
Q 2: Write a program that asks the user to enter two numbers, obtains them from the user and
prints their sum, product, difference, quotient and remainder.
Q 3: State the order of evaluation of the operators in each of the following C statements and
show the value of x after each statement is performed.
a) x = 7 + 3 * 6 / 2 - 1;
b) x = 2 % 2 + 2 * 2 - 2 / 2;
c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );
Q 4: Write a C program to input number of days from user and convert it to years, weeks and
days.
Q 5: Write a C program that takes hours and minutes as input, and calculates the total number
of minutes.
Q 6: Write a C program to Find out distance, coordinates of midpoint using distance formula, derived
from Pythagorean Theorem and value of X by Quadratic formula, as follows:

Distance = √((x2-x1)^2 + (y2-y1)^2)

Midpoint= ((x2+x1/2), (y2+y1/2))

Q 7: Write a C program to Find the Roots of a Quadratic Equation. Take user input values of b,a, c

−𝑏±√𝑏2−4𝑎𝑐
𝑥= Given (a≠0.)
2𝑎

Q 8: Write c program that find the result of the following operations: you are allowed to
initialize any values.
Observe and write in comment how that operation produce results.

● 5+4
● 10/2
● True OR False
● 20 MOD 3

INTRODUCTION OF OPERATORS AND MATH.H LIBRARY


2
FUNCTIONS
● 5< 8
● 25 MOD 70
● “A” > “H”
● NOT True
● 25/70
● False AND True
● 20 * 0.5
● 35 <= 35

Q 9: Aiman is fond of collecting different countries’ postage stamps. She is so passionate that
she collected 7 Pakistani stamps, 4 UK stamps, and 3 German and 3 Australian stamps. Based
on the input, identify how many international stamps she has.
Q 10:

INTRODUCTION OF OPERATORS AND MATH.H LIBRARY


2
FUNCTIONS

You might also like