0% found this document useful (0 votes)
8 views7 pages

C Programming Lab - 2 Operators

The document outlines a C programming lab exercise focused on operators and expressions, detailing various types of operators including arithmetic, assignment, relational, logical, bitwise, and special operators. It includes practical exercises for students to implement these operators in programs, such as finding the biggest and smallest of three numbers, swapping two numbers, and calculating the damped natural frequency of an RLC circuit. Additionally, it provides review questions to reinforce understanding of operator precedence, associativity, and the differences between various operators.

Uploaded by

suhithamohan140
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

C Programming Lab - 2 Operators

The document outlines a C programming lab exercise focused on operators and expressions, detailing various types of operators including arithmetic, assignment, relational, logical, bitwise, and special operators. It includes practical exercises for students to implement these operators in programs, such as finding the biggest and smallest of three numbers, swapping two numbers, and calculating the damped natural frequency of an RLC circuit. Additionally, it provides review questions to reinforce understanding of operator precedence, associativity, and the differences between various operators.

Uploaded by

suhithamohan140
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Programming Lab – Exp.

Exp. No.: 2
Programs using Operators and Expressions
Date:

Aim
To learn and practice various types of operators in C programming by implementing
operations in the expressions.

Theory
Operators in C Programming
An operator is simply a symbol that is used to perform operations. Operators in C are
symbols used to perform operations on variables and values. C supports a wide range of operators,
which can be categorised based on functionality.

➢ There are 3 types of instructions. (1) Type declaration; (2) Arithmetic; (3) Control.
➢ Based on Operands: Unary (+, -, ++, --, !, ~, &, *, sizeof), Binary and Ternary (?:) operators.
➢ The functions of <math.h> only works with and returns ‘float’ and ‘double’.
➢ The compound operators (a combination of two operators) should not have space between them.

1. Arithmetic Operators
These operators perform mathematical arithmetic operations like addition, subtraction, etc.
Operator Description Syntax Values (a=5, b=3)
+ Addition a+b 8
- Subtraction a-b 2
* Multiplication a*b 15
/ Division (aware of divide by zero error) a/b 1 (int); 1.66 (float)
% Modulus (remainder after division) a%b 2
++ Increment by 1 (unary) a++ or ++a a=a+1=6
-- Decrement by 1 (unary) a-- or --a a=a–1=4
➢ If both operands are ‘int’, the result is ‘int. If one operand is ‘float’, the result is ‘float’.
➢ Modulus (%) provides a reminder (the sign is the same as the numerator). It only works with ‘int’
and doesn’t work with ‘float’.

Page 1 of 7
C Programming Lab – Exp. 2

➢ +, - can be used as unary to denote the sign of the number. (eg.: +a, -5).
➢ Postfix ++ or -- (a++, a--), the expression evaluated first, then the variable is incremented or
decremented by one.
➢ Prefix ++ or -- (++a, --a), first the variable is incremented or decremented by one, then the
expression is evaluated.

2. Assignment Operators
These operators are used to assign values to variables (contains ‘=’). It helps to write more
efficient, concise statements with arithmetic operators and assignments.
Operator Description Syntax Full form Values (a=5, b=3)
= Simple assignment a=b - a=3
+= Add and assign a += b a = a + b a=5+3=8
-= Subtract and assign a -= b a=a-b a=5-3=2
*= Multiply and assign a *= b a = a * b a = 5 * 3 = 15
/= Divide and assign a /= b a=a/b a=5/3=1
%= Modulus and assign a %= b a = a % b a=5%3=2

3. Relational Operators
These operators compare two values and return (int) ‘1 (True)’ or ‘0 (False)’.
Operator Description Syntax Values (a=5, b=3)
== Equal to a == b 5 == 3 → 0 (false)
!= Not equal to a != b 5 != 3 → 1 (true)
> Greater than a>b 5 > 3 → 1 (true)
< Less than a<b 5 < 3 → 0 (false)
>= Greater than or equal to a >= b 5 >= 3 → 1 (true)
<= Less than or equal to a <= b 5 <= 3 → 0 (false)

4. Logical Operators
These operators are used to perform logical (gate) operations and return (int) 1 or 0. Generally,
this is used for logic gate operation and to test more than one condition.
Operator Description Syntax Values (a=1, b=0)
&& Logical AND a && b 1 && 0 → 0 (false)
|| Logical OR a || b 1 || 0 → 1 (true)
! Logical NOT (unary) !a !1 → 0 (false)

