Operators in C FAQ — 20 Interview Questions
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Operators in C — Frequently Asked Questions (FAQ)

📑 On this page:
  • Operator Basics
  • Prefix vs Postfix
  • Logical & Relational Operators
  • Arithmetic & Shift Operators
  • Comma, Ternary & sizeof
  • Frequently Asked Questions
Operators FAQ — frequently asked questions about operators in C language
Operators FAQ in C — interview questions with clear answers.
📚 On this page, you will revise:
  • Why operators are the building blocks of expressions
  • Prefix (++a) vs postfix (a++) in depth
  • How &&, ||, and ! work — and what they return
  • The difference between < and <= in expressions
  • Arithmetic operator types and unary vs binary
  • Shift operators and how to combine them
  • The comma operator as separator vs operator
  • Ternary operator vs if-else
  • What sizeof() returns and how it evaluates

This page answers 20 of the most-common operator questions from C interviews and exams. It starts with the basics — why operators exist — then moves into the subtle stuff: what prefix versus postfix really does, what logical operators return, how the comma operator works in two different roles, and why sizeof is an operator and not a function. Each answer is in plain English with small examples so you can use it in interviews or in code right away.

Operator Basics

Q1. Why do we use operators in C programming language?

Answer: Operators are the symbols that perform operations on values — without them you cannot express any computation.

Operators act on variables or values to perform operations. C groups them into several categories:

  • Arithmetic: +, -, *, /, %
  • Relational: <, >, <=, >=, ==, !=
  • Logical: &&, ||, !
  • Bitwise: &, |, ^, ~, <<, >>
  • Assignment: =, +=, -=, *=, etc.
  • Special: sizeof, ?:, comma, pointer operators (&, *)

Without operators, a C program could declare variables and call functions but couldn't compute anything — no math, no comparisons, no logic. They're one of the language's most-used features, and learning them fluently is essential to writing readable C.

Q2. What is the difference between an operator and an operand?

Answer: An operator is the symbol; operands are the values it acts on.

In the expression a + b, the + is the operator (the action) and a and b are operands (the values). Some operators are unary (one operand: -a, !x, sizeof(x)), some are binary (two operands: a + b, a && b), and one is ternary (three operands: a ? b : c). Understanding this distinction helps you reason about precedence and read complex expressions correctly.

Prefix vs Postfix

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

Answer: Prefix modifies the variable first, then returns the new value. Postfix returns the current value first, then modifies.

The four forms are ++a (prefix increment), --a (prefix decrement), a++ (postfix increment), and a-- (postfix decrement). The difference is what value the expression produces:

int a = 5;

int b = ++a;   /* prefix: a becomes 6 first, then b = 6 */
/* Result: a = 6, b = 6 */

int c = a++;   /* postfix: b gets current value 6, then a becomes 7 */
/* Result: a = 7, c = 6 */

A common exam trap: int x = 5; printf("%d", x++); prints 5 (uses the value, then increments). printf("%d", ++x); would print 6 (increments, then uses). The same logic applies to -- for decrement.

Q4. When should I use prefix vs postfix increment?

Answer: When the return value doesn't matter, use either — they're identical in effect. When it does matter, choose based on what value you want.

In a standalone statement like i++; or ++i;, both do the same thing — increment i by 1. The difference only matters when the result is used inside a larger expression.

A subtle point: in C++, prefix is generally preferred for non-trivial types because it avoids creating a temporary copy. In C, this doesn't matter (all types are trivially copyable), but many C programmers still prefer ++i in for-loop counters as a habit. In C, however, i++ reads more naturally in loops and is the more common style — many style guides actually prefer postfix when the result is unused.

Logical & Relational Operators

Q5. Why do we use logical operators in C?

Answer: Logical operators combine multiple conditions or negate a condition — essential for making decisions in if, while, and for statements.

Real programs rarely rely on a single condition. You often need to check things like "the user is logged in AND the cart is not empty" or "the score is above 90 OR the bonus is applied". That's what logical operators do:

  • && (AND) — true only if both conditions are true
  • || (OR) — true if at least one condition is true
  • ! (NOT) — flips the truth value of a single condition

