Arlindi Frakulla
Arlindi Frakulla

Reputation: 19

How to pad integer printing with leading zeros?

So i have to make kinda like a timer and this has to be a string and it has to have a 0 before if it is smaller than ten so like 00,01,02...09,10,11 so all numbers are 2 digits. But I've ran into a problem and i cant seem to solve it. And I need to have it kinda like so no ctime or not to turn it at all into a string.

I've tried a couple of times also with the decompiler tools but I see that the main problem is in position 4 and 7. I think that there may be something wrong with the code blocks compiler, but i am not so sure.

#include <stdio.h>

int main(){
    char tostring[9]={};
    int timer=6588;
    int hours=0;
    int minutes=0;
    int seconds=0;

    hours=timer/3600;
    minutes=(timer%3600)/60;
    seconds=(timer%3600)%60;
    //printf("%i:%i:%i",hours,minutes,seconds);

    if(hours<10){
        tostring[0]=(char)0;
        tostring[1]=(char)hours;
    }else{
        tostring[0]=(char)(hours/10);
        tostring[1]=(char)(hours%10);
    }
    tostring[2]=':';
    if(minutes<10){
        tostring[3]=(char)0;
        tostring[4]=(char)minutes;
    }else{
        tostring[3]=(char)(minutes/10);
        tostring[4]=(char)(minutes%10);
    }
    tostring[5]=':';
    if(seconds<10){
        tostring[6]=(char)0;
        tostring[7]=(char)seconds;
    }else{
        tostring[6]=(char)(seconds/10);
        tostring[7]=(char)(seconds%10);
    }
    tostring[8]='\0';
    printf("%s",tostring);
    return 0;
}

i expect the output to be 01:49:48, but nothing comes out, just blank. What would be a solution to solving this problem.

Upvotes: 0

Views: 2281

Answers (2)

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

Instead of this, why don't you print integers with fixed width and padded with 0?

Something like

 printf("%02i:%02i:%02i",hours,minutes,seconds);

should do the job.

Explanation for the format specification %02i:

  • % start of conversion specifier
  • 0 leading 0s will be used for padding (if the actual length of the value is less than the minimum field width)
  • 2 value of the minimum field width.
  • i integer

Edited code:

#include <stdio.h>

int main(void){

    int timer=6588;
    int hours=0;
    int minutes=0;
    int seconds=0;

    hours=timer/3600;
    minutes=(timer%3600)/60;
    seconds=(timer%3600)%60;

    printf("%02i:%02i:%02i",hours,minutes,seconds);

    return 0;
}

Upvotes: 4

William Pursell
William Pursell

Reputation: 212208

tostring[0]=(char)0; does not write the character representation of 0 into tostring[0]. It writes a zero. You want tostring[0] = '0', with single quotes. And similarly, to write the character representation of a single digit, you can write tostring[1] = '0' + hours (If hours is 5, then '0' + 5 is the character value used to represent 5 in the local character set. Thankfully, it was standardized long ago that those representations should be sequential so that sort of thing works.) But, unless the point of this exercise is to avoid using printf, you should really just use printf.

Upvotes: 2

Related Questions