468,720 Members | 1,551 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

Control strings scanf()

#include<stdio.h>
main()
{
char line[80];
scanf("%[^\n]", line);
printf("%s", line);
}

it will read and print the line but what is "%[^\n]" in general we
gives %s, %c .
i searched about it but i still its not clear .

please clear me "%[^\n]" how it is working and geting line with space
till end .
Jun 27 '08 #1
3 9446
Tinku wrote:
#include<stdio.h>
main()
{
char line[80];
scanf("%[^\n]", line);
printf("%s", line);
}

it will read and print the line but what is "%[^\n]" in general we
gives %s, %c .
i searched about it but i still its not clear .

please clear me "%[^\n]" how it is working and geting line with space
till end .
"%[^\n]" means to:
scan
every byte from stdin,
up to but not including the newline character,
and to write a corresponding string in line[].
There is still left a newline character in the input stream,
which you can eat with a call to getc().

"%*[^\n]" means to:
reject
every byte from stdin,
up to but not including the newline character,
and to write a corresponding string in line[].
There is still left a newline character in the input stream,
which you can eat with a call to getc().

/* BEGIN fscanf_input.c */
/*
** There are only three different values
** that can be assigned to the int variable rc,
** from the fscanf calls in this program;
** They are:
** EOF
** 0
** 1
** If rc equals EOF, then the end of file was reached,
** or there is some other input problem;
** ferror and feof can be used to distinguish which.
** If rc equals 0, then an empty line
** (a line consisting only of one newline character)
** was entered, and the array contains garbage values.
** If rc equals 1, then there is a string in the array.
** Up to LENGTH number of characters are read
** from a line of a text stream
** and written to a string in an array.
** The newline character in the text line is replaced
** by a null character in the array.
** If the line is longer than LENGTH,
** then the extra characters are discarded.
*/
#include <stdio.h>

#define LENGTH 40
#define str(x) # x
#define xstr(x) str(x)

int main(void)
{
int rc;
char array[LENGTH + 1];

puts("The LENGTH macro is " xstr(LENGTH) ".");
do {
fputs("Enter any line of text to continue,\n"
"or just hit the Enter key to quit:", stdout);
fflush(stdout);
rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getc(stdin);
}
if (rc == 0) {
array[0] = '\0';
}
if (rc == EOF) {
puts("rc equals EOF");
} else {
printf("rc is %d. Your string is:%s\n\n", rc, array);
}
} while (rc == 1);
return 0;
}

/* END fscanf_input.c */
--
pete
Jun 27 '08 #2
pete wrote:
Tinku wrote:
>#include<stdio.h>
main()
{
char line[80];
scanf("%[^\n]", line);
printf("%s", line);
}

it will read and print the line but what is "%[^\n]" in general we
gives %s, %c .
i searched about it but i still its not clear .

please clear me "%[^\n]" how it is working and geting line with space
till end .

"%[^\n]" means to:
scan
every byte from stdin,
up to but not including the newline character,
and to write a corresponding string in line[].
There is still left a newline character in the input stream,
which you can eat with a call to getc().

"%*[^\n]" means to:
reject
every byte from stdin,
up to but not including the newline character,

and to write a corresponding string in line[].
That doesn't make any sense.
I used copy and paste too much.
There is no corresponding string to write.
There is still left a newline character in the input stream,
which you can eat with a call to getc().

--
pete
Jun 27 '08 #3
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.)
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by happy | last post: by
7 posts views Thread by Buck Rogers | last post: by
95 posts views Thread by hstagni | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Oskars | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.