Without these, you'd have to nest many if statements to express the same logic — verbose and harder to read. They're also short-circuit: a && b doesn't evaluate b if a is false, which matters when b has side effects.

Q6. What are the types of logical operators in C?

Answer: Three: logical AND (&&), logical OR (||), and logical NOT (!).

Each has a clear rule:

  • && (AND): returns 1 (true) if both operands are non-zero (true); returns 0 otherwise.
  • || (OR): returns 1 if either operand is non-zero; returns 0 only if both are zero.
  • ! (NOT): returns 0 if its operand is non-zero; returns 1 if its operand is zero.

All three return exactly 0 or 1 — never any other number. Both && and || short-circuit (they stop evaluating once the result is known), which is why if (p != NULL && *p > 0) is safe: the dereference is skipped if p is NULL.

Q7. What is the difference between < and <= in C?

Answer: < is strictly less than (false when equal); <= is less than or equal to (true when equal).

These are two of the six relational operators (along with >, >=, ==, !=). The difference is how they treat the "equal" case:

15 <  16   /* true */
15 <  15   /* false */
15 <= 15   /* true */
15 <= 16   /* true */
15 <  14   /* false */
15 <= 14   /* false */

The same pattern applies to > and >=: 17 > 17 is false, but 17 >= 17 is true. These may look obvious, but a wrong comparison operator is one of the most common bugs in loops and conditionals — for example, using < instead of <= when the last element should be included.

Q8. What does a relational operator return in C?

Answer: Exactly 1 (true) or 0 (false) — the type is int.

Every relational operator (<, >, <=, >=, ==, !=) returns an int with value 1 or 0. This is stricter than many beginners expect — C does not return a distinct "true" object. Any non-zero value in a condition is treated as true, but relational operators always produce exactly 1 or 0.

This matters when you use comparisons in arithmetic: int count = (a > b) + (c > d); gives a count of how many comparisons succeeded — because each returns 0 or 1.

Arithmetic & Shift Operators

Q9. What are the types of arithmetic operators in C?

Answer: Binary (+, -, *, /, %) and unary (+, -, ++, --).

Binary arithmetic operators take two operands:

  • + — addition
  • - — subtraction
  • * — multiplication
  • / — division (integer division if both operands are int)
  • % — modulus (remainder, only for integers in C)

Unary arithmetic operators take one operand: -a (negation), +a (no-op, sometimes used for clarity), ++a, --a (increment/decrement). Integer division and modulus are the two most-tested arithmetic concepts in C exams — 12 / 5 = 2, and 12 % 5 = 2.

Q10. Can we use combinations of shift operators (left and right) in our code?

Answer: Yes — you can freely mix << and >> in the same expression, subject to precedence and parenthesization.

Shift operators are commonly used to extract bits from an integer. For example, to isolate certain bit positions from an int:

unsigned int x = 0xABCD;   /* 16-bit value */
unsigned int high_byte = (x >> 8) & 0xFF;   /* 0xAB */
unsigned int low_byte  = x & 0xFF;          /* 0xCD */

You can combine << and >> in the same expression — just be careful with precedence. x >> 4 << 2 means (x >> 4) << 2 because both have the same precedence and are left-associative. In practice, parenthesize for clarity.

Q11. What does the bitwise NOT operator (~) do, and can it flip an entire sequence of bits?

Answer: ~ flips every bit (0 → 1, 1 → 0). Yes, it inverts the entire bit pattern of its operand.

The bitwise NOT operator is unary — it takes one operand and flips every bit. For an 8-bit value:

     0000 0101   /* 5 */
~    1111 1010   /* -6 in two's complement */

The shortcut rule: ~x == -(x + 1) in two's complement. So ~0 = -1, ~5 = -6, ~(-1) = 0. This operator is useful for clearing bits (x & ~mask) and is heavily used in systems programming.

Comma, Ternary & sizeof

