3
#include <stdio.h>
#include <cs50.h>

int
main(void)
{

    int height;

    do {
        height = get_int("Size: \n");
    } while (height < 1 || height > 50);

    for (int row = 1; row < height + 1; row++) {
        for (int space = height - row; space > 0; space--) {
            printf(" ");
        }
    }

    return 0;
}

Why does my code work? I'm doing the pset1 of mario of cs50. I don´t understand why printf(" ") prints exactly the number of spaces that are in int space = height-row — how does printf know to print the number stored in int space if I didn´t specify it?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Victor B
  • 51
  • 2
  • 1
    `printf` does _not_ "know" how many spaces to print. Your `printf(" ");` will print _one_ space. But, it is called in a _loop_. So, the number of spaces is the number of loop iterations. – Craig Estey Nov 12 '20 at 02:16
  • 1
    You could tell `printf()` how many spaces to print with `printf("%*s", height - row, " ");` without needing the loop. It then knows how many spaces to print because you tell it. – Jonathan Leffler Nov 12 '20 at 04:23
  • (meta: when you accept one of the answers (by clicking on the big grey V mark next to it), the one *you* deem the most helpful (if any), you get a +2 rep bump) :) – Will Ness Dec 01 '20 at 00:27

2 Answers2

2

printf() doesn't know the value of space. In this case the for (int space = height - row; ... ) loop is just running the printf() statement height-row times, and each time it runs it exactly one space gets printed.

Mustafa Quraish
  • 692
  • 4
  • 10
1

This is prefaced by my top comment.

printf does not "know" how many spaces to print. Your printf(" "); will print one space. But, it is called in a loop. So, the number of spaces is the number of loop iterations.


Here's a refactored version of your program that shows various height values and the number of spaces they print. I've replaced " " with "#" so they can be seen.

Also, I believe you want printf("\n"); after the end of the inner loop. That allows it to generate a pyramid like output.

Anyway, here's the code:

#include <stdio.h>
//#include <cs50.h>

int
main(void)
{
    int height;

#if 0
    do {
        printf("Size:\n");
        scanf("%d",&height);
        //height = get_int("Size: \n");
    } while (height < 1 || height > 50);
#endif

    for (height = 0;  height <= 20;  ++height) {
        if (height > 0)
            printf("\n");
        printf("height: %d\n",height);

        int count = 0;
        for (int row = 1; row < height + 1; row++) {
            for (int space = height - row; space > 0; space--) {
#if 1
                printf("#");
#endif
                ++count;
            }
            printf("\n");
        }

        printf("count: %d\n",count);
    }

    return 0;
}

Here is the output of the program:

Note that the program always prints a blank line at the end. This is because you're going one iteration too far.

height: 0
count: 0

height: 1

count: 0

height: 2
#

count: 1

height: 3
##
#

count: 3

height: 4
###
##
#

count: 6

height: 5
####
###
##
#

count: 10

height: 6
#####
####
###
##
#

count: 15

height: 7
######
#####
####
###
##
#

count: 21

height: 8
#######
######
#####
####
###
##
#

count: 28

height: 9
########
#######
######
#####
####
###
##
#

count: 36

height: 10
#########
########
#######
######
#####
####
###
##
#

count: 45

height: 11
##########
#########
########
#######
######
#####
####
###
##
#

count: 55

height: 12
###########
##########
#########
########
#######
######
#####
####
###
##
#

count: 66

height: 13
############
###########
##########
#########
########
#######
######
#####
####
###
##
#

count: 78

height: 14
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#

count: 91

height: 15
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#

count: 105

height: 16
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#

count: 120

height: 17
################
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#

count: 136

height: 18
#################
################
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#

count: 153

height: 19
##################
#################
################
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#

count: 171

height: 20
###################
##################
#################
################
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#

count: 190
Craig Estey
  • 30,627
  • 4
  • 24
  • 48
  • Thank you so much it was really helpful! – Victor B Nov 13 '20 at 02:27
  • 1
    You're welcome! Somebody downvoted the answer even though [I thought] it was reasonable and not worthy of a downvote. Would you please upvote/accept the answer to compensate? See: https://stackoverflow.com/help/someone-answers Thanks. – Craig Estey Nov 13 '20 at 14:56