Skip to contentSkip to conversation

scanf and stdin having multiple ints

DISCUSSION Bryan Simmons 2 participants 3 messages Nov 18, 2002 – Nov 19, 2002

lore
Share & tools
lore
#0 lore raw

Supose I write a program in nasm that does this:

Enter a set of integers separated by spaces:
22 31 456 87

Suppose that in the module I am calling scanf to repeatedly parse the ints from stdin to place in an array, each index of which is 32 bits in size to hold a 32 bit unsigned int.

I've been told that using the format string "%n" does nothing in scanf. I'm going to try "%i" next, but I don't know if it'll work...

By the way, eax should be 0 when scanf stops getting data from stdin right? And incrementing my pointer to the array should be like "add esi, 4" right? Documentation is scarce for this type of thing. All I've been told is that fgets is the right function to use, but scanf is faster because it does parsing and conversion in one call...

Signature · 15 lines
Regards,

Bryan Simmons

=-=-=-=-==-=-=-=-==-=-=-=-==-=-=-=-=-=
 "More hay Trigger?", "No thanks Roy,
  I'm stuffed."
=-=-=-=-==-=-=-=-==-=-=-=-==-=-=-=-=-=
http://www.sarcastic-bastard.com

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#1 lore raw

I didn't see any other responses to this, so let me say what I can. I can't help with anything specific to nasm, but as to scanf ... did you consult its man page?

A quick read of it confirms that %n is, at best, unpredictable in how it will be interpreted (or perhaps "at worst", since I think "unpredictable" is scarier than "does nothing"). %i reads an "optionally signed" integer, and %u an unsigned integer. To be sure you are reading a 32-bit long, you might want to use %l [L, not one].

Quoted 9 lines · Bryan Simmons

Supose I write a program in nasm that does this:

Enter a set of integers separated by spaces:
22 31 456 87

Suppose that in the module I am calling scanf to repeatedly parse the ints from stdin to place in an array, each index of which is 32 bits in size to hold a 32 bit unsigned int.

Quoted 8 lines

I've been told that using the format string "%n" does nothing in scanf. I'm going to try "%i" next, but I don't know if it'll work...

By the way, eax should be 0 when scanf stops getting data from stdin right? And incrementing my pointer to the array should be like "add esi, 4" right? Documentation is scarce for this type of thing. All I've been told is that fgets is the right function to use, but scanf is faster because it does parsing and conversion in one call...

--
-------------------------------------------"Never tell me the odds!"--------
Ray Olszewski -- Han Solo
Palo Alto, California, USA [email protected]
-------------------------------------------------------------------------------

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#2 lore raw

Thanks, it turns out that %n doesn't do what I need and that %i works just fine.

Quoted 42 lines · Ray Olszewski

I didn't see any other responses to this, so let me say what I can. I can't help with anything specific to nasm, but as to scanf ... did you consult its man page?

A quick read of it confirms that %n is, at best, unpredictable in how it will be interpreted (or perhaps "at worst", since I think "unpredictable" is scarier than "does nothing"). %i reads an "optionally signed" integer, and %u an unsigned integer. To be sure you are reading a 32-bit long, you might want to use %l [L, not one].

Quoted 9 lines · Bryan Simmons

Supose I write a program in nasm that does this:

Enter a set of integers separated by spaces:
22 31 456 87

Suppose that in the module I am calling scanf to repeatedly parse the ints from stdin to place in an array, each index of which is 32 bits in size to hold a 32 bit unsigned int.

Quoted 8 lines

I've been told that using the format string "%n" does nothing in scanf. I'm going to try "%i" next, but I don't know if it'll work...

By the way, eax should be 0 when scanf stops getting data from stdin right? And incrementing my pointer to the array should be like "add esi, 4" right? Documentation is scarce for this type of thing. All I've been told is that fgets is the right function to use, but scanf is faster because it does parsing and conversion in one call...

--
-------------------------------------------"Never tell me the odds!"--------
Ray Olszewski -- Han Solo
Palo Alto, California, USA [email protected]
-------------------------------------------------------------------------------

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Signature · 15 lines
Regards,

Bryan Simmons

=-=-=-=-==-=-=-=-==-=-=-=-==-=-=-=-=-=
 "More hay Trigger?", "No thanks Roy,
  I'm stuffed."
=-=-=-=-==-=-=-=-==-=-=-=-==-=-=-=-=-=
http://www.sarcastic-bastard.com

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs