473,756 Members | 3,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

Hi,

Today I got a really strange problem... I've made myself a data-file and
I read in data from that file....

When I read something like this line:

03 04 05, 00 04 01, 05 03 07, 08 03 00, 09 06 03 ... etc.

with something like scanf("%i %i %i, ", &var1, &var2, &var3);

then it stopped reading data starting with "08", "09" etc... But
everything such as "07" and below worked...

Then I tried scanf("%d %d %d, ", &var1, &var2, &var3);

And the program WORKED!?!!?

Why?

I looked in my C-book and it says absolutetly the same about using %i as
%d.... I'm too tired to cut down my progrm and post a small version of
it now, and it might be that's even unnecessary because somebody know
what's happening...

Compiler: Visual studio 2005, windows xp. I suspect perhaps it's a bug,
if my C book is right that there isn't any difference between %i and %d?
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Apr 26 '06
18 11852
Martin Jørgensen <un*********@sp am.jay.net> writes:
Ben Pfaff wrote:
Martin Jxrgensen <un*********@sp am.jay.net> writes:
When I read something like this line:

03 04 05, 00 04 01, 05 03 07, 08 03 00, 09 06 03 ... etc.

with something like scanf("%i %i %i, ", &var1, &var2, &var3);

then it stopped reading data starting with "08", "09" etc... But
everything such as "07" and below worked...

Then I tried scanf("%d %d %d, ", &var1, &var2, &var3); %d reads a decimal integer.
%i reads a C-like integer, so that a leading 0 means "octal".
Your reference manual should have told you this.


Now I double-checked my book: "C primer plus, 5th edition".

It simply *doesn't* tell this... What a piece of junk...
It says (table 4.6, p.117):

%d = interpret input as a signed decimal integer
. . .
%i = interpret input as a signed decimal integer
%o = interpret input as a signed octal integer
%x = interpret input as a signed hexadecimal integer
So the above is wrong?


Yes.

John Bode recommended Harbison&Steele , _C: A Reference Manual_, 5th
Edition; so do I, it's excellent. You *might* consider the standard
itself (the most recent draft is n1124.pdf); it's definitely not for
beginners, but with some effort it can answer these questions for you.
And what are %o and %x for if you can do exactly the same with %0i and
probably %x-something? ? ?


scanf() reads integer literals with the same syntax used by strtoul();
the use of %d vs %i vs %o vs %x changes the base.

For "%d", the input is assumed to be decimal. A "0x" or "0X" prefix
is not allowed, and a leading 0 doesn't make the number octal.
(Actually, if you have a leading "0x" or "0X", it just reads the "0",
and the "x" or "X" and everything following it is left in the input
stream.)

For "%i", the input looks like an integer literal in a C program (with
an optional sign). A leading "0" makes it octal, and a leading "0x"
or "0X" makes it hexadecimal. "123" yields 123, "0123" yields 83, and
"0x123" yields 291. Use this only if you expect the input to use a
leading "0", "0x", or "0X" to indicate the base. If you're getting
interactive input from a user, this probably isn't what you want
(unless you expect the user to be familiar with C syntax).

For "%o", the input is assumed to be octal, even if it doesn't have a
leading "0". For example, either "123" or "0123" yields the integer
value 83. A leading "0x" or "0X" is not allowed (sort of; see above).

For "%x", the input is assumed to be hexadecimal. A leading "0x" or
"0X" is allowed, but ignored. For example, either "123", "0x123", or
"0123" yields the integer value 291.

--
Keith Thompson (The_Other_Keit h) 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.
Apr 27 '06 #11
Keith Thompson wrote:
scanf() reads integer literals with the same syntax used by strtoul();
the use of %d vs %i vs %o vs %x changes the base.
[...]
For "%o", the input is assumed to be octal, even if it doesn't have a
leading "0". For example, either "123" or "0123" yields the integer
value 83. A leading "0x" or "0X" is not allowed (sort of; see above).

For "%x", the input is assumed to be hexadecimal. A leading "0x" or
"0X" is allowed, but ignored. For example, either "123", "0x123", or
"0123" yields the integer value 291.


I found this in N869:
o,u,x are stated to all match optionally signed integers
but expect arguments as pointers to unsigned integers.
This seems rather inconsistent.
A contrario, my man pages state that o,u,x match unsigned integers
and also expect arguments as unsigned integers.

