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.)

5

7 Answers 7

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.

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.

2
  • 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(). Apr 1, 2013 at 8: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 1, 2013 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.

1
  • Wrong slash. Also, this answer adds nothing to previous answers. May 30, 2017 at 15:18
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);

1
  • The other answers stated this 8 years ago. This adds nothing new. Feb 20, 2021 at 18:23
-1

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

3
  • 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() Apr 1, 2013 at 8: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 1, 2013 at 9:09
  • 1
    My suggestion is to read the fscanf manual so that you can clear up the confusion you have in regards to the %[ directive and the %s directive.
    – autistic
    Apr 1, 2013 at 9:11
-1

Don't use the escape sequence in scanf stdio function

     scanf ("%s", a);
1
  • 3
    There's no explanation of why — and what happens when you do. This adds nothing to the answer by MOHAMED, or the accepted answer. Jan 17, 2017 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.