On May 14, 8:27 am, Tinku <sumit15...@gmail.comwrote:
#include<stdio.h>
main()
{
char line[80];
scanf("%[^\n]", line);
printf("%s", line);
}
Some examples:
scanf("%[0-9]",line); matches any number of digits (and stores into
line)
(On many systems, this is implementation
defined)
scanf("%[abc]",line); matches any number of consecutive 'a' or 'b'
or 'c'
characters, and they are stored into line.
scanf("%s",line); skips(eats) initial white spaces, all
characters
until an other white space comes (word goes
into line)
scanf(" %[abc}",line); skips initial white spaces, then like "%[abc]"
scanf(" %[abc] ",line); skips initial and trailing white spaces...
scanf("%[^abc]",line); reads a sequence of characters into line until
an 'a', 'b' or 'c' is found, which is left in
the stream
scanf("%[\n]",line); consumes blank lines and put the corresponding
number
of '\n' characters into line.
scanf("%[^\n]",line); puts all characters into line until a newline
comes,
this '\n' character is kept in the stream
All these were quite dangerous, as a crappy input might put you in the
dark
realm of undefined behaviour.
scanf("%*[\n]"); eats and ignores all blank lines until
something
else comes
scanf("%80[^\n]",line); same as "%[^\n]", but will not put more than
79
characters into line. You can put line[79] to
zero (prior to scanf) and check it afterwards
to
see if the input was truncated before a
newline.
Alternatively:
scanf("%80[^\n]%[\n]",line,other_char_array)
will return 2 if ok (eating the newline)
or 1, if the line was too long.
Alternatively:
fgets(line,sizeof(line),stdin) but that will eat the newline, too
Also when using %s or %c, use the length modifier, to set the number
of bytes
in the character array that receives the data. %80s will stop at a
whitespace,
and put a '\0' character to the end, %80c will do neither.
Szabolcs
ps: Did you really search for this information? I might have plainly
refer you
to 7.19.6.2 in
http://www.open-std.org/jtc1/sc22/wg...docs/n1124.pdf
or simply search for man scanf (which will tell more about the
implementation
defined behaviour on an implementation of not yours.)