Q12. Why do we use the comma operator (,) in C?

Answer: Two different roles: as a separator (in declarations and function arguments) and as an operator (evaluates left to right, returns the last value).

The comma is overloaded in C — it has two completely different behaviours depending on context. As a separator, it just divides items in a list. As an operator, it evaluates two expressions in sequence and returns the second. This question tests whether you can distinguish the two roles.

Q13. Why do we use the comma (,) as a separator?

Answer: To declare multiple variables in one statement or pass multiple arguments to a function.

In declarations and function calls, the comma separates items:

int p, q, r;   /* declares three separate int variables */
printf("%d, %d", a, b);   /* passes two arguments */

Note that this is not the comma operator — this is a comma used as punctuation in the grammar. The compiler treats int p, q, r; as three declarations: int p;, int q;, int r;.

Q14. Why do we use the comma (,) as an operator?

Answer: To evaluate multiple expressions in a single statement, where only the last expression's value matters.

The comma operator evaluates its left side, discards the result, then evaluates its right side and returns that value. It's most useful in for loops:

for (int i = 0, j = 10; i < j; i++, j--) {
    /* runs with i=0,j=10 then i=1,j=9 then ... */
}

Here i++, j-- uses the comma operator to do two increments in one expression. The most-asked exam trap: int p = (10, 20, 30); — because of the comma operator, p is 30, not 10. But int p = 10, 20, 30; is a syntax error (the compiler sees "int p = 10" then can't parse ", 20").

Q15. Why is the ternary operator (?:) used in C?

Answer: As a compact alternative to if-else when choosing between two values.

The ternary operator has the form condition ? value_if_true : value_if_false and returns one of two values based on the condition. It's useful when the if-else would only assign a value:

int max = (a > b) ? a : b;   /* instead of if (a > b) max = a; else max = b; */

The ternary form is one line, has no side effects, and can be used in a larger expression. Use it for simple value choices; use if-else for anything with multi-statement branches. Overusing ternary can hurt readability.

Q16. Is the ternary operator different from a conditional statement?

Answer: No — the ternary operator is a kind of conditional construct, but it's an expression (returns a value), while if-else is a statement (doesn't).

Both ternary and if-else make decisions, but they're not interchangeable:

  • if-else is a statement — it controls execution flow but doesn't return a value. You can't write int x = (if (c) 1 else 2);
  • ternary ?: is an expression — it produces a value. int x = c ? 1 : 2; is valid. int x = if (c) 1 else 2; is not.

In practice, use if-else for multi-statement branches and ternary for simple value choices. Optionally, both can be nested, but deeply nested ternaries are notoriously hard to read.

Q17. What is the advantage of the ternary operator over if-else?

Answer: It's more compact and can be used inside a larger expression — a common pattern when assigning a value based on a condition.

The main advantage is brevity and the ability to use it where a value is expected:

/* With ternary (one line) */
int max = (a > b) ? a : b;

/* With if-else (three lines) */
int max;
if (a > b) {
    max = a;
} else {
    max = b;
}

Another advantage: the ternary can appear inside function arguments (printf("%s", ok ? "yes" : "no");), while if-else can't. That said, "shorter" isn't always "better." If the branches have multiple statements or side effects, use if-else — the code will be clearer.

Q18. Why do we use the sizeof() operator in C?

Answer: To determine the byte size of a type or variable at compile time — essential for allocation, array length, and portability.

sizeof takes one operand (a type in parentheses, or a variable) and returns its size in bytes. Common uses:

sizeof(int)            /* 4 on most modern systems */
sizeof(char)           /* always 1 */
sizeof(arr)            /* total bytes of the array */
sizeof(arr)/sizeof(arr[0])   /* number of elements */
malloc(n * sizeof(int))      /* allocate n ints */

sizeof is evaluated at compile time (unless the operand is a VLA), so it has no runtime cost. Its return type is size_t (an unsigned type from <stddef.h>). Because sizes vary by platform, using sizeof instead of hardcoded byte counts is what makes C code portable.

