Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Fixed number of digits printing of integers (ex: 0001 to 0100)

Reply
Thread Tools

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

 
 
Keith Thompson
Guest
Posts: n/a
 
      12-09-2005
"Michel Rouzic" <(E-Mail Removed)> writes:
> Ben Pfaff wrote:

[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?


A good tutorial is K&R2 (Kernighan & Ritchie_, _The C Programming
Language_, 2nd Edition). A good reference is H&S5 (Harbison & Steele,
_C: A Reference Manual_, 5th Edition).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
Michel Rouzic
Guest
Posts: n/a
 
      12-09-2005

Ben Pfaff wrote:
> "Michel Rouzic" <(E-Mail Removed)> 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.


yes it's good, I mostly search in it for descriptions of functions,
but for such a thing as %0*d, I have no idea how I could have found it
in it

 
Reply With Quote
 
 
 
 
Michel Rouzic
Guest
Posts: n/a
 
      12-09-2005

Flash Gordon wrote:
> Michel Rouzic wrote:
> > Ben Pfaff wrote:
> >> "Michel Rouzic" <(E-Mail Removed)> 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.


I see that the K&R2 is from 1988. Isn't it a bit outdated, since I tend
to stick to C99, as it was made even before the C89?

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      12-09-2005
"Michel Rouzic" <(E-Mail Removed)> writes:

> Ben Pfaff wrote:
>> "Michel Rouzic" <(E-Mail Removed)> 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.

>
> yes it's good, I mostly search in it for descriptions of functions,
> but for such a thing as %0*d, I have no idea how I could have found it
> in it


You could have read the description of the fprintf function.
--
"Welcome to the wonderful world of undefined behavior, where the demons
are nasal and the DeathStation users are nervous." --Daniel Fox
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-09-2005
"Michel Rouzic" <(E-Mail Removed)> writes:
> Flash Gordon wrote:
>> Michel Rouzic wrote:
>> > Ben Pfaff wrote:
>> >> "Michel Rouzic" <(E-Mail Removed)> 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.

>
> I see that the K&R2 is from 1988. Isn't it a bit outdated, since I tend
> to stick to C99, as it was made even before the C89?


K&R2 is based on a draft of what became the C90 standard.
You should also read the errata list at
<http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html>.

It doesn't cover C99, but then most compilers don't either. A number
of compilers support *some* of the C99 extensions, but very few
support all of them. It's up to you to decide whether a given C99
feature is useful enough that you can risk the possible
non-portability.

C99 is very nearly upward compatible with C90 (even more so with
well-written C90 that avoids things like implicit int) , so if you
stick to what K&R2 tells you, your code should work with any C90 or
C99 compiler.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Michel Rouzic
Guest
Posts: n/a
 
      12-10-2005

Ben Pfaff wrote:
> "Michel Rouzic" <(E-Mail Removed)> writes:
>
> > Ben Pfaff wrote:
> >> "Michel Rouzic" <(E-Mail Removed)> 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.

> >
> > yes it's good, I mostly search in it for descriptions of functions,
> > but for such a thing as %0*d, I have no idea how I could have found it
> > in it

>
> You could have read the description of the fprintf function.


well, I just looked the whole description of this function (which cover
several pages) and couldn't even find it, although I knew precisely
what I was looking for. You know when you're looking for such an info
it's not obvious to tell yourself "How am I gonna find out how to print
that? i know! i'm gonna read the 7 pages of the fprintf function, the
answer will surely be there" well of course you can read those pages
just by curiosity and find that out but when you're looking for
something precise you won't do that...

 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      12-10-2005
Michel Rouzic wrote:
> Ben Pfaff wrote:
>
>>"Michel Rouzic" <(E-Mail Removed)> writes:
>>
>>
>>>Ben Pfaff wrote:
>>>
>>>>"Michel Rouzic" <(E-Mail Removed)> 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.
>>>
>>>yes it's good, I mostly search in it for descriptions of functions,
>>>but for such a thing as %0*d, I have no idea how I could have found it
>>>in it

>>
>>You could have read the description of the fprintf function.

>
> well, I just looked the whole description of this function (which cover
> several pages) and couldn't even find it, although I knew precisely
> what I was looking for. You know when you're looking for such an info
> it's not obvious to tell yourself "How am I gonna find out how to print
> that? i know! i'm gonna read the 7 pages of the fprintf function, the
> answer will surely be there" well of course you can read those pages
> just by curiosity and find that out but when you're looking for
> something precise you won't do that...


Well, the text tells you about flags, precision, fieldwidth, length
modifiers, and conversion specifiers. The latter are mandatory.
So, once again: Is 0 a flag? Yes
Next one: Is * in this case a fieldwidth or a precision? You can answer
that one yourself. As there is no length modifier and the d conversion
specifier, you should be able to figure out what it does.
If you are not, you may be more happy with the C99 library reference
provided to the public by dinkumware.com (this is a resource
I appreciate very much) -- there is no shame in this; you have to
understand "Standardese" and know C and the capabilities of the
library to some extent to make full use of N869 or one of the other
public drafts.

If this is not enough, get a paper reference book. Maybe others can
give you the title of a book appropriate to your needs.

Another way is searching the clc archives. I/O, especially the
formatted variety, comes up around here very often.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Arctic Fidelity
Guest
Posts: n/a
 
      12-10-2005
On Fri, 09 Dec 2005 12:27:28 -0500, Michel Rouzic <(E-Mail Removed)>
wrote:

>
> Ben Pfaff wrote:
>
>> 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 for quick reference on the fly when I am looking for certain
functionality, I find O'Reilly's Pocket C Reference Guide to be very
useful.

- Arctic

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      12-10-2005

"Michel Rouzic" <(E-Mail Removed)> wrote
>>
>> You could have read the description of the fprintf function.

>
> well, I just looked the whole description of this function (which cover
> several pages) and couldn't even find it, although I knew precisely
> what I was looking for. You know when you're looking for such an info
> it's not obvious to tell yourself "How am I gonna find out how to print
> that? i know! i'm gonna read the 7 pages of the fprintf function, the
> answer will surely be there" well of course you can read those pages
> just by curiosity and find that out but when you're looking for
> something precise you won't do that...
>

Some computer documentation is appalling.
What the computer wants and needs is some sort of formal grammatical
description of the printf string (maybe a regular expression or a yacc
grammar).

Some people think that, therefore, it is also appropriate to provide the
user with such a description. Of course the computer and the human
understanding of the function are two different things. Humans can easily
extrapolate from examples of usage to the grammar, whilst computers find
this very difficult.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Using %x to format number to hex and number of digits Tim Chase Python 2 11-06-2010 01:22 AM
Re: Using %x to format number to hex and number of digits Chris Rebert Python 1 11-05-2010 07:05 PM
Printing significant digits of a floating point number Christoph Bartoschek C++ 6 04-21-2009 07:53 AM
number of digits in a number bejiz C++ 12 09-27-2007 09:21 PM
Datatype's to store integers as large as 100 digits psanand@gmail.com Java 2 01-20-2005 12:31 PM



Advertisments