简体   繁体   中英

Why does printf print the number of spaces I assigned to the int space?

#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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM