Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How come a printf is performed after a true condition was tested?

Tags:

c

I'm a beginner in C ,so please forgive me if this question is stupid or was asking weirdly.

I'm reading C primer plus and one of the examples in Chapter-8 is some loop that testing whether the user entered - a newline character or not ,which I couldn't understand.

The code is short so I will show it to you:

int main(void)
{
    int ch; /* character to be printed */
    int rows, cols; /* number of rows and columns */
    printf("Enter a character and two integers:\n");
    while ((ch = getchar()) != '\n')
    {
        if (scanf("%d %d",&rows, &cols) != 2)
            break;
        display(ch, rows, cols);
        while (getchar() != '\n')
            continue;
        printf("Enter another character and two integers;\n");
        printf("Enter a newline to quit.\n");
    }
    printf("Bye.\n");
    return 0;
}
void display(char cr, int lines, int width) // the function to preform the printing of the arguments being passed 

What i dont understand is right here:

while (getchar() != '\n')
                continue;
            printf("Enter another character and two integers;\n");
            printf("Enter a newline to quit.\n");

First of all, the while (getchar() != '\n') is testing the first ch was entered right? Second, if that is true, how come the continue is not skiping the printf statements and going to the first while? isn't it what it should do?

Tnx

like image 829
MNY Avatar asked Jan 31 '13 13:01

MNY


People also ask

Can printf be used in if condition?

The expression within if( expression ) is always evaluated, and in your case that's a call to printf . The value of this expression is used to determine if the body (blank in your case) of the if is run.

How does the printf function work?

The printf function (the name comes from “print formatted”) prints a string on the screen using a “format string” that includes the instructions to mix several strings and produce the final string to be printed on the screen.

What is true about printf () function?

The printf() function sends a formatted string to the standard output (the display). This string can display formatted variables and special control characters, such as new lines ('\n'), backspaces ('\b') and tabspaces ('\t'); these are listed in Table 2.1.

How does printf work in assembly?

Printf in Assembly To call printf from assembly language, you just pass the format string in rdi as usual for the first argument, pass any format specifier arguments in the next argument register rsi, then rdx, etc.


2 Answers

Because there are no braces after the while statement, only the very next line is included in the loop. So, continue continues the while loop until a new line character is found and then the execution continues to the printf statements.

It is equivalent to this:

 while (getchar() != '\n')
 {
    continue;
 }
like image 63
SShaheen Avatar answered Oct 26 '22 21:10

SShaheen


The continue is applied to the while just before the two printf-s so when you enter \n you will get out of the innermost while back to the line

printf("Enter another character and two integers;\n");
like image 37
Eregrith Avatar answered Oct 26 '22 21:10

Eregrith