The Wayback Machine - https://web.archive.org/web/20241109033723/https://www.geeksforgeeks.org/operators-in-c/
Open In App

Operators in C

Last Updated : 11 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In C language, operators are symbols that represent operations to be performed on one or more operands. They are the basic components of the C programming. In this article, we will learn about all the built-in operators in C with examples.

What is a C Operator?

An operator in C can be defined as the symbol that helps us to perform some specific mathematical, relational, bitwise, conditional, or logical computations on values and variables. The values and variables used with operators are called operands. So we can say that the operators are the symbols that perform operations on operands.

Operators-in-C

For example,

c = a + b;


Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’. To dive deeper into how operators are used with data structures, the C Programming Course Online with Data Structures covers this topic thoroughly.

Types of Operators in C

C language provides a wide range of operators that can be classified into 6 types based on their functionality:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Other Operators

1. Arithmetic Operations in C

The arithmetic operators are used to perform arithmetic/mathematical operations on operands. There are 9 arithmetic operators in C language:

S. No.

Symbol

OperatorDescription

Syntax

1

+

Plus

Adds two numeric values.

a + b

2

–

Minus

Subtracts right operand from left operand.

a – b

3

*

Multiply

Multiply two numeric values.

a * b

4

/

Divide

Divide two numeric values.

a / b

5

%

Modulus

Returns the remainder after diving the left operand with the right operand.

a % b

6

+

Unary Plus

Used to specify the positive values.

+a

7

–

Unary Minus

Flips the sign of the value.

-a

8

++

Increment

Increases the value of the operand by 1.

a++

9

—

Decrement

Decreases the value of the operand by 1.

a–

Example of C Arithmetic Operators

C
// C program to illustrate the arithmatic operators
#include <stdio.h>

int main()
{

    int a = 25, b = 5;

    // using operators and printing results
    printf("a + b = %d\n", a + b);
    printf("a - b = %d\n", a - b);
    printf("a * b = %d\n", a * b);
    printf("a / b = %d\n", a / b);
    printf("a % b = %d\n", a % b);
    printf("+a = %d\n", +a);
    printf("-a = %d\n", -a);
    printf("a++ = %d\n", a++);
    printf("a-- = %d\n", a--);

    return 0;
}

Output
a + b = 30
a - b = 20
a * b = 125
a / b = 5
a % b = 0
+a = 25
-a = -25
a++ = 25
a-- = 26

2. Relational Operators in C

The relational operators in C are used for the comparison of the two operands. All these operators are binary operators that return true or false values as the result of comparison.

These are a total of 6 relational operators in C:

S. No.

Symbol

OperatorDescription

Syntax

1

<

Less thanReturns true if the left operand is less than the right operand. Else false a < b

2

>

Greater thanReturns true if the left operand is greater than the right operand. Else false a > b

3

<=

Less than or equal toReturns true if the left operand is less than or equal to the right operand. Else false a <= b

4

>=

Greater than or equal toReturns true if the left operand is greater than or equal to right operand. Else false a >= b

5

==

Equal toReturns true if both the operands are equal.a == b

6

!=

Not equal toReturns true if both the operands are NOT equal.a != b

Example of C Relational Operators

C
// C program to illustrate the relational operators
#include <stdio.h>

int main()
{

    int a = 25, b = 5;

    // using operators and printing results
    printf("a < b  : %d\n", a < b);
    printf("a > b  : %d\n", a > b);
    printf("a <= b: %d\n", a <= b);
    printf("a >= b: %d\n", a >= b);
    printf("a == b: %d\n", a == b);
    printf("a != b : %d\n", a != b);

    return 0;
}

Output
a < b  : 0
a > b  : 1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1

Here, 0 means false and 1 means true.

3. Logical Operator in C

Logical Operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. The result of the operation of a logical operator is a Boolean value either true or false.

S. No.

Symbol

OperatorDescription

