C Time Complexity

Intermediate
6 min read
Updated: Nov 2026
1 example

How Work Grows with n

Time complexity describes how the number of basic operations grows with input size n. A single loop is linear, nested loops over the same data are quadratic, and a loop that halves its range each step is logarithmic. You compare algorithms by these growth rates, not by raw seconds.

Remember
for (i = 0; i < n; i++)          /* O(n)      */
for (i...) for (j...)             /* O(n^2)    */
for (k = n; k > 1; k /= 2)        /* O(log n)  */

Cheat Sheet

Keep these facts handy while you work with Time Complexity in C.

TopicDetail
O(1)Constant
O(log n)Halving
O(n)One pass
O(n^2)Nested passes

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Space Complexity.

Example 1 — Count steps for three loop shapes

Try n = 8 and n = 16.

C
#include <stdio.h>

int main(void) {
    int sizes[] = {8, 16};
    for (int s = 0; s < 2; s++) {
        int n = sizes[s];
        int linear = 0, quadratic = 0, logarithmic = 0;

        for (int i = 0; i < n; i++) linear++;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) quadratic++;
        for (int k = n; k > 1; k /= 2) logarithmic++;

        printf("n=%d: linear=%d quadratic=%d log=%d\n", n, linear, quadratic, logarithmic);
    }
    return 0;
}

How It Works

  1. Doubling n doubles the linear count but quadruples the quadratic count.
  2. The halving loop only gains one step when n doubles.
  3. This is why O(log n) scales so well.

Edge Cases & Pitfalls

Check these before relying on Time Complexity in real programs.

hidden

Hidden loops

A library call like strlen inside a loop adds another factor of n.

drop

Dropping the dominant term

Keep the fastest-growing term: n^2 + n is O(n^2).

Key Takeaways

  • Count operations.
  • Nested loops multiply.
  • Halving gives log n.

One line: Time complexity describes how the number of basic operations grows with input size n.

Frequently Asked Questions

A measure of how the number of operations an algorithm performs grows as the input size grows.
Two loops over n items each are O(n^2); three are O(n^3).
Continue with "C Space Complexity" next. Compare constant extra space with stack-depth space in recursion. The C Algorithm Basics hub lists every topic in order.

Next: Space Complexity

Compare constant extra space with stack-depth space in recursion.

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.

10 people found this page helpful