473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Formatted IO; %d or %i?

Is there any difference between `%d' and `%i' in formatted IO? Which one
is preferred?

Regards,
August
Nov 14 '05 #1
13 1342
In article <9g*******************@newsb.telia.net>
August Karlstrom <fu********@comhem.se> wrote:
Is there any difference between `%d' and `%i' in formatted IO? Which one
is preferred?


For the printf family of functions: there is no difference whatsoever.
(I use %d mostly out of habit, because K&R C did not have %i.)

For the scanf family of functions: the difference between %d and
%i is essentially the same as that between using strtol() with a
base of 10 (%d) or 0 (%i). That is, with %i, the number 0xa0 is
valid and has the value 160 decimal, and 031 is octal and has the
value 25 decimal. (This is, of course, why so many programmers
confuse Christmas and Halloween: DEC 25 equals OCT 31.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #2
"Chris Torek" <no****@torek.net> wrote in message
news:d7*********@news2.newsguy.com...
base of 10 (%d) or 0 (%i).


How does one count in base 0?
Nov 14 '05 #3
Mark wrote:
"Chris Torek" <no****@torek.net> wrote in message
news:d7*********@news2.newsguy.com...
base of 10 (%d) or 0 (%i).


How does one count in base 0?


Unfortunately, you snipped the relevant part; I'll quote it for
reference:
,- Chris Torek: ----
| For the scanf family of functions: the difference between %d
| and %i is essentially the same as that between using strtol()
| with a base of 10 (%d) or 0 (%i).
| [...]
`----
base 10/base 0 just refers to the base parameter of strtol():
#include <stdlib.h>
long strtol(const char *str, char **ptr, int base);
i.e.
sscanf(str, "%i", ....) <-> strtol(str, NULL, 0)
sscanf(str, "%d", ....) <-> strtol(str, NULL, 10)

Following the above, Chris also explained the difference; in
short, base=10 expects a base 10 number and will scan 0x57 as
0 and 057 as 57, whereas base=0 means "try to intelligently guess
whether the number is base 8, 10, or 16". So, 0x57 will be 57
hexadecimal and 057 will be 57 octal.
BTW: You cannot work sensibly with base 1 and not at all with base 0.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #4
>"Chris Torek" <no****@torek.net> wrote in message
news:d7*********@news2.newsguy.com...
base of 10 (%d) or 0 (%i).

In article <A1********************@newshog.newsread.com>
Mark <so***@localbar.com> wrote:How does one count in base 0?


Heh.

The quote above trims too much: I remember mentioning the strtol()
function specifically, so that the word "base" refers to its third
argument:

long strtol(const char *nptr, char **endptr, int base);

When the base argument is 0, strtol() uses the same "leading 0x means
hex, leading 0 means octal, otherwise the number is decimal" rule that
the C language itself uses.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #5
Michael Mair <Mi**********@invalid.invalid> wrote:

BTW: You cannot work sensibly with base 1


You can if all you need to do is count -- have you never used hash/tally
marks?

-Larry Jones

Oh, what the heck. I'll do it. -- Calvin
Nov 14 '05 #6
la************@ugs.com wrote:
Michael Mair <Mi**********@invalid.invalid> wrote:
BTW: You cannot work sensibly with base 1


You can if all you need to do is count -- have you never used hash/tally
marks?


Nope, so I do not know about that.

In base 1, every digit can take values from 0 to base-1=0 and digit
n (counting from 0) represents the multiples of 1^n=1.
So, we can only represent the value 0 which does not make much sense
in terms of counting. If you want to count occurrences of 0, you are
not performing base 1 arithmetics in my book, as you cannot count in
base 1.

Can you maybe provide a good starting point for information about hash
marks in base 1? Googling for >hash mark "base 1"< did not give me
satisfactory results (maybe I did not know what to look for).
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #7
On 2005-05-31 16:51:11 -0400, Michael Mair <Mi**********@invalid.invalid> said:
la************@ugs.com wrote:
Michael Mair <Mi**********@invalid.invalid> wrote:
BTW: You cannot work sensibly with base 1
You can if all you need to do is count -- have you never used hash/tally
marks?


Nope, so I do not know about that.

In base 1, every digit can take values from 0 to base-1=0 and digit
n (counting from 0) represents the multiples of 1^n=1.


Right, which means that there is only one value that a digit can take
(we can call it "0" or we can call it "1", whatever).
So, we can only represent the value 0 which does not make much sense
in terms of counting. If you want to count occurrences of 0, you are
not performing base 1 arithmetics in my book, as you cannot count in
base 1.


Sure you can:
0 -> ""
1 -> "1"
2 -> "11"
3 -> "111"
4 -> "1111"
5 -> "11111"
6 -> "111111"
7 -> "1111111"
8 -> "11111111"
9 -> "111111111"
10-> "1111111111"
--
Clark S. Cox, III
cl*******@gmail.com

Nov 14 '05 #8
Op Tue, 31 May 2005 17:20:05 -0400 schreef Clark S. Cox III:
On 2005-05-31 16:51:11 -0400, Michael Mair <Mi**********@invalid.invalid> said:
la************@ugs.com wrote:
Michael Mair <Mi**********@invalid.invalid> wrote:

BTW: You cannot work sensibly with base 1

You can if all you need to do is count -- have you never used hash/tally
marks?


Nope, so I do not know about that.

In base 1, every digit can take values from 0 to base-1=0 and digit
n (counting from 0) represents the multiples of 1^n=1.


Right, which means that there is only one value that a digit can take
(we can call it "0" or we can call it "1", whatever).
So, we can only represent the value 0 which does not make much sense
in terms of counting. If you want to count occurrences of 0, you are
not performing base 1 arithmetics in my book, as you cannot count in
base 1.


Sure you can:
0 -> ""
1 -> "1"
2 -> "11"
3 -> "111"
4 -> "1111"
5 -> "11111"
6 -> "111111"
7 -> "1111111"
8 -> "11111111"
9 -> "111111111"
10-> "1111111111"


No, if you call your digit '1', 1+1+1+1 still is 1.
So you can't count with base 1.
Read Michael's posting more carefully.

--
Coos
Nov 14 '05 #9
Coos Haak <ch*******************@hccnet.nl> writes:
No, if you call your digit '1', 1+1+1+1 still is 1.
So you can't count with base 1.


1+1+1+1 would be 1111 in the suggested meaning for base 1.
I don't know whether this is a widely accepted or understood
meaning for "base 1".
--
"Given that computing power increases exponentially with time,
algorithms with exponential or better O-notations
are actually linear with a large constant."
--Mike Lee
Nov 14 '05 #10
Ben Pfaff <bl*@cs.stanford.edu> writes:
Coos Haak <ch*******************@hccnet.nl> writes:
No, if you call your digit '1', 1+1+1+1 still is 1.
So you can't count with base 1.


1+1+1+1 would be 1111 in the suggested meaning for base 1.
I don't know whether this is a widely accepted or understood
meaning for "base 1".


The obvious extrapolation from bases greater than 1 is:

In base N, the allowed digits are 0 through N-1.
The digit in rightmost position denotes the digit value.
The digit in the next position denotes the digit value times N.
The next digit denotes the digit value times N*N.
And so forth.

These rules apply to binary, octal, decimal, hexadecimal, sexagesimal,
etc (ignoring things like C's "0x" prefix).

So unless you invent new special-case rules for base 1, the only
allowed digit is 0, and the only representable number is 0. (I think
Coos Haak was suggesting that the 0 digit could be represented as a
vertical line that bears an uncanny resemblance to the character '1'.
It's a bit of a stretch, but it's the only consistent interpretation
that allows 1111 to be a base-1 number.)

A tally system where 1111 represents 4 is perfectly sensible, but it's
not a based positional system, and calling it "base 1" is
inconsistent.

--
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.
Nov 14 '05 #11
Keith Thompson wrote:
These rules apply to binary, octal, decimal, hexadecimal, sexagesimal,
etc (ignoring things like C's "0x" prefix).

So unless you invent new special-case rules for base 1,


There's a little bit about postional number systems
and nonpositional number systems in Knuth,
The Art Of Computer Programming, Volume 2,
section 4.1 Positional Numbers Systems,
page 195.

Base one is a nonpositional number system,
unlike the ones you mentioned.
Each base one digit stands for one.

Whe you hold up three fingers to indicate the number three,
or when you write the Roman numeral for three, that's base one.

--
pete
Nov 14 '05 #12
Ben Pfaff <bl*@cs.stanford.edu> wrote:
Coos Haak <ch*******************@hccnet.nl> writes:
No, if you call your digit '1', 1+1+1+1 still is 1.
So you can't count with base 1.


1+1+1+1 would be 1111 in the suggested meaning for base 1.
I don't know whether this is a widely accepted or understood
meaning for "base 1".


It's incorrect, but widely (ab-)used that way, in particular in the
crappier kind of introduction to bases.

Richard
Nov 14 '05 #13
pete <pf*****@mindspring.com> wrote:
Keith Thompson wrote:
These rules apply to binary, octal, decimal, hexadecimal, sexagesimal,
etc (ignoring things like C's "0x" prefix).

So unless you invent new special-case rules for base 1,


There's a little bit about postional number systems
and nonpositional number systems in Knuth,
The Art Of Computer Programming, Volume 2,
section 4.1 Positional Numbers Systems,
page 195.

Base one is a nonpositional number system,
unlike the ones you mentioned.
Each base one digit stands for one.


No, _tallying_ is a nonpositional number system. Base one is often
abused as a name for tallying, but strictly speaking that's wrong.

Richard
Nov 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: James Turner | last post by:
I am trying to store formatted text (windows format) into a MySQL database and then retrieve it. The field in the database is a varchar. I cut and paste the test into a form field formatted, then...
5
by: Paul Lamonby | last post by:
Hi, I am generating a HTML formatted email in PHP, but I am getting the most bizarre error, it will randomly delete characters out of the HTML code while in transit to the address, therefore...
4
by: The Roys | last post by:
Hi I have a textbox which displays a formatted numeric. eg: 100,000 When I retreive this value for calculations, it is valued at 100 eg: calc = val(txtbox) * 2 calc will equal 200, not...
8
by: Matthew Thorley | last post by:
Greetings, perhaps someone can explain this. I get to different styles of formatting for xmla and xmlb when I do the following: from elementtree import ElementTree as et xmla =...
3
by: Niel | last post by:
Hello friends, Don't know if i am posting to the right group, but if anyone has an idea about this please let me know. I just wanted to ask that is it true that it is not possible to send HTML...
2
by: Steven T. Hatton | last post by:
I'm still not completely sure what's going on with C++ I/O regarding the extractors and inserters. The following document seems a bit inconsistent:...
3
by: Craig Petrie | last post by:
Hi, I have a large table in Word 2003 that has formatted text in the cells and wish to read and convert a cells formatted contents to html output via vb.net code. The formatting contains the...
2
by: JSheble | last post by:
I have a method in my class that needs to return formatted XML, with the carriage returns, linefeeds, and tabs... However, when I return oXml.OuterXml, the Xml is not formatted... Every example...
4
by: cybervigilante | last post by:
I sent HTML formatted email, using PHP, to my Yahoo address from my server, and it came out fine, styles and all. I sent it to my gmail address to test it and all I see is the raw html code. But I...
1
by: EricBlair | last post by:
Hello, Wondering if someone might help on this... I write out formatted text to a file then load it into a RichTextBox. However the text that is formatted into a table displays like this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.