26
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *method1(void)
{
    static char a[4];
    scanf("%s\n", a);
    return a;
}

int main(void)
{
    char *h = method1();
    printf("%s\n", h);
    return 0;
}

When I run the code above, the prompt is asking me twice for input (I only use scanf once in the code). Why is that?

(I entered 'jo'; it asked for more input, so I entered 'jo' again. Then it only printed out 'jo' once.)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
joy
  • 721
  • 2
  • 11
  • 18

7 Answers7

27

From my scanf manual page

White space (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else matches only itself.

Thus with scanf ("%s\n", a) it will scan for a string followed by optional white space. Since after the first newline more whitespace may follow, scanf is not done after the first newline and looks what's next. You will notice that you can enter any number of newlines (or tabs or spaces) and scanf will still wait for more.

However, when you enter the second string, the sequence of whitespace is delimited and scanning stops.

Use scanf ("%s", a) to not scan trailing whitespace.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jens
  • 69,818
  • 15
  • 125
  • 179
13

you have to remove the \n from the string format of the scanf. It should be

scanf("%s",a);

EDIT: Explanation

the %s means that the scanf reads the input character till it gets a delimiter which should be a white space like space or tab or new line(\n) so the first enter is get as a delimiter for the "%s" and adding the "\n" to the string format "%s\n" means that the scanf will wait 2 newlines the first newline is related to the delimiter of the "%s" and the second newline is related to the\n of the string format.

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • 2
    You're on the right track, but you've not said that `scanf` will keep reading white space until it comes across a non-space character. You could enter many new lines (and blank or tabs) before a non-space is entered and stops `scanf`. One more reason to prefer `fgets()` and `sscanf()` over the file I/O variants of `scanf()`. – Jonathan Leffler Apr 01 '13 at 08:41
  • 1
    Your reasoning about scanf waiting for two newlines is wrong. All newlines entered (even more than two) are scanned by the **single** `\n` in the format, as are any intermingled spaces and tabs. Please read the scanf specification in the C standard or the manual page. – Jens Apr 01 '13 at 22:06
0

Remove \n from the scanf format and give an input and it displays the output based on the given output once.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

you can use either of these to avoid the mentioned problem : scanf("%s",a); or scanf("\n%s",a);

0

Try this: Don't use \n on scanf, it won't ask you twice and sometimes it might show an error

Your code: scanf("%s\n", a);

Try this on scanf: scanf("%s", a);

Ramesh
  • 11
  • 1
-1

use gets() or fgets() instead...alternatively use scanf("%[^\n]s",a);

Darshan Shah
  • 57
  • 1
  • 7
  • scanf inputs into the array till you it receives a space..so use %[^\n]s...or dont use scanf, instead go for fgets() or gets() – Darshan Shah Apr 01 '13 at 08:26
  • 3
    `"%[^\n]s"` is composed of two directives: `%[^\n]`, which either matches (and stores) one or more non-'\n' characters, or fails because it encountered a '\n' immediately, and `s` which matches a literal 's' and discards it, or fails and pushes the non-'s' character back onto stdin. The first directive will fail for consecutive attempts, because '\n' is not 's', so '\n' will be pushed straight back onto stdin and that is the first character encountered. See for yourself: `char a[64]; int n = scanf("%[^\n]s", a); assert(n == 1); n = scanf("%[^\n]s", a); assert(n == 1);` – autistic Apr 01 '13 at 09:09
  • 1
    My suggestion is to read the [fscanf](http://pubs.opengroup.org/onlinepubs/007904975/functions/fscanf.html) manual so that you can clear up the confusion you have in regards to the `%[` directive and the `%s` directive. – autistic Apr 01 '13 at 09:11
-1

Don't use the escape sequence in scanf stdio function

     scanf ("%s", a);
Vijay S B
  • 1,251
  • 1
  • 13
  • 24
  • 3
    There's no explanation of why — and what happens when you do. This adds nothing to the [answer](http://stackoverflow.com/a/15740060/) by [MOHAMED](http://stackoverflow.com/users/1003575/mohamed), or the accepted answer. – Jonathan Leffler Jan 17 '17 at 20:11