Skip to main content

Online C Compiler

main.c
Ready

About C

C is the language most Indian engineering students start with. It's small and close to the hardware, which makes it the base for operating systems, embedded devices and many other languages.

How to take input in C

Read with scanf and pass the address of each variable with &. Strings (char arrays) don't need the &. %s stops at the first space.

int n;        scanf("%d", &n);
float f;      scanf("%f", &f);
char c;       scanf(" %c", &c);    // the space skips leftover new lines
char s[100];  scanf("%99s", s);    // one word

C programs to try

Copy one into the editor above, put any input in the Input tab and press Run.

Swap two numbers

#include <stdio.h>

int main() {
    int a, b, t;
    scanf("%d %d", &a, &b);
    t = a; a = b; b = t;
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

Input: 5 9

Factorial with a loop

#include <stdio.h>

int main() {
    int n;
    long long f = 1;
    scanf("%d", &n);
    for (int i = 2; i <= n; i++) f *= i;
    printf("%lld\n", f);
    return 0;
}

Input: 10

Largest element of an array

#include <stdio.h>

int main() {
    int n, a[100];
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    int max = a[0];
    for (int i = 1; i < n; i++) if (a[i] > max) max = a[i];
    printf("%d\n", max);
    return 0;
}

Input: 5 ⏎ 42 7 19 3 25

Common C errors and how to fix them

Segmentation fault
The program touched memory it doesn't own: often a missing & in scanf, or an array index past its size.
warning: implicit declaration of function 'printf'
Add #include <stdio.h> at the top of the file.
error: expected ';' before ...
A statement is missing its semicolon, usually on the line before the one named.
Wrong or garbage output
Check the format specifiers: %d for int, %lld for long long, %f for float, %lf to read a double with scanf.

Frequently asked questions

Is this online C compiler free?

Yes. Write and run C code in your browser for free, with nothing to install and no setup.

How do I give input to my C program?

Type it in the Input tab before pressing Run, and read it with scanf. Values can be separated by spaces or new lines.

Which header files can I use?

The standard C library headers, such as stdio.h, stdlib.h, string.h and math.h.

Does it work on my phone?

Yes. The editor, input and output work in a mobile browser, so you can practise C without a laptop.