Syntax

1

&&

Logical ANDReturns true if both the operands are true.

a && b

2

||

Logical ORReturns true if both or any of the operand is true.

a || b

3

!

Logical NOTReturns true if the operand is false.

!a

Example of Logical Operators in C

C
// C program to illustrate the logical operators
#include <stdio.h>

int main()
{

    int a = 25, b = 5;

    // using operators and printing results
    printf("a && b : %d\n", a && b);
    printf("a || b : %d\n", a || b);
    printf("!a: %d\n", !a);

    return 0;
}

Output
a && b : 1
a || b : 1
!a: 0

4. Bitwise Operators in C

The Bitwise operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands. Mathematical operations such as addition, subtraction, multiplication, etc. can be performed at the bit level for faster processing.

There are 6 bitwise operators in C:

S. No.

Symbol

OperatorDescription

Syntax

1

&

Bitwise ANDPerforms bit-by-bit AND operation and returns the result.a & b

2

|

Bitwise ORPerforms bit-by-bit OR operation and returns the result.a | b

3

^

Bitwise XORPerforms bit-by-bit XOR operation and returns the result.a ^ b

4

~

Bitwise First ComplementFlips all the set and unset bits on the number.~a

5

<<

Bitwise LeftshiftShifts the number in binary form by one place in the operation and returns the result.a << b

6

>>

Bitwise RightshilftShifts the number in binary form by one place in the operation and returns the result.a >> b

Example of Bitwise Operators

C
// C program to illustrate the bitwise operators
#include <stdio.h>

int main()
{

    int a = 25, b = 5;

    // using operators and printing results
    printf("a & b: %d\n", a & b);
    printf("a | b: %d\n", a | b);
    printf("a ^ b: %d\n", a ^ b);
    printf("~a: %d\n", ~a);
    printf("a >> b: %d\n", a >> b);
    printf("a << b: %d\n", a << b);

    return 0;
}

Output
a & b: 1
a | b: 29
a ^ b: 28
~a: -26
a >> b: 0
a << b: 800

5. Assignment Operators in C

Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error.

The assignment operators can be combined with some other operators in C to provide multiple operations using single operator. These operators are called compound operators.

In C, there are 11 assignment operators :

S. No.

Symbol

OperatorDescription

Syntax

1

=

Simple AssignmentAssign the value of the right operand to the left operand.

a = b

2

+=

Plus and assignAdd the right operand and left operand and assign this value to the left operand.

a += b

3

-=

Minus and assignSubtract the right operand and left operand and assign this value to the left operand.

a -= b

4

*=

Multiply and assignMultiply the right operand and left operand and assign this value to the left operand.

a *= b

5

/=

Divide and assignDivide the left operand with the right operand and assign this value to the left operand.

a /= b

6

%=

Modulus and assignAssign the remainder in the division of left operand with the right operand to the left operand.

a %= b

7

&=

AND and assignPerforms bitwise AND and assigns this value to the left operand.

a &= b

8

|=

OR and assignPerforms bitwise OR and assigns this value to the left operand.

a |= b

9

^=

XOR and assignPerforms bitwise XOR and assigns this value to the left operand.

a ^= b

10

>>=

Rightshift and assignPerforms bitwise Rightshift and assign this value to the left operand.

a >>= b

11

<<=

Leftshift and assignPerforms bitwise Leftshift and assign this value to the left operand.

a <<= b

Example of C Assignment Operators

C
// C program to illustrate the assignment operators
#include <stdio.h>

int main()
{
    int a = 25, b = 5;

    // using operators and printing results
    printf("a = b: %d\n", a = b);
    printf("a += b: %d\n", a += b);
    printf("a -= b: %d\n", a -= b);
    printf("a *= b: %d\n", a *= b);
    printf("a /= b: %d\n", a /= b);
    printf("a %%= b: %d\n", a %= b);
    printf("a &= b: %d\n", a &= b);
    printf("a |= b: %d\n", a |= b);
    printf("a >>= b: %d\n", a >>= b); 
    printf("a <<= b: %d\n", a <<= b);

    return 0;
}

