What Is an Algorithm in C?

Intermediate
6 min read
Updated: Nov 2026
1 example

Algorithms in Plain Terms

An algorithm takes input, follows finite, unambiguous steps, and produces output. It must always terminate and each step must be doable. The same algorithm can be written in any language; the C code is just one implementation. Euclid's algorithm for the greatest common divisor is over two thousand years old and a perfect first example.

Remember
/* gcd(a, b):
   while b != 0:
       r = a mod b
       a = b
       b = r
   return a            */

Cheat Sheet

Keep these facts handy while you work with What Is an Algorithm in C? in C.

TopicDetail
InputZero or more values
OutputAt least one result
FiniteMust terminate
DefiniteEach step is unambiguous

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Writing Pseudocode for C Programs.

Example 1 — Euclid's GCD with a trace

Print every remainder step for 48 and 18.

C
#include <stdio.h>

int main(void) {
    int a = 48, b = 18;

    while (b != 0) {
        int r = a % b;
        printf("%d mod %d = %d\n", a, b, r);
        a = b;
        b = r;
    }
    printf("gcd = %d\n", a);
    return 0;
}

How It Works

  1. Each round replaces (a, b) with (b, a mod b).
  2. The remainder shrinks every round, so the loop must end.
  3. When the remainder is 0, the previous divisor, 6, is the gcd.

Edge Cases & Pitfalls

Check these before relying on What Is an Algorithm in C? in real programs.

loop

No termination argument

If nothing shrinks each iteration, the loop may never end.

edge

Ignoring edge cases

What if b starts at 0, or the inputs are negative? Decide and document.

Key Takeaways

  • Input, output, finite, definite.
  • Always prove it ends.
  • Language independent.

One line: An algorithm takes input, follows finite, unambiguous steps, and produces output.

Frequently Asked Questions

Input, output, finiteness, definiteness, and effectiveness: every step is clear and executable.
Not necessarily. A program that loops forever does not satisfy finiteness.
Continue with "Writing Pseudocode for C Programs" next. Describe the steps in plain structured text before coding. The C Algorithm Basics hub lists every topic in order.

Next: Writing Pseudocode for C Programs

Describe the steps in plain structured text before coding.

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.

8 people found this page helpful