2 ways to print a half-pyramid I wrote this bit of C that prints a half-pyramid,

   *
  **
 ***
****


Here's the main body:

#include &lt;stdio.h&gt;
#define SIZE 4

void printrow(int threshold);

int main()
{
    int i, threshold;

    for (i = 0; i &lt; SIZE; i++) {
        // threshold slides to the left with each iteration:
        // ===|
        // ==|=
        // =|==
        // |===
        threshold = SIZE - (i+1);

        printrow(threshold);
    }

    return 0;
}


And here are two different implementations of printrow. I'm not sure which is the better of the two.

Version one uses the threshold as a slider that divides the row into two smaller loops:

void printrow(int threshold)
{
    int j;

    for (j = 0; j &lt; threshold; j++)
        printf(" ");

    for (j = threshold; j &lt; SIZE; j++)
        printf("*");

    printf("\n");
}


Version two runs a single loop and each iteration checks its position relative to the threshold when determining which character to print:

void printrow(int threshold)
{
    int j;
    char c;

    for (j = 0; j &lt; SIZE; j++) {
        c = (j &lt; threshold) ? ' ' : '*';
        printf("%c", c);
    }

    printf("\n");
}


This is a very small, simple program, but I'm curious which is the better approach, as I'm sure the same scenario can arise on a larger scale.
I like your version one better - in that you can break it up into even smaller pieces. Split out the printing of repeated characters:

void print_repeat(char c, int n) {
    while (n--) {
        printf("%c", c);
    }
}


So that printing a row just calls that twice:

void print_row(int row_num, int size) {
    print_repeat(' ', size - (row_num + 1));
    print_repeat('*', row_num + 1);
    printf("\n");
}


And printing the whole pyramid just calls that in a loop:

void print_pyramid(int size) {
    int i;
    for (i=0; i&lt;size; ++i) {
        print_row(i, size);
    }
}