Output
a = b: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0
a &= b: 0
a |= b: 5
a >>= b: 0
a <<= b: 0

6. Other Operators

Apart from the above operators, there are some other operators available in C used to perform some specific tasks. Some of them are discussed here: 

sizeof Operator

  • sizeof is much used in the C programming language.
  • It is a compile-time unary operator which can be used to compute the size of its operand.
  • The result of sizeof is of the unsigned integral type which is usually denoted by size_t.
  • Basically, the sizeof the operator is used to compute the size of the variable or datatype.

Syntax

sizeof (operand)


To know more about the topic refer to this article.

Comma Operator ( , )

  • The comma operator (represented by the token) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type).
  • The comma operator has the lowest precedence of any C operator.
  • Comma acts as both operator and separator. 

Syntax

operand1 , operand2 


To know more about the topic refer to this article.

Conditional Operator ( ? : )

  • The conditional operator is the only ternary operator in C++.
  • Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then we will execute and return the result of Expression2 otherwise if the condition(Expression1) is false then we will execute and return the result of Expression3.
  • We may replace the use of if..else statements with conditional operators.

Syntax

operand1 ? operand2 : operand3;


To know more about the topic refer to this article.

dot (.) and arrow (->) Operators

  • Member operators are used to reference individual members of classes, structures, and unions.
  • The dot operator is applied to the actual object. 
  • The arrow operator is used with a pointer to an object.

Syntax

structure_variable . member;


and

structure_pointer -> member;


To know more about dot operators refer to this article and to know more about arrow(->) operators refer to this article.

Cast Operator

  • Casting operators convert one data type to another. For example, int(2.2000) would return 2.
  • A cast is a special operator that forces one data type to be converted into another. 
  • The most general cast supported by most of the C compilers is as follows −   [ (type) expression ].

Syntax

(new_type) operand;


To know more about the topic refer to this article.

addressof (&) and Dereference (*) Operators

  • Pointer operator & returns the address of a variable. For example &a; will give the actual address of the variable.
  • The pointer operator * is a pointer to a variable. For example *var; will pointer to a variable var. 

To know more about the topic refer to this article.

Example of Other C Operators

C
// C Program to demonstrate the use of Misc operators
#include <stdio.h>

int main()
{
    // integer variable
    int num = 10;
    int* add_of_num = &num;

    printf("sizeof(num) = %d bytes\n", sizeof(num));
    printf("&num = %p\n", &num);
    printf("*add_of_num = %d\n", *add_of_num);
    printf("(10 < 5) ? 10 : 20 = %d\n", (10 < 5) ? 10 : 20);
    printf("(float)num = %f\n", (float)num);

    return 0;
}

Output
sizeof(num) = 4 bytes
&num = 0x7ffe2b7bdf8c
*add_of_num = 10
(10 < 5) ? 10 : 20 = 20
(float)num = 10.000000

Unary, Binary and Ternary Operators in C

Operators can also be classified into three types on the basis of the number of operands they work on:

  1. Unary Operators: Operators that work on single operand.
  2. Binary Operators: Operators that work on two operands.
  3. Ternary Operators: Operators that work on three operands.

Operator Precedence and Associativity in C

In C, it is very common for an expression or statement to have multiple operators and in these expression, there should be a fixed order or priority of operator evaluation to avoid ambiguity.

Operator Precedence and Associativity is the concept that decides which operator will be evaluated first in the case when there are multiple operators present in an expression.

The below table describes the precedence order and associativity of operators in C. The precedence of the operator decreases from top to bottom. 

Precedence

Operator

Description

Associativity

1

()

Parentheses (function call)

left-to-right

[]

Brackets (array subscript)

left-to-right

