Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site brl-tgr.ARPA Path: utzoo!linus!philabs!cmcl2!seismo!brl-tgr!gwyn From: gwyn@brl-tgr.ARPA (Doug Gwyn ) Newsgroups: net.lang.c Subject: Re: ugliness in scanf(3) Message-ID: <10646@brl-tgr.ARPA> Date: Mon, 13-May-85 21:54:51 EDT Article-I.D.: brl-tgr.10646 Posted: Mon May 13 21:54:51 1985 Date-Received: Thu, 16-May-85 04:09:55 EDT References: <10496@brl-tgr.ARPA> <190@mplvax.UUCP> <504@umd5.UUCP> <6411@boring.UUCP> Organization: Ballistic Research Lab Lines: 35 > Something I've tried about every year in the last decade but > haven't got to work on any machine is the following : > > main() { > char buf[64]; > > printf("Gimme string -"); > scanf("%s\n", buf); > ... Try: #include /*ARGSUSED*/ main(argc, argv) char *argv[]; { char buf[64]; (void)printf("Gimme string -"); (void)scanf("%[^\n]", buf); (void)getchar(); /* eat NL */ ... } It is important not to begin or end the format string with whitespace, since that causes ALL whitespace in the input stream at that point to be skipped. In particular, trying to consume the newline with the format statement will cause you to have to type extra stuff before the first line scan is considered complete, and leading whitespace on the second line will be eaten. By the way, what happens if someone types a very long line in response to your prompt? (This sort of thing caused some really bad security loopholes in older UNIX systems.) The safe way to input a line is with fgets() (NOT gets()).