Is it a "cut and paste" typo in N869 ?

From page 322, section 7.19.6.2 The fscanf function:

o Matches an optionally signed octal integer, whose
format is the same as expected for the subject
sequence of the strtoul function with the value 8
for the base argument. The corresponding argument
shall be a pointer to unsigned integer.

u Matches an optionally signed decimal integer, whose
format is the same as expected for the subject
sequence of the strtoul function with the value 10
for the base argument. The corresponding argument
shall be a pointer to unsigned integer.

x Matches an optionally signed hexadecimal integer,
whose format is the same as expected for the subject
sequence of the strtoul function with the value 16
for the base argument. The corresponding argument
shall be a pointer to unsigned integer.
Apr 27 '06 #12
regis <re************ **@free.fr> writes:
Keith Thompson wrote:
scanf() reads integer literals with the same syntax used by strtoul();
the use of %d vs %i vs %o vs %x changes the base.


[...]
For "%o", the input is assumed to be octal, even if it doesn't have a
leading "0". For example, either "123" or "0123" yields the integer
value 83. A leading "0x" or "0X" is not allowed (sort of; see above).
For "%x", the input is assumed to be hexadecimal. A leading "0x" or
"0X" is allowed, but ignored. For example, either "123", "0x123", or
"0123" yields the integer value 291.


I found this in N869:
o,u,x are stated to all match optionally signed integers
but expect arguments as pointers to unsigned integers.
This seems rather inconsistent.
A contrario, my man pages state that o,u,x match unsigned integers
and also expect arguments as unsigned integers.

[...]

It's not really inconsistent. The phrase "optionally signed" refers
to the optional '+' or '-' character preceding the integer literal in
the input. This needs to be specified because, in a C program, "-1"
isn't an integer literal; it's a unary "-" operator followed by a
literal "1". If there's a '-', the result is negated (which is well
defined for both signed and unsigned types).

For example, this:

#include <stdio.h>
int main(void)
{
char *s = "-1";
unsigned int x;
int count;

count = sscanf(s, "%o", &x);
printf("count = %d, x = %u\n", count, x);

return 0;
}

prints this:

count = 1, x = 4294967295

where 4294967295 is UINT_MAX on the system where I ran it.

Here's what N1124 says about 'u':

Matches an optionally signed decimal integer, whose format is the
same as expected for the subject sequence of the strtoul function
with the value 10 for the base argument. The corresponding
argument shall be a pointer to unsigned integer.

The wording is similar for 'o' and 'x'.

--
Keith Thompson (The_Other_Keit h) 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.
Apr 27 '06 #13
Keith Thompson wrote:
regis <re************ **@free.fr> writes:
I found this in N869:
o,u,x are stated to all match optionally signed integers
but expect arguments as pointers to unsigned integers.
This seems rather inconsistent.
A contrario, my man pages state that o,u,x match unsigned integers
and also expect arguments as unsigned integers.


It's not really inconsistent. The phrase "optionally signed" refers
to the optional '+' or '-' character preceding the integer literal in
the input. This needs to be specified because, in a C program, "-1"
isn't an integer literal; it's a unary "-" operator followed by a
literal "1". If there's a '-', the result is negated (which is well
defined for both signed and unsigned types).


Ok, now I understand.
Thank you for your explanations.

( I find both the wording of N869 and the wording of the man pages
misleading on this subject.

Also, as far as *text* representation of unsigned integers
are concerned, I wonder if accepting negatives representations
of unsigned integers as input at the sight of format "%u"
is a desirable feature. IMHO, since input functions are
the interface with the user, it have been more useful if
such negatives representations made the conversion fail, and only
canonical positive representations made the conversion succeed. )

--
regis
Apr 28 '06 #14
Keith Thompson wrote:
Martin Jørgensen <un*********@sp am.jay.net> writes:

-snip-
Now I double-checked my book: "C primer plus, 5th edition".

It simply *doesn't* tell this... What a piece of junk...
It says (table 4.6, p.117):

%d = interpret input as a signed decimal integer
. . .
%i = interpret input as a signed decimal integer
%o = interpret input as a signed octal integer
%x = interpret input as a signed hexadecimal integer
So the above is wrong?

Yes.

