Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Fixed number of digits printing of integers (ex: 0001 to 0100) (http://www.velocityreviews.com/forums/t440472-fixed-number-of-digits-printing-of-integers-ex-0001-to-0100-a.html)

Michel Rouzic 12-09-2005 04:20 PM

Fixed number of digits printing of integers (ex: 0001 to 0100)
 
I can't think of a simple way to print integers with a fix number of
digits, in order to obtain something like 0001 for 1 and 0100 for 100
automatically just by specifying in a variable the number of desired
digits.

I thought about printing to a variable using for example %4d and then
replace ' ' by '0', but I don't know if i can print to an array, and
then I don't think I can put a variable between % and d to specify the
number of digits I want.

I thought that maybe there would be some already existing and easy way
to do it that I don't know of, otherwise i'd like to know how people
usually deal with that problem, thanks


jacob navia 12-09-2005 04:30 PM

Re: Fixed number of digits printing of integers (ex: 0001 to 0100)
 
Michel Rouzic a écrit :
> I can't think of a simple way to print integers with a fix number of
> digits, in order to obtain something like 0001 for 1 and 0100 for 100
> automatically just by specifying in a variable the number of desired
> digits.
>
> I thought about printing to a variable using for example %4d and then
> replace ' ' by '0', but I don't know if i can print to an array, and
> then I don't think I can put a variable between % and d to specify the
> number of digits I want.
>
> I thought that maybe there would be some already existing and easy way
> to do it that I don't know of, otherwise i'd like to know how people
> usually deal with that problem, thanks
>


#include <stdio.h>
int main(void)
{
for (int i=4; i<10;i++)
printf("%05d\n",i*100);
int width=6;
printf("%0*d\n",width,746);
}

Output
00400
00500
00600
00700
00800
00900
000746

Mike Wahler 12-09-2005 04:31 PM

Re: Fixed number of digits printing of integers (ex: 0001 to 0100)
 

"Michel Rouzic" <Michel0528@yahoo.fr> wrote in message
news:1134145254.553730.298940@g44g2000cwa.googlegr oups.com...
>I can't think of a simple way to print integers with a fix number of
> digits, in order to obtain something like 0001 for 1 and 0100 for 100
> automatically just by specifying in a variable the number of desired
> digits.
>
> I thought about printing to a variable using for example %4d and then
> replace ' ' by '0', but I don't know if i can print to an array, and
> then I don't think I can put a variable between % and d to specify the
> number of digits I want.
>
> I thought that maybe there would be some already existing and easy way
> to do it that I don't know of, otherwise i'd like to know how people
> usually deal with that problem, thanks


#include <stdio.h>

int main()
{
int i = 0;

for(i = 1; i < 101; ++i)
printf("%04d\n", i);

return 0;
}

Where's your textbook?

-Mike



David Resnick 12-09-2005 04:33 PM

Re: Fixed number of digits printing of integers (ex: 0001 to 0100)
 

Michel Rouzic wrote:
> I can't think of a simple way to print integers with a fix number of
> digits, in order to obtain something like 0001 for 1 and 0100 for 100
> automatically just by specifying in a variable the number of desired
> digits.
>
> I thought about printing to a variable using for example %4d and then
> replace ' ' by '0', but I don't know if i can print to an array, and
> then I don't think I can put a variable between % and d to specify the
> number of digits I want.
>
> I thought that maybe there would be some already existing and easy way
> to do it that I don't know of, otherwise i'd like to know how people
> usually deal with that problem, thanks


Try "%04d"

-David


Ben Pfaff 12-09-2005 04:33 PM

Re: Fixed number of digits printing of integers (ex: 0001 to 0100)
 
"Michel Rouzic" <Michel0528@yahoo.fr> writes:

> I can't think of a simple way to print integers with a fix number of
> digits, in order to obtain something like 0001 for 1 and 0100 for 100
> automatically just by specifying in a variable the number of desired
> digits.
>
> I thought about printing to a variable using for example %4d and then
> replace ' ' by '0', but I don't know if i can print to an array, and
> then I don't think I can put a variable between % and d to specify the
> number of digits I want.
>
> I thought that maybe there would be some already existing and easy way
> to do it that I don't know of, otherwise i'd like to know how people
> usually deal with that problem, thanks


int num_digits = 4;
int value = ...whatever...;
printf("%0*d\n", num_digits, value);

You should really get a C reference manual.
--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield

Kleuskes & Moos 12-09-2005 04:41 PM

Re: Fixed number of digits printing of integers (ex: 0001 to 0100)
 
On Fri, 09 Dec 2005 08:20:54 -0800, Michel Rouzic wrote:

> I can't think of a simple way to print integers with a fix number of
> digits, in order to obtain something like 0001 for 1 and 0100 for 100
> automatically just by specifying in a variable the number of desired
> digits.


sprintf("%04d", some_int);


Michel Rouzic 12-09-2005 05:26 PM

Re: Fixed number of digits printing of integers (ex: 0001 to 0100)
 

Mike Wahler wrote:
> "Michel Rouzic" <Michel0528@yahoo.fr> wrote in message
> news:1134145254.553730.298940@g44g2000cwa.googlegr oups.com...
> >I can't think of a simple way to print integers with a fix number of
> > digits, in order to obtain something like 0001 for 1 and 0100 for 100
> > automatically just by specifying in a variable the number of desired
> > digits.
> >
> > I thought about printing to a variable using for example %4d and then
> > replace ' ' by '0', but I don't know if i can print to an array, and
> > then I don't think I can put a variable between % and d to specify the
> > number of digits I want.
> >
> > I thought that maybe there would be some already existing and easy way
> > to do it that I don't know of, otherwise i'd like to know how people
> > usually deal with that problem, thanks

>
> #include <stdio.h>
>
> int main()
> {
> int i = 0;
>
> for(i = 1; i < 101; ++i)
> printf("%04d\n", i);
>
> return 0;
> }
>
> Where's your textbook?


Thanks. Got no textbook. n869.pdf is all I got. Plus i'm learning C on
my own


Michel Rouzic 12-09-2005 05:27 PM

Re: Fixed number of digits printing of integers (ex: 0001 to 0100)
 

Ben Pfaff wrote:
> "Michel Rouzic" <Michel0528@yahoo.fr> writes:
>
> > I can't think of a simple way to print integers with a fix number of
> > digits, in order to obtain something like 0001 for 1 and 0100 for 100
> > automatically just by specifying in a variable the number of desired
> > digits.
> >
> > I thought about printing to a variable using for example %4d and then
> > replace ' ' by '0', but I don't know if i can print to an array, and
> > then I don't think I can put a variable between % and d to specify the
> > number of digits I want.
> >
> > I thought that maybe there would be some already existing and easy way
> > to do it that I don't know of, otherwise i'd like to know how people
> > usually deal with that problem, thanks

>
> int num_digits = 4;
> int value = ...whatever...;
> printf("%0*d\n", num_digits, value);
>
> You should really get a C reference manual.


OK thanks. All i got is n869.pdf, otherwise I have nothing else. What
else should I get?


Ben Pfaff 12-09-2005 05:36 PM

Re: Fixed number of digits printing of integers (ex: 0001 to 0100)
 
"Michel Rouzic" <Michel0528@yahoo.fr> writes:

> Ben Pfaff wrote:
>> int num_digits = 4;
>> int value = ...whatever...;
>> printf("%0*d\n", num_digits, value);
>>
>> You should really get a C reference manual.

>
> OK thanks. All i got is n869.pdf, otherwise I have nothing else. What
> else should I get?


n869.pdf should work fine as a C reference manual. You just need
to read it carefully.
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

Flash Gordon 12-09-2005 06:32 PM

Re: Fixed number of digits printing of integers (ex: 0001 to 0100)
 
Michel Rouzic wrote:
> Ben Pfaff wrote:
>> "Michel Rouzic" <Michel0528@yahoo.fr> writes:


<snip>

>> You should really get a C reference manual.

>
> OK thanks. All i got is n869.pdf, otherwise I have nothing else. What
> else should I get?


I would recommend reading the FAQ and getting a copy of K&R2.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


All times are GMT. The time now is 10:16 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.