Re: sscanf question
- From: Han from China - Master Troll <autistic-pedantry@xxxxxxxxxxx>
- Date: Sun, 18 Jan 2009 06:20:24 -0700 (MST)
Zach wrote:
Two additional questions:
Sure. Keep 'em coming, bro. The judge made me sit here answering C
questions for my community service.
In the sscanf call what does this mean: "%31[^;]%n"
I am familiar with %n but not %31 nor what the [^;] indicates.
The "%31[^;]" is another conversion specification. The number 31 after the
% is called a maximum field width. It basically restricts the scan to no
more than 31 characters. You'll notice the field[] buffer has 32
characters. The extra character is room for the NUL terminator, which
sscanf() will add automatically. Between the '[' and the ']' is something
called a scanlist. In this case, the scanlist starts with the circumflex
'^', which has a special meaning -- it says, "Hey, give me anything not in
the list that follows!" Note that the '[' specifier is one of the few
exceptions I mentioned in my other post: It doesn't skip initial
whitespace characters. The end result of "%31[^;]" is to dump up to 31
non-semicolon characters into the field[] buffer and add a NUL terminator.
Also why is there not a semicolon after the sscanf call inside the
whale statement? I tried adding one but it then causes an error. Is
this defined somewhere in the C99 standard?
C has a thing called an expression statement. An expression statement has
the following form
expression ;
where 'expression' is optional. If missing, we have a semicolon by
itself, yielding a null statement.
So when you write
sscanf(...) ;
sscanf(...) returns a value, which becomes the value of the expression.
That value is silently discarded in this case.
Now, C has another kind of statement called an iteration statement. One
example of an iteration statement is the while() construction:
while (expression) statement
So you plop an expression in there such as
sscanf(...)
or
sscanf(...) == 1
in your case. Semicolons aren't used.
As some bonuses, try compiling the following code:
int main(void)
{
42;
return 0;
}
Is the result what you expect? Also try a null statement:
int main(void)
{
;
return 0;
}
Where might that be used?
Yours,
Han from China
--
"Only entropy comes easy." -- Anton Chekhov
.
- Follow-Ups:
- Re: sscanf question
- From: Bill Cunningham
- Re: sscanf question
- References:
- Re: sscanf question
- From: Zach
- Re: sscanf question
- Prev by Date: Re: Question on strncmp / strnicmp use
- Next by Date: Re: sscanf question
- Previous by thread: Re: sscanf question
- Next by thread: Re: sscanf question
- Index(es):
Relevant Pages
|