Operators & Expressions
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Operators and Expressions in C: A Complete Guide

📑 On this page:
  • What are Operators and Expressions?
  • 1. Arithmetic Operators
  • 2. Relational Operators
  • 3. Logical Operators
  • 4. Assignment Operators
  • 5. Bitwise Operators
  • 6. Increment/Decrement Operators
  • 7. Special Operators
  • 8. Conditional Operator
  • Frequently Asked Questions
📚 In this tutorial, you will learn:
  • What are operators and expressions in C
  • What are arithmetic operators
  • What are relational and logical operators
  • What are assignment and bitwise operators
  • What are increment/decrement operators
  • What are special and conditional operators

What are Operators and Expressions?

Operators are special symbols that tell the compiler to perform specific mathematical or logical operations. Think of them like verbs in a sentence — they do the action.

An Expression is a combination of operators and operands (values or variables) that produces a result. For example, a + b is an expression where + is the operator and a, b are operands.

💡 Quick Definition: An operator is a symbol that performs an operation. An expression is a combination of operators and operands that yields a value.

C supports 8 types of operators:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators
  • Increment/Decrement Operators
  • Special Operators
  • Conditional Operator
Operators in C

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, and modulus.

Operator Operation Example Description
+Additiona + bAdds two numbers
-Subtractiona - bSubtracts two numbers
*Multiplicationa * bMultiplies two numbers
/Divisiona / bDivides two numbers
%Moduloa % bReturns remainder

2. Relational Operators

Relational operators compare two values and return true (1) or false (0). They're used in decision-making and loops.

Symbol Operation Example Description
<Less thana < bTrue if a is less than b
>Greater thana > bTrue if a is greater than b
==Equal toa == bTrue if a is equal to b
!=Not equala != bTrue if a is not equal to b
<=Less than or equala <= bTrue if a is less than or equal to b
>=Greater than or equala >= bTrue if a is greater than or equal to b

3. Logical Operators

Logical operators are used to combine two or more conditions. They're essential for complex decision-making.

Symbol Operation Example Description
&&Logical AND(x > 5) && (y < 5)True when both conditions are true
||Logical OR(x >= 10) || (y >= 10)True when at least one condition is true
!Logical NOT!(x >= 5)True when condition is false

4. Assignment Operators

Assignment operators are used to assign values to variables. The most common is =, but C also provides compound assignment operators.

Symbol Operation Example Equivalent
=Assignmenta = 5Assigns 5 to a
+=Addition assignmenta += 5a = a + 5
-=Subtraction assignmenta -= 5a = a - 5
*=Multiplication assignmenta *= 5a = a * 5
/=Division assignmenta /= 5a = a / 5
%=Modulo assignmenta %= 5a = a % 5

5. Bitwise Operators

Bitwise operators perform operations at the bit level. They're used for low-level programming and optimization.

Symbol Operation Example Description
&Bitwise ANDa & bAND operation on bits
|Bitwise ORa | bOR operation on bits
^Bitwise XORa ^ bXOR operation on bits
~Bitwise NOT~aComplement of bits
<<Left shifta << bShifts bits left
>>Right shifta >> bShifts bits right

6. Increment/Decrement Operators

Increment (++) and decrement (--) operators are unary operators that increase or decrease the value of a variable by 1.

Prefix vs Postfix:

++a (Prefix) — Increments first, then uses the value

a++ (Postfix) — Uses the value first, then increments

int a = 5;
a++; // a becomes 6 (post-increment)
++a; // a becomes 7 (pre-increment)
a--; // a becomes 6 (post-decrement)
--a; // a becomes 5 (pre-decrement)

7. Special Operators

C provides two special operators:

  • & (Address of Operator): Returns the memory address of a variable. Example: &a gives the address of variable a.
  • * (Pointer Operator): Used to declare pointers and dereference them. Example: *ptr accesses the value at the address stored in ptr.

8. Conditional Operator (?:)

The conditional operator (also called the ternary operator) is a shorthand for if-else. It takes three operands.

Syntax: condition ? expression1 : expression2;

If condition is true, expression1 is evaluated; otherwise, expression2 is evaluated.

int a = 10, b = 20;
int max = (a > b) ? a : b;
// max will be 20 because a > b is false
printf("Maximum value is: %d", max);

Frequently Asked Questions About Operators in C

1. What is the difference between = and == in C?

= is the assignment operator used to assign values. == is the equality operator used to compare values.

2. What is the difference between prefix and postfix increment?

Prefix (++a): Increments first, then returns the new value. Postfix (a++): Returns the original value first, then increments.

3. What is the ternary operator?

The ternary operator (?:) is a shorthand for if-else. It takes three operands: condition, true result, false result.

4. Why are bitwise operators useful?

Bitwise operators are useful for low-level programming, optimization, and tasks like setting/clearing specific bits in a number.

💡 Quick tip: Operators are the building blocks of C expressions. Mastering them is essential for writing efficient, readable code.

📖 Related Tutorials

  • Data Types in C
  • Constants in C
  • Assignment Statements
  • Decision Making in C

Previous Topic: -->> Data Types in C   ||   Next topic: -->> Constants in C


📚 Explore More Topics

🗄️ SQL Interview Questions & Answers

SQL SELECT Statement FAQ SQL Restricting & Sorting Data FAQ SQL Group Functions & Aggregated Data FAQ SQL Multiple Tables (JOINs) FAQ SQL Subqueries FAQ SQL DML Statements (Managing Tables) FAQ SQL Indexes, Synonyms & Sequences FAQ SQL DDL (Tables & Relationships) FAQ SQL Views FAQ SQL Indexing Best Practices FAQ SQL Window & Analytic Functions FAQ

🐍 Python Interview Questions & Answers

Python Interview Questions Python Syntax & Variables FAQ Python Data Types FAQ Python If-Else FAQ Python Loops FAQ Python Functions Interview Q Python String Manipulation FAQ Python Lists & Dictionaries FAQ Python Tuples & Sets FAQ Python Exception Handling FAQ Python OOP Interview Questions

☕ Java Interview Questions & Answers

Java Introduction Interview Q Java Development Environment FAQ Java Data Types FAQ Java Control Flow & Operators FAQ Java Basic Input/Output FAQ Java Arrays FAQ Java Strings FAQ Java Methods FAQ Java Basic OOP Concepts FAQ Java Advanced OOP Concepts FAQ Java OOP Best Practices FAQ Java Exception Handling FAQ Java Synchronization FAQ Java Threads & Concurrency FAQ Java Collection Framework FAQ Java File I/O & Serialization FAQ Java Serialization & Deserialization FAQ Java Features FAQ Java Inner & Anonymous Classes FAQ Java Memory Management FAQ Java Packages FAQ Java Wrapper Classes FAQ Java Streams & Lambda 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
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.