.

Member selection via object name

left-to-right

->

Member selection via a pointer

left-to-right

a++ , a–

Postfix increment/decrement (a is a variable)

left-to-right

2

++a , –a

Prefix increment/decrement (a is a variable)

right-to-left

+ , –

Unary plus/minus

right-to-left

! , ~

Logical negation/bitwise complement

right-to-left

(type)

Cast (convert value to temporary value of type)

right-to-left

*

Dereference

right-to-left

&

Address (of operand)

right-to-left

sizeof

Determine size in bytes on this implementation

right-to-left

3

* , / , %

Multiplication/division/modulus

left-to-right

4

+ , –

Addition/subtraction

left-to-right

5

<< , >>

Bitwise shift left, Bitwise shift right

left-to-right

6

< , <=

Relational less than/less than or equal to

left-to-right

> , >=

Relational greater than/greater than or equal to

left-to-right

7

== , !=

Relational is equal to/is not equal to

left-to-right

8

&

Bitwise AND

left-to-right

9

^

Bitwise XOR

left-to-right

10

|

Bitwise OR

left-to-right

11

&&

Logical AND

left-to-right

12

||

Logical OR

left-to-right

13

?:

Ternary conditional

right-to-left

14

=

Assignment

right-to-left

+= , -=

Addition/subtraction assignment

right-to-left

*= , /=

Multiplication/division assignment

right-to-left

%= , &=

Modulus/bitwise AND assignment

right-to-left

^= , |=

Bitwise exclusive/inclusive OR assignment

right-to-left

<<=, >>=

Bitwise shift left/right assignment

right-to-left

15

,

expression separator

left-to-right

To know more about operator precedence and associativity, refer to this article – Operator Precedence and Associativity in C

Conclusion

In this article, the points we learned about the operator are as follows:

  • Operators are symbols used for performing some kind of operation in C.
  • There are six types of operators, Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, Assignment Operators, and Miscellaneous Operators.
  • Operators can also be of type unary, binary, and ternary according to the number of operators they are using.
  • Every operator returns a numerical value except logical, relational, and conditional operator which returns a boolean value (true or false).
  • There is a Precedence in the operators means the priority of using one operator is greater than another operator.

FAQs on C Operators

Q1. What are operators in C?

Answer:

Operators in C are certain symbols in C used for performing certain mathematical, relational, bitwise, conditional, or logical operations for the user.

Q2. What are the 7 types of operators in C?

Answer:

There are 7 types of operators in C as mentioned below:

  • Unary operator
  • Arithmetic operator
  • Relational operator
  • Logical operator
  • Bitwise operator
  • Assignment operator
  • Conditional operator

Q3. What is the difference between the ‘=’ and ‘==’ operators?

Answer:

‘=’ is a type of assignment operator that places the value in right to the variable on left, Whereas ‘==’ is a type of relational operator that is used to compare two elements if the elements are equal or not.

Q4. What is the difference between prefix and postfix operators in C?

Answer:

In prefix operations, the value of a variable is incremented/decremented first and then the new value is used in the operation, whereas, in postfix operations first the value of the variable is used in the operation and then the value is incremented/decremented.

Example:

b=c=10;
a=b++; // a==10
a=++c; // a==11

Q5. What is the Modulo operator?

Answer:

The Modulo operator(%) is used to find the remainder if one element is divided by another.

Example:

a % b (a divided by b)
5 % 2 == 1


Previous Article
Next Article

Similar Reads