5. Bitwise Operators
Bitwise operators are used to manipulate bits and perform bit-level operations. These
operators work with ‘int’ data type only.
Operator Description Syntax Values (a=5, b=3) Binary form
& Bitwise AND a&b 5&3=1 0101 & 0011 = 0001
| Bitwise OR a|b 5|3=7 0101 | 0011 = 0111
^ Bitwise XOR a^b 5^3=6 0101 ^ 0011 = 0110
~ Bitwise NOT (unary) ~a ~5 = -6 ~0101 = 1010(signed)
<< Left shift a << 1 5 << 1 = 10 0101 << 1 = 1010
>> Right shift a >> 1 5 >> 1 = 2 0101 >> 1 = 0010

Page 2 of 7
C Programming Lab – Exp. 2

6. Special (Ternary, sizeof, Pointer) Operators


The ternary operator is a shorthand for an if-else statement. It evaluates a condition and returns
one of two values based on the result. The sizeof operator returns the size, in bytes, of a data type or
variable. Pointers use operators to access and manipulate memory addresses.
Operator Description Syntax Values (a=5, b=3)
Conditional (Ternary) (a > b) ? a : b 5
?:
condition ? expr1 : expr2 (a < b) ? a+2 : b+1 4
Returns the size of a type or sizeof(char) 1
sizeof
variable sizeof(int a=5) 4
Address-of operator (unary) Address of ‘a’
& &a
(returns the address of a variable) (e.g., 0x7ffee81fbdc4)
De-reference operator (unary) Value at address ‘p’
* *p
(accesses the value at the address) (e.g., 5 if ‘p’ points to ‘a’)

Expressions in C & Evaluation


An expression is a combination of operators and operands which reduces to a single value.
Precedence: Determines the order in which operators are evaluated.
Associativity: Defines the direction (left-to-right or right-to-left) in which expressions are processed.
Operators Precedence and Associativity
Operator precedence determines the order in which operators are evaluated in an expression.
If more than one operators have the same precedence, associativity decides the evaluation order (left-
to-right or right-to-left).
Precedence Operator Description Associativity
( ), [ ] Parentheses, Array subscript
1 (Highest) Left-to-right
->, . Structure pointer, member access
++, -- increment/decrement
+, - (unary) Unary plus/minus
2 Right-to-left
!, ~ Logical NOT, Bitwise NOT
*, &, sizeof De-reference, Address, Size
3 *, /, % Multiplication, Division, Modulus Left-to-right
4 +, - Addition, Subtraction Left-to-right
5 <<, >> Bitwise shift left/right Left-to-right
6 <, <=, >, >= Relational operators Left-to-right
7 ==, != Equality operators Left-to-right
8, 9, 10 &, ^, | Bitwise AND, XOR, OR Left-to-right
11, 12 &&, || Logical AND, OR Left-to-right
13 ?: Ternary conditional Right-to-left
14 =, +=, -=, *=, /=, %= Assignment operators Right-to-left
15 (Lowest) , Comma Left-to-right
➢ If the parentheses are nested, the innermost parentheses expression will be evaluated first,
followed by the operations within the second innermost pair.
➢ If multiple variable altering operators (like ++, --, +=, *=, …) that may not follow associativity.
That may produce undefined behavioural output or error.

Page 3 of 7
C Programming Lab – Exp. 2

Exercise Program 2a
(2a) Prompt the user to enter 3 different numbers (A, B, C). Write a C program to return the biggest
number and smallest number.
Hints:
➢ Use the conditional operator to find it.
➢ Try to implement both nested conditional operator and logical operator.
I/O (Program 2a) Variable Type Test Case 1 Test Case 2
Input A int 10 10
Input B int 20 2
Input C int 30 5
Output Big int 30 10
Output Big_Variable char C A
Output Small int 10 2
Output Small_Variable char A B

/*(Exp. 2a) Find Biggest and Smallest of 3 numbers*/


#include <stdio.h>
int main()
{
int A, B, C;
int Big, Small;
char Big_var, Small_var;

printf("Enter three different numbers (A, B, C): ");


scanf("%d %d %d", &A, &B, &C);

// Find Big and Small numbers using nested conditional operator


Big = (A > B) ? ((A > C) ? A : C) : ((B > C) ? B : C);
Small = (A < B) ? ((A < C) ? A : C) : ((B < C) ? B : C);
// Find Big and Small variables using nested conditional operator
Big_var = (A > B && A > C) ? 'A' : (B > C ? 'B' : 'C');
Small_var = (A < B && A < C) ? 'A' : (B < C ? 'B' : 'C');

printf("\n The biggest number is: %c = %d", Big_var, Big);


printf("\n The smallest number is: %c = %d", Small_var, Small);
return 0;
}

Page 4 of 7
C Programming Lab – Exp. 2

