Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

printf of a for loop is skipping only 0

I have hit a roadblock in one of my tasks.

This is the yet to be finished code of my conversion table.

int main(int argc, char *argv[]) {

    int i = 0; //celsius = 0, fahrenheit = 0;

    printf("Celsius\tFahrenheit \n");

    for (i = -20 ; i<=300 ; i= (i+20)) //iteration from -20 to 300 °C by +20
    {
        printf("%7.d\t%10d \n",i,i);
    }


    return 0;
}

The Output looks like this, which is exactly as expected by the teacher, minus the missing 0 (and soon to be implemented conversion)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Celsius Fahrenheit
    -20        -20
                 0
     20         20
     40         40
     60         60
     80         80
    100        100
    120        120
    140        140
    160        160
    180        180
    200        200
    220        220
    240        240
    260        260
    280        280
    300        300

Could anyone please tell me why the 0 keeps getting skipped and how to prevent that from happening in the future?

>Solution :

The problem is the conversion specifier %7.d: for an integer conversion, the precision field that follows the . is the minimum number of digits to produce (padding with leading zeroes). If you do not specify a ., the default value is 1 but with a . the number is 0 if no digits follow the .. The effect of this 0 precision is the value 0 is converted to an empty string. Remove the . to fix this problem.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading