C Programming Operators Questions
The C programming language has a broad range of operators that let it carry out numerous operations on various data types. Operators are signs or words that denote certain operations or calculations to be carried out on operands. It is vital to comprehend how operators function to create effective C programs. We'll look at a few frequently asked questions concerning operators in C programming in this article.
What are operators in C programming?
Operators in C are the keywords and symbols that help find the result after performing the operation between the operands. The C programming language offers a wide range of operators, including arithmetic, relational, logical, assignment, and bitwise operators. Operators help manipulate data, perform computations, and make judgments in the C program.
Let’s discuss the different types of C operators:
Arithmetic Operators
Basic mathematical operations, including addition, subtraction, multiplication, division, and modulus, are carried out in C using arithmetic operators.
Following are the arithmetic operators in C:
- Addition (+): Produces a sum by combining two operands.
- Subtraction (-): Produces a difference by deducting the right operand from the left operand.
- Multiplication (*): Produces a product by multiplying two operands.
- Division (/): Produces a quotient by dividing the operands of the left and right.
- Modulus (%): Produces the remainder after dividing the left and right operands.
Relational Operators
In C, relational operators are used to comparing values and establish the connection between two operands. The comparison outcome determines the Boolean value that they return, which is either true (1) or false (0).
The following are some typical relational operators in C:
- Greater than (>): The greater than (>) operator determines if the value on the left exceeds the value on the right.
- Less than (<): The less-than (>) operator determines whether the value on the left is lower than the value on the right.
- Greater than or equal to (>=): This operator is used to checks whether the value on the left of the operator is greater than or equal to the value on the right of the operator.
- Less than or equal to (<=): It is used to check whether the value on the left is less than or equal to the value on the right.
- Equal to (==): It is used to check whether the values on both sides are equal (==).
- Not equal to (!=): It is used to check whether the values on each side are not equal.
Logical Operators
In C, logical operators combine several conditions to build complex logical expressions. They are generally used with relational operators to make decisions based on various conditions.
Here are a few typical C logical operators:
- Logical AND (&&): If both operands are true, the logical AND (&&) returns true; otherwise, it returns false.
- Logical OR (||): If either operand is true, the logical OR operator (||) returns true; otherwise, it returns false.
- Logical NOT (!): Logical Returns false if the operand is true and true otherwise when using the NOT (!) operator.
Assignment Operators
In C, variables are assigned values using assignment operators. They are used to store variables' values or replace an existing variable's value with a new one. The assignment (=) operator assigns a value to a variable and is the basic assignment operator.
Compound assignment operators in C are also available. These operators combine an assignment with an arithmetic or bitwise operation.
Following aresome more assignment operators:
- Addition assignment (+=): This operator adds the right operand to the left operand and assigns the result to the left operand.
- Subtraction assignment (-=): It assigns the result of the right operand's subtraction to the left operand.
- Multiplication assignment (*=): It assigns the outcome to the left operand after multiplying the left operand by the right operand.
- Division assignment (/=): It assigns the outcome to the left operand after dividing the left operand by the right operand.
- Modulus assignment (%=): It divides the left operand by the right operand, calculates the remainder, and assigns the outcome to the left operand.
Bitwise Operators
The individual bits of integer data types can be processed using bitwise operations using bitwise operators in C.Bitwise operators are frequently used in low-level programming to change specific bits in memory or register values in things like embedded systems, device drivers, and network protocols.
Following are a few typical bitwise operations in C:
- Bitwise AND (&): This operator performs a bitwise AND operation on every pair of corresponding operand bits. If both bits are 1, the outcome is 1. Otherwise, it is 0.
- Bitwise OR (|): This operator performs a bitwise OR operation on every pair of corresponding operand bits. If one of the bits is 1, the outcome is 1, and else it is 0.
- Bitwise XOR (^): Bitwise XOR (exclusive OR) operation is performed on every pair of corresponding operand bits. If the two bits are different (one is 0 and the other is 1), the outcome is 1. Otherwise, it is 0.
- Bitwise NOT (~): This operator inverts the operand's bits. The complement of the operand is the outcome.
Conditional Operators
Conditional Operatorsare also referred to as ternary operators. It offers a convenient shortcut for writing straightforward if-else statements in a single line of code. A condition, a result expression for a true condition, and a result expression for a false condition make up the conditional operator's three operands.
The conditional operator's syntax is as follows:
Syntax:
`(condition) ? expression1 : expression2`
If the condition is true, thenexpression1 is the outcome. Else the outcome is expression2.
Comma Operators
Expressions are separated and evaluated sequentially in C using the comma operator. The right operand, which serves as the result of the complete expression, is evaluated after the left operand, which the comma operator subsequently discards. The comma operator is frequently used to write several expressions in a single statement because it has the lowest precedence of all the C operators.
Let’s discuss questions in c programming operators.
Some Important Questions n C Operators
Question 1
#include <stdio.h>
void main()
{
printf("value is = %d",(16++));
}
Try to solve this.
This question gives an error as L-value is required.
This is because the increment and decrement operators work on a variable.
Question 2
#include <stdio.h>
void main()
{
const char x = 'a';
++x;
printf("%c",var);
}
Try to solve this.
This question gives an error.
This is because the increment and decrement operators work on a variable. We can’t change the const value.
Question 3
#include <stdio.h>
void main()
{
int x=(20 || 40 )&& (10);
printf("x= %d",x);
}
Try to solve this.
The output of this code is 1.
As (20 || 40), both values are non-zero. Hence the outcome is 1, then (1 && 10) again. Both values are non-zero. Hence the outcome is 1.
Question 4
#include <stdio.h>
void main()
{
int x =3,b=2;
a=a==b==0;
printf("%d,%d",a,b);
}
Try to solve this code.
The output of this code is: 1, 2.
Since the == operator evaluates associability from left to right, (a==b) is evaluated first, and since a =3 and b =2, the result is 0.Then, 0==0 assigns 1 to a.Finally, a=1 and b=2.
Question 5
#include <stdio.h>
void main()
{
int x = 10,y;
x= ++x, x++,++x;
printf("Value of x =%d, y =%d",x, y);
}
Try to solve this question.
The output of this question is: Value of x=13, y=11
The assignment operator (=) will be evaluated first since it has higher precedence than the comma operator.In this case, y will be allocated the value ++x before the comma operator is evaluated.
Question 6
#include <stdio.h>
int main()
{
int x, y;
x=- -10;
y=+ +10;
printf("x= %d and y = %d",x, y);
return 0;
}
Find the output of this question.
The output of this code is: x = 10 and y = 10
This expression uses the unary minus (or negation) operator twice. So, here - -10 = 10 and + +10 = 10.
Question 7
#include <stdio.h>
int main()
{
int x,y;
x=(100,200);
y=100,200;
printf("x=%d,y=%d",x,y);
return 0;
}
Try to solve this question.
The output of this code is: x = 200, y = 100
Here, x = (100,200), and 200 will be assigned to x since the () operator takes precedence over the = operators. Since ',' possesses left-to-right associativity, 200 is assigned to x. Whereas the priority of the = operator over the ',' operator is higher, in the case of y=100,200, 100 will be assigned to y.
Question 8
#include <stdio.h>
intmain()
{
int x;
x= (printf("AA")||printf("BB"));
printf("%d",x);
printf("\n");
x= (printf("AA")&&printf("BB"));
printf("%d",x);
}
Try to find the output of this question.
The output is:
AA1
AABB1
In this question, The function printf() returns the total amount of characters, and printf("AA") will return two. Therefore, the second condition, printf("BB"), is true.Both expressions in this statement will run.
Question 9
#include <stdio.h>
void main()
{
int a= 20,b=3,x=0;
x=a+b*a+10/2*a;
printf("value is =%d",x);
}
Solve this question.
The output of this question is: value is = 180
Here, x = 20 + (3 * 20) + (5 * 20) = 20 + 60 + 100 = 180
Question 10
#include <stdio.h>
int main()
{
int a= 2;
int b= 10;
int c;
c=(a & b);
printf("c= %d",c);
return 0;
}
Try to solve this question.
The output of this question is: 2.
This is an example of a bitwise operation. Here, a = 20, i.e., 0010, and b = 1010, so there and is 0010, equal to 2.
Question 11
#include <stdio.h>
int main()
{
int x = -5;
int k = x %4;
printf("%d\n", k);
}
Find the output of the question.
The output of this question is: -1.
Question 12
#include <stdio.h>
int main()
{
int x = 7, y, z;
y = --x;
z = x--;
printf("%d%d%d", x, y, z);
}
Solve this question.
The output is: 5 6 6
Here, the x final value decreases twice, so x is 5, and y is 6. Here the pre-decrement operator is used, i.e., First, the value decrease and is then stored in variable y. Whereas, in z post-decrement operator is used to assign the value, so the earlier value is stored in z, i.e., 6.
Question 13
#include <stdio.h>
int main()
{
unsigned short var = 'A';
var += 2;
var++;
printf("var: %c, %d ", var, var);
}
Try to solve this question.
The output of this question is: var: D, 68
Here, Unsigned short var='A'; var has ASCII value of 'A', which is 65; var+=2 results in var=67; var++ results in var=68; this prints 'D' when the placeholder is %c; and '68' when the placeholder is %d.
Question 14
#include <stdio.h>
int main()
{
int i = -1, j = -1, k = 0, l = 2, m;
m = i++ &&j++&& k++ || l++;
printf("%d %d %d %d %d", i, j, k, l, m);
return 0;
}
Find the output of this question.
The output of this question is: 0 0 1 3 1
The ++ operator is examined first since it has priority over the && and || operators. Consequently, the expression becomes m= 0 && 0 && 1 || 3 i=0. j=0, k=1, and l=3.
&& now comes before ||, and both have left-to-right associativity. Consequently, component 0 && 0 is examined first, and so on.
m=((0 && 0) && 1) || 3) =((0 && 1) || 3) = 1 || 3= 1
Consequently, the value of m=1 i=0 j=0 k=1 l=3.
Question 15
#include <stdio.h>
int main(){
float a;
(int)a= 10;
printf("value of a=%d",a);
return 0;
}
Try to solve this question.
This question gives an error as L-value is required.
The reason for this error is since 10 is also an integer value, (int)a will return an integer constant value; however, constant values cannot be assigned to constant values.
Question 16
#include <stdio.h>
int main() {
int x = 30;
int y = 25;
int max = (x > y)? x : y;
printf("The larger value is: %d", max);
return 0;
}
Find the output of this question.
The output of this question is: The larger value is: 30
Here, we are using the ternary operator to find the max between two given numbers.
Conclusion
Programming in C is only possible with operators, which allow for various operations on operands. Calculations in mathematics are done using arithmetic operators such as addition, subtraction, multiplication, division, and modulus. These operators make C a flexible language for numerical computations by enabling programmers to perform typical mathematical operations in their code. In this article, we will learn the different operator types and practice questions related to C programming.