C | Operators | Question 2
#include <stdio.h> int main() {     int i = 1, 2, 3;          printf("%d", i);          return 0; } (A) 1 (B) 3 (C) Garbage value (D) Compile time error Answer: (D) Explanation: Comma acts as a separator here. The compiler creates an integer variable and initializes it with 1. The compiler fails to create integer variable 2 because 2 is not a
1 min read
C | Operators | Question 3
#include <stdio.h> int main() {     int i = (1, 2, 3);          printf("%d", i);          return 0; } (A) 1 (B) 3 (C) Garbage value (D) Compile time error Answer: (B) Explanation: The bracket operator has higher precedence than assignment operator. The expression within bracket operator is evaluated from left to right but it is always the res
1 min read
C | Operators | Question 4
#include <stdio.h> int main() {     int i;        i = 1, 2, 3;     printf("%d", i);          return 0; } (A) 1 (B) 3 (C) Garbage value (D) Compile time error Answer: (A) Explanation: Comma acts as an operator. The assignment operator has higher precedence than comma operator. So, the expression is considered as (i = 1), 2, 3 and 1 gets assign
1 min read
C | Operators | Question 5
C/C++ Code #include int main() { int i = 3; printf(\"%d\", (++i)++); return 0; } What is the output of the above program? (A)3 (B)4 (C)5 (D)Compile-time error Answer: (D)Explanation: In C, prefix and postfix operators need l-value to perform operation and return r-value. The expression (++i)++ when executed increments the value of variable i(i is a
1 min read
C | Operators | Question 6
What is the output of below program? #include <stdio.h> int foo(int* a, int* b) { int sum = *a + *b; *b = *a; return *a = sum - *b; } int main() { int i = 0, j = 1, k = 2, l; l = i++ || foo(&j, &k); printf("%d %d %d %d", i, j, k, l); return 0; } (A) 1 2 1 1 (B) 1 1 2 1 (C) 1 2 2 1 (D) 1 2 2 2 Answer: (A) Explanation: The con
1 min read
C | Operators | Question 7
#include &lt;stdio.h&gt; int main() { int i = 5, j = 10, k = 15; printf(&quot;%d &quot;, sizeof(k /= i + j)); printf(&quot;%d&quot;, k); return 0; } Assume size of an integer as 4 bytes. What is the output of above program? (A) 4 1 (B) 4 15 (C) 2 1 (D) Compile-time error Answer: (B) Explanation: The main theme of the program
1 min read
C | Operators | Question 8
#include <stdio.h> int main() { //Assume sizeof character is 1 byte and sizeof integer is 4 bytes printf("%d", sizeof(printf("GeeksQuiz"))); return 0; } (A) GeeksQuiz4 (B) 4GeeksQuiz (C) GeeksQuiz9 (D) 4 (E) Compile-time error Answer: (D) Explanation: An expression doesn't get evaluated inside sizeof operator. GeeksQuiz wi
1 min read
C | Operators | Question 9
Output of following program? #include <stdio.h> int f1() { printf ("Geeks"); return 1;} int f2() { printf ("Quiz"); return 1;} int main() { int p = f1() + f2(); return 0; } (A) GeeksQuiz (B) QuizGeeks (C) Compiler Dependent (D) Compiler Error Answer: (C) Explanation: The operator ‘+’ doesn't have a standard defined order o
1 min read
C | Operators | Question 10
What is the output of following program? #include <stdio.h> int main() { int a = 1; int b = 1; int c = a || --b; int d = a-- && --b; printf("a = %d, b = %d, c = %d, d = %d", a, b, c, d); return 0; } (A) a = 0, b = 1, c = 1, d = 0 (B) a = 0, b = 0, c = 1, d = 0 (C) a = 1, b = 1, c = 1, d = 1 (D) a = 0, b = 0, c = 0, d = 0 Ans
2 min read
C | Operators | Question 11
#include <stdio.h> int main() { int a = 10, b = 20, c = 30; if (c > b > a) printf("TRUE"); else printf("FALSE"); return 0; } (A) TRUE (B) FALSE (C) Compiler Error (D) Output is compiler dependent Answer: (B) Explanation: Let us consider the condition inside the if statement. Since there are two greater than (>) opera
1 min read
C | Operators | Question 12
#include<stdio.h> int main() { char *s[] = { "knowledge","is","power"}; char **p; p = s; printf("%s ", ++*p); printf("%s ", *p++); printf("%s ", ++*p); return 0; } (A) is power (B) nowledge nowledge s (C) is ower (D) nowledge knowledge is Answer: (B) Explanation: Let us consider the
1 min read
C | Operators | Question 13
C/C++ Code #include int main(void) { int a = 1; int b = 0; b = a++ + a++; printf(\"%d %d\",a,b); return 0; } (A) 3 6 (B) Compiler Dependent (C) 3 4 (D) 3 3 Answer: (B) Explanation: It is compiler dependent as different compilers takes different bytes for integers. Quiz of this Question Please comment below if you find anything wrong in the above po
1 min read
C | Operators | Question 27
Which of the following is not a logical operator? (A)&& (B)! (C)|| (D)| Answer: (D)Explanation: &&- Logical AND !- Logical NOT ||- Logical OR |- Bitwise OR(used in bitwise manipulations) Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
C | Operators | Question 15
Predict the output of following program. Assume that the characters are represented using ASCII Values. #include <stdio.h> #define VAL 32 int main() { char arr[] = "geeksquiz"; *(arr + 0) &= ~VAL; *(arr + 5) &= ~VAL; printf("%s", arr); return 0; } (A) GeeksQuiz (B) geeksQuiz (C) Geeksquiz (D) geeksquiz (E) Garbage
1 min read
C | Operators | Question 16
Predict the output of the below program: #include <stdio.h> int main() { printf("%d", 1 << 2 + 3 << 4); return 0; } (A) 112 (B) 52 (C) 512 (D) 0 Answer: (C) Explanation: The main logic behind the program is the precedence and associativity of the operators. The addition(+) operator has higher precedence than shift(<
1 min read
C | Operators | Question 17
Which of the following can have different meaning in different contexts? (A) & (B) * (C) Both of the above (D) There are no such operators in C Answer: (C) Explanation: '&' can be used to get address of a variable and can also be used to do bitwise and operation. Similarly '*' can be used to get value at an address and can also be used to m
1 min read
C | Operators | Question 18
In C, two integers can be swapped using minimum (A) 0 extra variable (B) 1 extra variable (C) 2 extra variable (D) 4 extra variable Answer: (A) Explanation: We can swap two variables without any extra variable using bitwise XOR operator '^'. Let X and Y be two variables to be swapped. Following steps swap X and Y. X = X ^ Y; Y = X ^ Y; X = X ^ Y; S
1 min read
C | Operators | Question 26
What does the following statement do? x = x | 1 << n; (A) Sets x as 2n (B) Sets (n+1)th bit of x (C) Toggles (n+1)th bit of x (D) Unsets (n+1)th bit of x Answer: (B) Explanation: Let n be 3, the value of expression 1 << n would be 8 (In binary 00001000). If we do bitwise or of binary 00001000 with any number x would set its (n+1)th bit
1 min read
C | Operators | Question 27
Predict the output of following C program C/C++ Code #include int main() { int i = 0; do { printf(\"GeeqsQuiz \"); i = i++; } while (i (A) GeeqsQuiz GeeqsQuiz GeeqsQuiz GeeqsQuiz GeeqsQuiz (B) Infinite time GeeksQuiz (C) Undefined Behavior (D) None of the above Answer: (C) Explanation: The below statement causes undefined behavior. i = i++; Quiz of
1 min read
C | Operators | Question 21
Assume that the size of an integer is 4 bytes, predict the output of following program. #include <stdio.h> int main() { int i = 12; int j = sizeof(i++); printf("%d %d", i, j); return 0; } (A) 12 4 (B) 13 4 (C) Compiler Error (D) 0 4 Answer: (A) Explanation: The expressions written inside sizeof are not evaluated, so i++ is not perfo
1 min read
C | Operators | Question 22
#include<stdio.h> int main() { int a = 2,b = 5; a = a^b; b = b^a; printf("%d %d",a,b); return 0; } (A) 5 2 (B) 2 5 (C) 7 7 (D) 7 2 Answer: (D) Explanation: ^ is bitwise xor operator. a = 2 (10) b = 5 (101) a = a^b (10 ^ 101) = 7(111) b = a^b (111 ^ 101) = 2(10)Quiz of this Question
1 min read
C | Operators | Question 23
Predict the output of following program? C/C++ Code #include int main() { int x = 10; int y = 20; x += (y += 10); printf("%d %d", x, y); return 0; } (A) 40 20 (B) 40 30 (C) 30 30 (D) 30 40 Answer: (B) Explanation: The main statement in question is "x += y += 10". Since there are two += operators in the statement, associativity comes into the pictur
1 min read
C | Operators | Question 24
Output of following C code will be? C/C++ Code #include int main() { int x = 10; int y = (x++, x++, x++); printf("%d %d\n", x, y); return 0; } (A) 13 12 (B) 13 13 (C) 10 10 (D) Compiler Dependent Answer: (A) Explanation: Option A is correct. Quiz of this Question Please comment below if you find anything wrong in the above post
1 min read
C | Operators | Question 26
Output of following C code will be? C/C++ Code #include int main() { int y = 0; int x = (y != 0); printf("%d", x); return 0; } (A) 0 (B) 1 (C) A bog negative Number (D) Compiler Error Answer: (A) Explanation: 0 is the output of the following C code. Quiz of this Question Please comment below if you find anything wrong in the above post
1 min read
C | Operators | Question 27
Output of following C code will be? C/C++ Code #include int main() { int a = 0; int b; a = (a == (a == 1)); printf("%d", a); return 0; } (A) 0 (B) 1 (C) Big negative number (D) -1 Answer: (B) Explanation: We need to figure out value of "(a == (a == 1))" (a == 1) returns false as a is initialized as 0. So in outer bracket, false is compared with a.
1 min read
C | Operators | Question 27
Output of following C code will be? C/C++ Code #include #include int top = 0; char fun1() { char a[] = {'a', 'b', 'c', '(', 'd'}; return a[top++]; } int main() { char b[10]; char ch2; int i = 0; while ((ch2 = fun1()) != '(') { b[i++] = ch2; } b[i] = '\0'; // Add null-terminating character to mark the end of the string printf("%s", b); return 0; } (
1 min read
Increment and Decrement Operators in C
The increment ( ++ ) and decrement ( -- ) operators in C are unary operators for incrementing and decrementing the numeric values by 1 respectively. The incrementation and decrementation are one of the most frequently used operations in programming for looping, array traversal, pointer arithmetic, and many more. In this article, we will discuss the
4 min read
What are the Operators that Can be and Cannot be Overloaded in C++?
There are various ways to overload Operators in C++ by implementing any of the following types of functions: 1) Member Function 2) Non-Member Function 3) Friend Function List of operators that can be overloaded are: + - * ? % ? & | ~ ! = < > += -= *= ?= %= ?= &= |= << >> <<= >>= == != <= >= && ||
4 min read
Increment (Decrement) operators require L-value Expression
What will be the output of the following program? #include<stdio.h> int main() { int i = 10; printf("%d", ++(-i)); return 0; } A) 11 B) 10 C) -9 D) None Answer: D, None - Compilation Error. Explanation: In C/C++ the pre-increment (decrement) and the post-increment (decrement) operators require an L-value expression as operand. Provi
1 min read
Order of operands for logical operators
The order of operands of logical operators &&, || are important in C/C++. In mathematics, logical AND, OR, etc... operations are commutative. The result will not change even if we swap RHS and LHS of the operator. In C/C++ (may be in other languages as well)  even though these operators are commutative, their order is critical. For example
1 min read
three90RightbarBannerImg