John Bode recommended Harbison&Steele , _C: A Reference Manual_, 5th
Edition; so do I, it's excellent. You *might* consider the standard
itself (the most recent draft is n1124.pdf); it's definitely not for
beginners, but with some effort it can answer these questions for you.

And what are %o and %x for if you can do exactly the same with %0i and
probably %x-something? ? ?

scanf() reads integer literals with the same syntax used by strtoul();
the use of %d vs %i vs %o vs %x changes the base.

For "%d", the input is assumed to be decimal. A "0x" or "0X" prefix
is not allowed, and a leading 0 doesn't make the number octal.
(Actually, if you have a leading "0x" or "0X", it just reads the "0",
and the "x" or "X" and everything following it is left in the input
stream.)

For "%i", the input looks like an integer literal in a C program (with
an optional sign). A leading "0" makes it octal, and a leading "0x"
or "0X" makes it hexadecimal. "123" yields 123, "0123" yields 83, and
"0x123" yields 291. Use this only if you expect the input to use a
leading "0", "0x", or "0X" to indicate the base. If you're getting
interactive input from a user, this probably isn't what you want
(unless you expect the user to be familiar with C syntax).

For "%o", the input is assumed to be octal, even if it doesn't have a
leading "0". For example, either "123" or "0123" yields the integer
value 83. A leading "0x" or "0X" is not allowed (sort of; see above).

For "%x", the input is assumed to be hexadecimal. A leading "0x" or
"0X" is allowed, but ignored. For example, either "123", "0x123", or
"0123" yields the integer value 291.


Thanks a lot.
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Apr 28 '06 #15
"regis" <re************ **@free.fr> wrote in message
news:44******** *************** @news.free.fr.. .
Keith Thompson wrote:
regis <re************ **@free.fr> writes:
I found this in N869:
o,u,x are stated to all match optionally signed integers
but expect arguments as pointers to unsigned integers.
This seems rather inconsistent.
A contrario, my man pages state that o,u,x match unsigned integers
and also expect arguments as unsigned integers.


It's not really inconsistent. The phrase "optionally signed" refers
to the optional '+' or '-' character preceding the integer literal in
the input. This needs to be specified because, in a C program, "-1"
isn't an integer literal; it's a unary "-" operator followed by a
literal "1". If there's a '-', the result is negated (which is well
defined for both signed and unsigned types).


Ok, now I understand.
Thank you for your explanations.

( I find both the wording of N869 and the wording of the man pages
misleading on this subject.

Also, as far as *text* representation of unsigned integers
are concerned, I wonder if accepting negatives representations
of unsigned integers as input at the sight of format "%u"
is a desirable feature. IMHO, since input functions are
the interface with the user, it have been more useful if
such negatives representations made the conversion fail, and only
canonical positive representations made the conversion succeed. )


Negating an unsigned integer is well defined. Some of us find it
useful. In recent years compilers have taken to issuing warnings
about this operation on the assumption that some programmers will
be surprised that the result of any negation is still non-negative.
(I like the ones that complain about -x yet quietly accept 0-x.
Go figure.) But that don't make it wrong.

I, for one, think -1 is a great shorthand for the largest
representable unsigned value. You don't get tired of writing 'f's,
and you don't have to know how many to write.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Apr 28 '06 #16
P.J. Plauger wrote:
"regis" wrote

Also, as far as *text* representation of unsigned integers
are concerned, I wonder if accepting negatives representations
of unsigned integers as input at the sight of format "%u"
is a desirable feature. IMHO, since input functions are
the interface with the user, it have been more useful if
such negatives representations made the conversion fail, and only
canonical positive representations made the conversion succeed. )


Negating an unsigned integer is well defined. Some of us find it
useful. In recent years compilers have taken to issuing warnings
about this operation on the assumption that some programmers will
be surprised that the result of any negation is still non-negative.
(I like the ones that complain about -x yet quietly accept 0-x.
Go figure.) But that don't make it wrong.

I, for one, think -1 is a great shorthand for the largest
representable unsigned value. You don't get tired of writing 'f's,
and you don't have to know how many to write.


Sure. On the programmer side, it is useful:

size_t k, n= strlen (text);
for (k= n-1; k != (size_t)-1; k--) { ... }

My remark was about *text* representations that the user deals with:

int i; scanf ("%i", & i);
The user types "1", he gets 1.
The user types "-1", he gets -1.

