mardi 28 avril 2020

To understand %*d and if (( scanf( "%d", &n ) != 1 ) || ( n <= 0 )) in c

I have to print this pattern in c

4 4 4 4 4 4 4

4 3 3 3 3 3 4

4 3 2 2 2 3 4

4 3 2 1 2 3 4

4 3 2 2 2 3 4

4 3 3 3 3 3 4

4 4 4 4 4 4 4

And I found this code associated with it

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

int main()  
{ 
    while ( 1 ) 
    { 
        printf( "Enter a non-negative number (0 - exit): " ); 

        int n; 

        if ( ( scanf( "%d", &n ) != 1 ) || ( n <= 0 ) ) break; 

        if ( INT_MAX / 2 < n )  
        { 
            n = INT_MAX / 2; 
        } 

        int width = 1; 

        for ( int tmp = n; tmp /= 10; ) ++width; 

        putchar( '\n' ); 

        int m = 2 * n - 1; 

        for ( int i = 0; i < m; i++ ) 
        { 
            for ( int j = 0; j < m; j++ ) 
            {
                int value1 = abs( n - i - 1 ) + 1;
                int value2 = abs( n - j - 1 ) + 1;

                printf( "%*d ", width, value1 < value2 ? value2 : value1 );
            } 
            putchar( '\n' ); 
        } 

        putchar( '\n' ); 
    } 

    return 0; 
 }

I want to know why in this statementscanf( "%d", &n ) != 1 is used

if (( scanf( "%d", &n ) != 1 ) || ( n <= 0 ));

and also how single format specifier is accepting two values here

printf( "%*d ", width, value1 < value2 ? value2 : value1 );

Why % and * are used together"%*d"??

Aucun commentaire:

Enregistrer un commentaire