Why scanf can't parse the input separate by / mark?

Saint

This is my program when I learn scanf function:

#include <stdio.h>
int main(int argc, char *argv[]) {
    int day, year;
    char monthName[20];

    printf("separate day by /\n");
    scanf("%d/%s/%d", &day, monthName, &year);
    printf("%d %s %d\n", day, monthName, year);

    printf("separate day by blank\n");
    scanf("%d%s%d", &day, monthName, &year);
    printf("%d %s %d\n", day, monthName, year);
    return 0;
}

The input and output are below:

separate day by /
3/Dec/2016
3 Dec/2016 0
separate day by blank
3 Dec 2016
3 Dec 2016

Why does the second / mark appears and so does the zero char? Is there any way or tools to analysis such problem?

Daniel

It's because your %s is getting everything after the first / character. So when your %d starts to read, there is nothing left.

You can clearly understand that output by doing this:

printf("separate day by /\n");
scanf("%d/%s/%d", &day, monthName, &year);
printf("%d---%s---%d\n", day, monthName, year);

Which yields:

separate day by /
3/Dec/2016
3---Dec/2016---0

The reason scanf is not respecting the second / as you expected is because %s doesn't stop at the /, it stops only on whitespace characters. Check this from the docs:

Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I read the input string using this scanf function?

From Dev

C scanf() doesn't parse my input

From Dev

Why can't you use scanf() in C for multiple input arguments of different types?

From Dev

Why can't I scanf and printf an integer?

From Dev

Why can't I mark an event as NotNull?

From Dev

can't parse xml input with logstash filter

From Dev

Can't parse twitter hidden input with BeautifulSoup

From Dev

Why I can't escape the question mark (C#)?

From Dev

Why can't I use nth-child on mark tags?

From Dev

Why can't I mark this member function as const?

From Java

Why can't Python parse this JSON data?

From Dev

Why can't I parse YAML with Ruby?

From Dev

why can't find/parse this element in HTML?

From Dev

Why can't I parse this double_?

From Dev

Why can't parse this XML file?

From Dev

Why doesn't scanf() wait for the next input if I previously entered a certain input

From Dev

Scanf doesn't wait for input

From Dev

Can't use 'mark a' or 'mark A' in vim

From Dev

Can't use 'mark a' or 'mark A' in vim

From Dev

Haskell, Don't know why this has a *parse error on input ‘if’*

From Dev

Haskell, Don't know why this has a *parse error on input ‘if’*

From Dev

Why can't I pass a string to @input

From Dev

Why can't I input it at once and output it?

From Dev

Why can't I get the input value?

From Dev

Why can't I reuse my Pikaday calendar in a separate field?

From Dev

Why can't I use globals in separate html and js files?

From Dev

Why a change of dir can't be made in a separate process?

From Dev

Using scanf with a while loop to input two separate strings

From Dev

Why scanf cannot read my input?

Related Related

HotTag

Archive