scanf %u

Discussion in 'C Programming' started by viza, Jul 5, 2003.

  1. viza

    viza Guest

    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)
    viza, Jul 5, 2003
    1. Advertisements

  2. On Sat, 05 Jul 2003 13:32:39 +0100, viza <> 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 ( #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 ( #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.
    Mathew Hendry, Jul 5, 2003
    1. Advertisements

  3. viza

    viza Guest

    and then viza said:
    > ...

    Thanks Mathew. My pooey server doesn't have your post, but I got it
    eventualy through google.
    viza, Jul 7, 2003
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Snubis

    Re: safe scanf( ) or gets

    Snubis, Jan 2, 2004, in forum: C++
    Jan 2, 2004
  2. JustSomeGuy


    JustSomeGuy, Jun 4, 2004, in forum: C++
    Jorge Rivera
    Jun 6, 2004
  3. Replies:
    Richard Herring
    Aug 4, 2004
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    those who know me have no need of my name
    Apr 3, 2006
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

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

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Richard Bos
    May 2, 2006

Share This Page