Reading input (stdin)

Read what you type in the IDE's Input tab with scanf or fgets.

Runs in Zerethon IDE · C (GCC 14) Official C documentation ↗

A run cannot ask questions while it runs: type the input in the Input tab before pressing Run.

main.c
#include <stdio.h>
#include <string.h>

int main(void) {
    char name[64];
    int a, b;
    if (!fgets(name, sizeof name, stdin)) return 1;
    name[strcspn(name, "\n")] = '\0';   /* drop the newline fgets keeps */
    if (scanf("%d %d", &a, &b) != 2) return 1;
    printf("Hello, %s! %d + %d = %d\n", name, a, b, a + b);
    return 0;
}

Input (stdin)

Ada
3 4

Output

Hello, Ada! 3 + 4 = 7
main.c
#include <stdio.h>

int main(void) {
    int value, total = 0;
    while (scanf("%d", &value) == 1) total += value;
    printf("total: %d\n", total);
    return 0;
}

Input (stdin)

5
10
15

Output

total: 30
scanf returns how many values it read — loop until it stops returning 1.

Watch out: Always check what scanf returns: with missing input the variables keep garbage values.

Related topics

Official references