Exercise Program 2b
(2b) Write a C program to swap two numbers. (Try with and without using the temporary variable)
I/O (Program 2b) Variable Type Test Case 1 Test Case 2
Input A int 10 5
Input B int 20 50
Output A int 20 50
Output B int 10 5

/* Swap two Numbers – using Temporary variable*/


#include <stdio.h>
int main()
{
int A, B, temp;
printf("Enter the value of A: "); scanf("%d", &A);
printf("Enter the value of B: "); scanf("%d", &B);

temp = A;
A = B;
B = temp;
printf("\n After swapping: A = %d, B = %d", A, B);
return 0;
}

/* Swap two Numbers – without Temporary variable*/


#include <stdio.h>
int main()
{
int A, B;
printf("Enter the value of A: "); scanf("%d", &A);
printf("Enter the value of B: "); scanf("%d", &B);

A = A + B;
B = A - B;
A = A - B;
printf("\n After swapping (without temp): A = %d, B = %d", A, B);
return 0;
}

Page 5 of 7
C Programming Lab – Exp. 2

Exercise Program 2c
(2c) Write a C program to calculate the damped natural frequency of an RLC circuit. Also, mention
the type of damping of the circuit.
Hints:
➢ Prompt the user to enter the values of R, L, and C.
➢ R2 > 4L/C, Over-damped; R2 < 4L/C, under-damped; R2 = 4L/C, critically damped
1 𝑅2
➢ Damped natural frequency, 𝑤 = √𝐿𝐶 − 4𝐿2
➢ ‘w’ is imaginary for overdamped. Try to display the error message.
I/O (Program 2c) Variable Type Test Case 1 Test Case 2 Test Case 3
Input R float 4 5 10
Input L float 1 2 0.1
Input C float 0.25 0.1 10e-3
Output Damping type string Critical Under-damped Over-damped
Output Damped frequency, w float 0.00 1.85 0.00

Exercise Program 2d
(2d) Write a C program to calculate the volume of the following shapes. Prompt the user to enter
the values of Radius, Height, Side, etc. (take, PI = 3.14)
I/O (Program 2d) Variable Type Test Case 1 Test Case 2
Input Side, A float 3
Input Radius, R float 5
Input Height, H float 10
Output Cube_Volume float 27
Output Sphere_Volume float 523.33
Output Cylinder_Volume float 785.00
Output Cone_Volume float 261.67

Page 6 of 7
C Programming Lab – Exp. 2

Review Questions & Answers


1) What is the purpose of the modulus (%) operator in C?
It returns the remainder of a division operation. It doesn’t work with ‘float’.
2) What is operator precedence and associativity?
It determines the order in which operators are evaluated in an expression. Associativity defines
the direction in which an expression is evaluated when operators have the same precedence.
3) What is the difference between = and == operators?
= is the assignment operator, while == is the equality comparison operator.
A = 5, which means 5 stored in A. A == 5, which means checks whether A is equal to 5 or not.
4) What is the difference between x++ and ++x?
x++ (post-increment) returns the value of x before incrementing.
++x (pre-increment) increments x first and then returns the new value.
Increment and decrement operators should not be used in float values.
5) What will be the output? int x = 10, y = 3; printf("%d", x / y * y + x % y);
10 (since 10 / 3 = 3, 3 * 3 = 9, and 10 % 3 = 1, so 9 + 1 = 10).
6) What is the ternary (conditional) operator in C?
Syntax: condition ? expr1 : expr2; It is a shorthand for if-else statements. If the condition is true
expression 1 will be evaluated, otherwise expression 2 will be evaluated.
7) Can the logical operator work with numbers other than 1 and 0?
Yes. The logical operator considers any non-zero number as ‘True (1)’. Considers zero as ‘False’.
8) What are compound assignment operators in C?
Compound assignment operators combine arithmetic or bitwise operations with the assignment.
These operators improve code readability and efficiency.
Examples: a += 5 (equivalent to a = a + 5); b *= 3 (equivalent to b = b * 3);
9) What is the difference between the operators ‘!’ and ‘~’ in C?
! is the logical NOT operator. It inverts the truth value of its operand (non-zero becomes 0, and
zero becomes 1). ~ is the bitwise NOT operator. It inverts all the bits of its operand.
10) What is the purpose of the comma operator (,) in C?
The comma operator is used to separate two or more expressions. It evaluates all expressions from
left to right and returns the value of the rightmost expression.
Example: int a = (5, 10); // assigns, a = 10.

Result
The basic concepts of operators and expressions in C programming have been successfully
implemented and tested.

***** ***** *****

Page 7 of 7

You might also like