typestar

histogram.c in C

Print a text histogram from counts given as arguments.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    if (argc < 2) {
        fprintf(stderr, "usage: %s N [N ...]\n", argv[0]);
        return 1;
    }
    for (int i = 1; i < argc; i++) {
        int count = atoi(argv[i]);
        printf("%3d | ", count);
        for (int j = 0; j < count; j++)
            putchar('#');
        putchar('\n');
    }
    return 0;
}

How it works

  1. atoi parses each argument into a count.
  2. An inner loop prints that many # characters.
  3. A guard prints usage when no counts are given.

Keywords and builtins used here

The run, in numbers

Lines
17
Characters to type
312
Tokens
114
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 68 seconds.

Type this snippet

Step 2 of 3 in Encore, step 34 of 35 in Language basics.

← Previous Next →