473,387 Members | 1,535 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.

spaces in scanf format string

Hello everyone,

Trusting K&R2 i thought until recently that spaces are ignored in
scanf's format string. Reading arguments to the contrary confused me a
little. So i now ask:

Is scanf("%d%d",...) different from scanf("%d %d",...) in the
Standard's point of view?

Thank you.

Nov 5 '06 #1
3 12765
st***********@hotmail.com said:
Hello everyone,

Trusting K&R2 i thought until recently that spaces are ignored in
scanf's format string. Reading arguments to the contrary confused me a
little. So i now ask:

Is scanf("%d%d",...) different from scanf("%d %d",...) in the
Standard's point of view?
From 4.9.6.2 fscanf:

"Input white-space characters (as specified by the isspace function)
are skipped, unless the specification includes a [ , c , or n
specifier."

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 5 '06 #2
st***********@hotmail.com wrote:
Hello everyone,

Trusting K&R2 i thought until recently that spaces are ignored in
scanf's format string. Reading arguments to the contrary confused me a
little. So i now ask:

Is scanf("%d%d",...) different from scanf("%d %d",...) in the
Standard's point of view?
They differ, but so subtly that it's a "difference that
makes no difference."

Suppose the input is " 12\t34\n". The "%d%d" format
matches this input as follows: The first "%d" skips the leading
white spaces and consumes and converts the 12, then the second
"%d" skips the tab and consumes and converts the 34. The newline
is left unread.

The "%d %d" format operates just a little differently, but
has the same outcome. The first "%d" skips the leading spaces
and converts the 12, then the space in the format matches and
skips the tab, then the second "%d" skips nothing and converts
the 34. As before, the newline is left unread.

So the only difference lies in whether the tab is consumed
by the second "%d" or by the space in the format string, and
this difference isn't detectable "from the outside." scanf()
has no way to tell you how many white space characters a "%d"
did or didn't consume before converting a number.

However, this doesn't mean that white space in the format
string is "ignored." Most conversion specifiers automatically
consume and ignore leading white spaces in the input until they
find something non-white they can try to convert, but a few do
not: "%c" and "%[" start converting immediately, whether the
input is white or not, and "%n" doesn't read any input at all.
So if you want white space skipped before one of these, you
need to put some white space in the format string: " %c" or
" %[" or " %n", for instance.

The other situation where it becomes obvious that white
space in the format is not ignored is when it appears at the
end of the format string -- which is usually a mistake made
by someone who doesn't quite understand the scanf() family yet.
Consider our sample input of " 12\t34\n" and imagine reading
it with scanf("%d%d\n", &x, &y). The two "%d" specifiers will
convert their numbers as before, but what will the format's "\n"
do? Hints: all white space in the format is equivalent in the
sense that it matches any kind of white space in the input, and
a format's white space can match any amount of white input. Try
to predict what will happen, and then try it for yourself.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 5 '06 #3
Ο/Η Eric Sosman *γραψε:
The other situation where it becomes obvious that white
space in the format is not ignored is when it appears at the
end of the format string -- which is usually a mistake made
by someone who doesn't quite understand the scanf() family yet.
Consider our sample input of " 12\t34\n" and imagine reading
it with scanf("%d%d\n", &x, &y). The two "%d" specifiers will
convert their numbers as before, but what will the format's "\n"
do? Hints: all white space in the format is equivalent in the
sense that it matches any kind of white space in the input, and
a format's white space can match any amount of white input. Try
to predict what will happen, and then try it for yourself.
Thank you and Richard for your answers.

I assume the statement above will try to consume all white space
until it encounters some non whitespace. I had never thought of this
before, so thank you for pointing this out as well.

Nov 5 '06 #4

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

Similar topics

4
by: Marc Durufle | last post by:
I have a string like that " Vertices " and i want to obtain "Vertices" I would want to know if there is a simple way to put off spaces on a string, thank you -- Marc Durufle Inria...
3
by: Paul Watson | last post by:
It is clear that just using 'print' with variable names is relatively uncontrollable. However, I thought that using a format string would reign the problem in and give the desired output. Must...
6
by: Serv Lau | last post by:
suppose I want to use sscanf get the functionname from a function prototype. Is the following format string correct then? char funcname; char *p = "func(void)"; sscanf(p, "%s", funcname);...
4
by: James | last post by:
vb.net 2003 i used console.writeline to output to screen. eg console.writeline ("test1 : " & vbtab & v_test1) console.writeline ("test2 : " & vbtab & v_test2) etc etc result becomes
3
by: devrobcom | last post by:
Anyone knowing a better way of creating a format string than this? WCHAR sw_arg; int val1, val2; WCHAR mstring; WCHAR inbuffer1 = L"-value1 100 -value2 1000 -string1 mystring\t"; int read_no2;...
15
by: DanielJohnson | last post by:
I am writing a program in which I am removing all the spaces from the string. I thought that I could do it two ways. One was parsing the string character by character and copying onto another...
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
7
by: moondaddy | last post by:
I need to format a string to it always has 2 digits in it. I'm getting the month like this: DateTime.Now.Month.ToString() Right now since it's August, this returns a string of "8", however, I...
7
by: Andrus | last post by:
How to create format string for decimal data type which shows blank for zero and default format otherwize ? I tried format string "f;f;#" but this shows f for nonzero numbers. Andrus. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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.