Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > scanf %u

Reply
Thread Tools

scanf %u

 
 
viza
Guest
Posts: n/a
 
      07-05-2003

unsigned int number;
scan=sscanf(" -76","%u",&number);
printf("number has the value %d as %%d and %u as %%u.\n",number,number);

under gcc/3.2.2* this is returning 1, and assigning a value to number.
Isn't this wrong?

My reading of the man pages is that a '-' is not allowed. Doesn't this mean
it should be impossible to make the conversion, scanf should return 0 and
number should be left alone - just like if another non allowed character
were there, (eg: a letter)?

What have I missed?

* gcc (GCC) 3.2.2 (Mandrake Linux 9.1 3.2.2-3mdk)

 
Reply With Quote
 
 
 
 
Mathew Hendry
Guest
Posts: n/a
 
      07-05-2003
On Sat, 05 Jul 2003 13:32:39 +0100, viza <(E-Mail Removed)> wrote:

>
> unsigned int number;
> scan=sscanf(" -76","%u",&number);
>
>under gcc/3.2.2* this is returning 1, and assigning a value to number.
>Isn't this wrong?


Nope, the C99 standard has this to say about the %u specifier (7.19.6.2 #12)

| 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.

Notice "optionally signed". Then we need to look at the definition of
strtoul (7.20.1.4 #5) to see how it treats negative numbers

| If the subject sequence begins with a minus sign, the value resulting from
| the conversion is negated (in the return type).

strtoul's return type is unsigned long, so in this case -(unsigned long)76
would be returned.

There appears to be a weakness in the specification at this point though, as
it is not specified *scanf converts that unsigned long value to unsigned
int. The natural way would be "as if by assignment", giving
(unsigned int)-(unsigned long)76, but this is not specified anywhere AFAICT.

> printf("number has the value %d as %%d and %u as %%u.\n",number,number);


This line has undefined behaviour, because the first conversion specifier
(%d) does not match the type of ''number''.

-- Mat.

 
Reply With Quote
 
 
 
 
viza
Guest
Posts: n/a
 
      07-07-2003
and then viza said:
> ...


Thanks Mathew. My pooey server doesn't have your post, but I got it
eventualy through google.


 
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
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
scanf and iostreams paidojoao-groups@yahoo.com C++ 5 08-04-2004 02:14 PM
string.scanf? JustSomeGuy C++ 5 06-06-2004 04:16 PM
Re: safe scanf( ) or gets Snubis C++ 0 01-02-2004 01:53 PM



Advertisments