unsigned u; scanf ("%u", & u);
The user types "1", he gets 1.
The user types "-1", he gets something that might be different
on different machines...
Apr 28 '06 #17
regis <re************ **@free.fr> writes:
[...]
My remark was about *text* representations that the user deals with:

int i; scanf ("%i", & i);
The user types "1", he gets 1.
The user types "-1", he gets -1.

unsigned u; scanf ("%u", & u);
The user types "1", he gets 1.
The user types "-1", he gets something that might be different
on different machines...


One solution to this might be to add a syntax that specifies that a
leading sign is not allowed. For example, with scanf("%u", &u), if
the user enters "-1", UINT_MAX is stored in u. With scanf("%+u", &u),
"-1" would be invalid (though "+1" might be ok).

In the absence of such a language change, if you read the line into a
string to be processed with sscanf() (which is the recommended
practice anyway), you can check for a sign character before calling
sscanf().

--
Keith Thompson (The_Other_Keit h) 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.
Apr 28 '06 #18
regis <re************ **@free.fr> wrote:
P.J. Plauger wrote:
I, for one, think -1 is a great shorthand for the largest
representable unsigned value. You don't get tired of writing 'f's,
and you don't have to know how many to write.


Sure. On the programmer side, it is useful:

size_t k, n= strlen (text);
for (k= n-1; k != (size_t)-1; k--) { ... }

My remark was about *text* representations that the user deals with:

int i; scanf ("%i", & i);
The user types "1", he gets 1.
The user types "-1", he gets -1.

unsigned u; scanf ("%u", & u);
The user types "1", he gets 1.
The user types "-1", he gets something that might be different
on different machines...


Then again, it is dubitable whether using scanf() itself for user input
is a good idea.

Richard
May 2 '06 #19

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

Similar topics

4
13254
by: 321ConTact | last post by:
I have to find zipcodes within a given mileage range (25, 50, etc). I know it's possible to use the longitude/latitude to find this, and I have that for the zipcodes, but does anyone have a "ready made" formula? I know it has something to do with the sine/cosine, but the "signs" aren't clear enough to me (neither are the wonders). Thanks in advance for the wisdom. J
4
1646
by: Richard Shea | last post by:
Hi - I've writing a Python script which has a query which looks like this ... select * from T where C1 not in (1,2,3) .... C1 is a numeric column so elements of (1,2,3) must not be quoted like this ('1','2','3') and of course they must not be quoted like this ('1,2,3'). I'm using 'scanf' style substitution into the SQL, eg ...
13
3293
by: Jalal | last post by:
I am trying to use numeric_limits<double>::min() in an MFC application in Microsoft Visual C++.NET 2003. I am having some difficulties here. The following are the parts of a simple program I wrote and corresponding error massages. I am confused here. Similar codes work fine in Win32 Console Project. Can you please help me to resolve this? Thank you for your helps. Best Regards, Jalal
24
2765
by: jrefactors | last post by:
I have an upload file operation in the web application. UploadForm.jsp is the form, and UploadAction.jsp is the form processing. The web server is Websphere. //UploadForm.jsp <FORM NAME="InputForm" ACTION="UploadAction.jsp" METHOD="POST" enctype=multipart/form-data> <input type="file" name="fileName"> //etc ...
1
4178
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a couple of tables in my database using INNER JOINS and the WHERE clause to specify the required constraints. However, I also want to read two fields from a *single* record from a table called 'Locations' and then apply one of these field's values...
72
4222
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and c?
21
7856
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
1
8399
by: abrown07 | last post by:
#include<stdio.h> #include<math.h> int main(void) { double Yeild, Heads, Seedhead, Size;
1
1476
by: Kamalesh kumar | last post by:
this is the statement i encountered . . . fscanf(fp,"%",str1); where str1 is a str1 can someone please unconfuse me on whats the difference between using a %c and a % in reading strings ? thanks
3
13104
by: lingjun | last post by:
Hi, I am taking my first programing course in college... and I am completely lost on this assignment. I am not sure what is wrong with my current code. Any help will be appreciate it... thanks! I keep on getting the follow error messages when I try to compile it. test.c:3: error: syntax error before numeric constant test.c: In function `main': test.c:18: error: `next_day' undeclared (first use in this function) test.c:18: error:...
0
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10034
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9843
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.