Big O Notation in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Describing Growth Rates

Big O gives an upper bound on growth: f(n) is O(g(n)) if, beyond some point, f(n) is at most a constant times g(n). In practice you keep only the dominant term and drop constants: 3n^2 + 5n + 2 is O(n^2). Common classes from fastest to slowest: O(1), O(log n), O(n), O(n log n), O(n^2), O(2^n).

Remember
3n^2 + 5n + 2   ->  O(n^2)
n + 100         ->  O(n)
5               ->  O(1)

Cheat Sheet

Keep these facts handy while you work with Big O Notation in C in C.

TopicDetail
O(1)Array index
O(log n)Binary search
O(n log n)Merge sort
O(n^2)Bubble sort
O(2^n)Subsets (brute force)

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Best, Average and Worst Case Analysis in C.

Example 1 — Watch the n^2 term dominate

f(n) = 3n^2 + 5n + 2 divided by n^2.

C
#include <stdio.h>

int main(void) {
    long long sizes[] = {10, 100, 1000};
    for (int i = 0; i < 3; i++) {
        long long n = sizes[i];
        long long f = 3 * n * n + 5 * n + 2;
        printf("n=%lld: f=%lld, f/n^2=%.2f\n", n, f, (double)f / (double)(n * n));
    }
    return 0;
}

How It Works

  1. The ratio settles near the constant 3 as n grows.
  2. Lower terms 5n and 2 become irrelevant.
  3. So f is O(n^2): constants and small terms are dropped.

Edge Cases & Pitfalls

Check these before relying on Big O Notation in C in real programs.

tight

Confusing O with exact

Big O is an upper bound. A linear algorithm is also technically O(n^2), but you should state the tightest bound.

const

Constants still matter in practice

Two O(n) algorithms can differ by 10x in real time.

Key Takeaways

  • Upper bound on growth.
  • Keep the dominant term.
  • Drop constants.

One line: Big O gives an upper bound on growth: f(n) is O(g(n)) if, beyond some point, f(n) is at most a constant times g(n).

Frequently Asked Questions

The running time grows at most proportionally to n for large n.
O(1) < O(log n) < O(n) < O(n log n) < O(n^2) < O(2^n).
Continue with "Best, Average and Worst Case Analysis in C" next. Measure linear search in its best, average, and worst cases. The C Algorithm Basics hub lists every topic in order.

Next: Best, Average and Worst Case Analysis in C

Measure linear search in its best, average, and worst cases.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful