Re: sscanf question



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

.



Relevant Pages

  • Re: Checking for a keypress on Linux ?????
    ... the short delay in kbhit() ... > int getch; ... > * returns the number of characters available to read. ... > static struct termios Otty, ...
    (comp.os.linux.development.apps)
  • Re: [PATCH] Fix console utf8 composing
    ... So, the result is that the result of the composing operation is still taken from the accent_table, and thus cannot be more than "unsigned char" allows. ... in UTF-8 mode, it is possible to generate characters from Latin-1 set by composing, and they are generated correctly. ... +static int use_unicode; ...
    (Linux-Kernel)
  • Re: [PATCH] console UTF-8 fixes
    ... as substitute glyph ... don't ignore zero-width characters (except for a few zero-width spaces ... print an extra space for double-wide characters for the cursor to stand ... int first; ...
    (Linux-Kernel)
  • Re: Get ASCII value for character when higher than 127
    ... The reason I want to store the int-values for the characters in stead ... I also imagined, that if I have the int value, I can perform some ... char timeString; ... strcat; ...
    (microsoft.public.vc.language)
  • Re: Get ASCII value for character when higher than 127
    ... The reason I want to store the int-values for the characters in stead ... I also imagined, that if I have the int value, I can perform some ... char timeString; ... strcat; ...
    (microsoft.public.vc.language)