C
Online C Compiler
Code compiles with GCC 16 as -std=gnu23, C23 with GNU extensions, and run settings switch to C89, C99, C11 or C17. scanf, fgets and getchar read what you type in the console while the program runs, and output shows as it’s printed, so a prompt doesn’t need fflush(stdout).
Try it aboveHow C runs here
- Versions
- C89, C99, C11, C17, C23 (default), GCC 16
- Build
gcc -O2 -std=gnu23 -o a.out *.c -lm- Run
./a.out- Limits per run
- 256 MB memory, 15 s of CPU time on 2 cores, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
- Good to know
- Every .c file is compiled together; the math library is linked.
A program that reads input
This reads a count and that many numbers, sorts them with qsort and prints the median:
#include <stdio.h> #include <stdlib.h> static int compare(const void *a, const void *b) { int x = *(const int *)a, y = *(const int *)b; return (x > y) - (x < y); } int main(void) { int n; printf("How many numbers? "); if (scanf("%d", &n) != 1 || n < 1 || n > 100) { puts("Please enter a whole number from 1 to 100."); return 1; } int a[100]; printf("Enter %d numbers: ", n); for (int i = 0; i < n; i++) { if (scanf("%d", &a[i]) != 1) { puts("That was not a number."); return 1; } } qsort(a, n, sizeof a[0], compare); printf("Sorted:"); for (int i = 0; i < n; i++) printf(" %d", a[i]); printf("\nMedian: %g\n", n % 2 ? a[n / 2] : (a[n / 2 - 1] + a[n / 2]) / 2.0); return 0; }
$ ./a.out How many numbers? 4 Enter 4 numbers: 12 5 9 1 Sorted: 1 5 9 12 Median: 7
scanf returns how many values it read, so comparing it with 1 catches a letter typed where a number belongs. Without the check, n keeps whatever garbage it held.
scanf, fgets and the leftover newline
scanf("%d") reads the number and leaves the Enter after it waiting in the input. A following fgets() reads just that newline and returns an empty string, so this program never stops for the name:
printf("Age: "); scanf("%d", &age); printf("Name: "); fgets(name, sizeof name, stdin);
$ ./a.out Age: 30 Name: [] is 30
Throw the rest of the line away before calling fgets():
#include <stdio.h> #include <string.h> int main(void) { int age; char name[50]; printf("Age: "); scanf("%d", &age); while (getchar() != '\n') {} printf("Name: "); fgets(name, sizeof name, stdin); name[strcspn(name, "\n")] = '\0'; printf("[%s] is %d\n", name, age); return 0; }
$ ./a.out Age: 30 Name: Ada Lovelace [Ada Lovelace] is 30
- scanf("%s", name) stops at the first space, so “Ada Lovelace” becomes “Ada”. Read names and sentences with fgets() and strip the newline with strcspn() as above.
- Reading every line with fgets() and converting it with sscanf() or strtol() avoids leftover input altogether.
- scanf("%d", &x) skips spaces and line breaks before a number, so numbers can be typed on one line or several.
- The console can’t send end of input (Ctrl+D), so while (scanf("%d", &x) == 1) keeps waiting after the last number. Read a count first, or end the input with a letter.
Several .c files
The build compiles every .c file in the playground together, so a function can live in its own file with a header that declares it:
// stats.h #pragma once double mean(const int *a, int n); // stats.c #include "stats.h" double mean(const int *a, int n) { long sum = 0; for (int i = 0; i < n; i++) sum += a[i]; return n > 0 ? (double)sum / n : 0.0; } // main.c #include <stdio.h> #include "stats.h" int main(void) { int a[] = {4, 8, 15, 16, 23, 42}; printf("Mean: %.2f\n", mean(a, 6)); return 0; }
$ ./a.out Mean: 18.00
- Because every .c file goes into one program, Run builds the same thing whichever tab is open.
- Two files that each define main() stop the link with multiple definition of `main'. To keep an old exercise around, rename it to .txt so the build skips it.
- Put only declarations in a .h file. A function defined in a header that two .c files include is defined twice, with the same multiple definition error.
What isn’t available
- conio.h, with getch() and clrscr(). It comes from Turbo C and Windows compilers: here #include <conio.h> stops with fatal error: conio.h: No such file or directory. Use getchar() for getch() and drop clrscr().
- windows.h and graphics.h. The compiler runs on Linux, so Sleep() is sleep() from <unistd.h>, and the Turbo C BGI graphics library doesn’t exist.
- scanf_s(), gets_s() and fopen_s(). These are Microsoft’s versions; GCC’s C library doesn’t have them. Use scanf() with a field width, fgets() and fopen().
- gets(). It was removed in C11, and GCC reports implicit declaration of function 'gets'; did you mean 'fgets'?
- system("pause") and system("cls"). They’re Windows commands, so the console shows sh: 1: pause: not found. Delete them; the output stays on screen anyway.
- Other libraries: sqrt() and pow() link because -lm is passed, but no other library is linked, so code that needs ncurses or another third-party library won’t build.
Errors you’re likely to see
- “error: expected ';' before 'return'”: the line before is missing its semicolon. GCC reports it at the next token, so look one line up.
- “error: 'count' undeclared (first use in this function)”: the variable is misspelled, or it was declared inside a block that has already ended.
- implicit declaration of function 'strlen', with a note to include '<string.h>': a header is missing. Since GCC 14 this is an error rather than a warning, so older code that compiled before can stop here.
- ** Process exited - Return Code: 139 **: a segmentation fault, usually scanf("%d", n) without the &, an index past the end of an array, or a NULL pointer. The console prints only the code, with no “Segmentation fault” text.
- ** Process exited - Return Code: 136 **: integer division or % by zero.
GCC’s messages start with main.c:7:5:, a link that jumps to that line. After a failed run, Fix with AI explains the error and proposes a change you review before it applies.
C23 by default, and when to switch
C23 makes bool, true, false and nullptr keywords, adds constexpr and typeof, and allows binary literals and digit separators:
#include <stdio.h> constexpr int LIMIT = 5; int main(void) { bool found = false; int *p = nullptr; typeof(LIMIT) n = 0b101; int big = 1'000'000; if (p == nullptr && n == LIMIT) found = true; printf("%d %d %s\n", n, big, found ? "yes" : "no"); return 0; }
$ ./a.out 5 1000000 yes
- Old code can break under C23: typedef int bool; is an error now that bool is a keyword, K&R definitions such as int add(a, b) int a, b; { ... } are no longer accepted, and int f(); now means a function that takes no arguments. Select C17 in run settings for such code.
- C89 rejects a declaration inside a for statement, as in for (int i = 0; ...); declare i before the loop or pick C99 or later.
- Every standard is the GNU flavor (gnu89 to gnu23), so GCC extensions such as statement expressions and __attribute__ keep working.
Questions
Does conio.h work?
No. conio.h belongs to Turbo C and Windows compilers, and GCC on Linux doesn’t have it. Replace getch() with getchar() and remove clrscr().
Which C compiler and standard is it?
GCC 16 with -std=gnu23, C23 plus GNU extensions. Run settings offer C89, C99, C11 and C17 as well.
Do I need fflush(stdout) before scanf?
Not here: output isn’t buffered, so a prompt shows the moment printf() runs. Keep fflush(stdout) if the same code must also work in other terminals.
How do I enter several numbers?
Separate them with spaces or Enter: scanf("%d") skips both before each number. You can also paste several lines at once.
Why did my program end with Return Code 139?
It crashed with a segmentation fault. Look for a scanf() argument missing its &, an array index past the end, or a pointer that is NULL or freed.
Is math.h linked?
Yes. The build passes -lm, so sqrt(), pow() and the rest of <math.h> work without extra flags.