Q19. Is sizeof a function or an operator in C?

Answer: An operator — despite looking like a function call, sizeof is built into the language and evaluated by the compiler.

sizeof has function-like syntax (sizeof(x)), but it's actually an operator. Proof: you can write sizeof x without parentheses for a variable, which no function would allow. It's also a compile-time constant in most cases, which a function call can't be.

This distinction matters in interview questions — if someone asks "what does sizeof return," the answer is size_t, and if they ask "is sizeof a keyword," the answer is yes (it's one of C's 32 classic keywords). The function-like syntax is just a readability convention.

Q20. Does the double equals operator (==) work with a single = sign?

Answer: No — == (comparison) and = (assignment) are two different operators and cannot be interchanged.

This is one of the most common beginner bugs. The two operators do completely different things:

if (a = 5) { ... }   /* assigns 5 to a; expression is 5 (true), always runs */
if (a == 5) { ... }  /* compares a to 5; runs only if a is 5 */

The first one is a silent bug — the assignment succeeds and returns 5, which the if treats as true. Modern compilers issue a warning (-Wparentheses in GCC) for this pattern, and style guides recommend "Yoda conditions" (if (5 == a)) to catch the mistake — because if (5 = a) is a hard compile error, which forces you to fix it.

Frequently Asked Questions

What is operator precedence, and why does it matter?

Operator precedence is the order in which operators are evaluated when multiple appear in an expression. a + b * c evaluates as a + (b * c) because * has higher precedence than +. If you want different behaviour, use parentheses. Precedence matters because getting it wrong is a silent bug — the code compiles, runs, and just produces the wrong answer. In C, precedence rules are formally defined but often counterintuitive, so the safest policy is "when in doubt, add parentheses."

What is short-circuit evaluation in logical operators?

Short-circuit means the compiler stops evaluating as soon as the answer is known. For a && b, if a is false, b is not evaluated — the result is false regardless of b. For a || b, if a is true, b is not evaluated. This is why if (p != NULL && *p == 0) is safe: the dereference is skipped when p is NULL. Short-circuit is one of the most-tested C concepts in interviews because getting it wrong leads to crashes.

What's the difference between = and ==?

= is the assignment operator (stores a value into a variable). == is the equality operator (compares two values and returns 1 or 0). They are not interchangeable. Writing if (a = 5) assigns 5 to a and the if always runs; writing if (a == 5) only runs when a is 5. Modern compilers warn about this, but it's still one of the most common bugs in real code.

What does the modulus operator % return for negative numbers in C?

For negative operands, the result of % follows the rule "a == (a / b) * b + (a % b)". In practice, the sign of the result in C matches the sign of the dividend (the left operand) — so -7 % 3 gives -1, and 7 % -3 gives 1. This is different from Python, where the result takes the sign of the divisor. It's an interview favourite because so many programmers are surprised by C's behaviour here.

Can I use the ternary operator instead of if-else everywhere?

No. The ternary operator is an expression that returns a value, while if-else is a statement. You can only use ternary where a value is expected — typically when assigning or passing an argument. For multi-statement branches or complex logic, if-else is clearer. And nesting ternaries more than one level deep is a code smell that most style guides (including the Linux kernel guide) discourage.

💡 Tip: Operators are one of the most-tested topics in C interviews because they look simple but hide subtle behaviours — prefix vs postfix, short-circuiting, comma-as-operator, sizeof-as-operator, and =/== confusion. If you can explain all five fluently with a small code example, you're ready for almost any operator question.

📖 Related FAQ & MCQ Pages

  • Introduction to C FAQ
  • Conditional Statements FAQ
  • Loops FAQ
  • Arrays FAQ
  • Function FAQ
  • Structure FAQ
  • Pointers FAQ
  • Files FAQ
  • Storage Classes FAQ
  • Dynamic Memory FAQ

Previous Topic: -->> Introduction to C FAQ   ||   Next topic: -->> Conditional Statements FAQ


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • Top 40 MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.