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 18 12239
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
"Michel Rouzic" <Mi********@yahoo.fr> wrote in message
news:11**********************@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
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
"Michel Rouzic" <Mi********@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
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);
Mike Wahler wrote: "Michel Rouzic" <Mi********@yahoo.fr> wrote in message news:11**********************@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
Ben Pfaff wrote: "Michel Rouzic" <Mi********@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?
"Michel Rouzic" <Mi********@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
"Michel Rouzic" <Mi********@yahoo.fr> 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) ks***@mib.org <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.
Michel Rouzic wrote: Ben Pfaff wrote: "Michel Rouzic" <Mi********@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.
Ben Pfaff wrote: "Michel Rouzic" <Mi********@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.
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
Flash Gordon wrote: Michel Rouzic wrote: Ben Pfaff wrote: "Michel Rouzic" <Mi********@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.
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?
"Michel Rouzic" <Mi********@yahoo.fr> writes: Ben Pfaff wrote: "Michel Rouzic" <Mi********@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.
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
"Michel Rouzic" <Mi********@yahoo.fr> writes: Flash Gordon wrote: Michel Rouzic wrote: > Ben Pfaff wrote: >> "Michel Rouzic" <Mi********@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.
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) ks***@mib.org <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.
Ben Pfaff wrote: "Michel Rouzic" <Mi********@yahoo.fr> writes:
Ben Pfaff wrote: "Michel Rouzic" <Mi********@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.
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...
Michel Rouzic wrote: Ben Pfaff wrote:
"Michel Rouzic" <Mi********@yahoo.fr> writes:
Ben Pfaff wrote:
"Michel Rouzic" <Mi********@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.
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.
On Fri, 09 Dec 2005 12:27:28 -0500, Michel Rouzic <Mi********@yahoo.fr>
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/
"Michel Rouzic" <Mi********@yahoo.fr> 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Abe Simpson |
last post by:
Hello all,
The application I am working on must never output numbers in a
floating-point format, that is, something like
2e-002
is a big no-no. At the same time, it must output numbers in a...
|
by: Roger Leigh |
last post by:
Hello,
I'm writing a fixed-precision floating point class, based on the ideas
in the example fixed_pt class in the "Practical C++ Programming" book
by Steve Oualline (O' Reilly). This uses a...
|
by: Hako |
last post by:
I try this command:
>>> import string
>>> string.atoi('78',16)
120
this is 120 not 4E.
Someone can tell me how to convert a decimal number to hex number? Can
print A, B, C,DEF.
Thank you.
|
by: pout |
last post by:
What are the purposes of fixed-point? When should it be used?
I read:
#define Int2Fixed(x) (((long)(short)x) << 16)
and the fixed-point in 16.16 format. Does the 16 in the MACRO refer to...
|
by: guidosh |
last post by:
Hello,
I'm trying to write a printf statement that sets the field width when
printing a number. I'm using this:
printf("%*", fieldwidth, num_to_print);
However, I can't figure out how to...
|
by: JackM |
last post by:
I'm still working on validating the phone numbers that are entered on a
form but have come across a problem I don't understand how to fix. I can
handle most instances when it's in regular US...
|
by: rhitz1218 |
last post by:
Hi,
I'm trying to create a function that will sort a number's digits
from highest to lowest.
For example
1000 - will become 0001
or 1234 to 4321
|
by: waterdriven |
last post by:
Hello;
I am a newbie.
A homework assignment was assigned and I am having trouble getting started.
The assignment reads:
Write a program to print out the binary value of a 16 bit number.
|
by: barmatt80 |
last post by:
I have a field(RecordID) in my database that is in this format: 2008-0001-MED The first 4 digits is the year in 4 digits, the second 4 numbers are an autonumber or a incremential number increasing...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |