scanf("%d. %d. %d",&d,&m,&y);

S

sophia

why scanf("%d. %d. %d",&d,&m,&y);

is NOT reading the i/p 22 4 1972 correctly,
whereas it is reading the i/p 22. 4. 1972 correctly ?.

Is it because of the dot in the format string of scanf ?
 
I

Ian Collins

sophia said:
why scanf("%d. %d. %d",&d,&m,&y);

is NOT reading the i/p 22 4 1972 correctly,
whereas it is reading the i/p 22. 4. 1972 correctly ?.

Is it because of the dot in the format string of scanf ?

Yes, why did you put them there if you didn't expect them in the input?
 
S

santosh

sophia said:
why scanf("%d. %d. %d",&d,&m,&y);

is NOT reading the i/p 22 4 1972 correctly,
whereas it is reading the i/p 22. 4. 1972 correctly ?.

Is it because of the dot in the format string of scanf ?

Yes. If a character (or characters) in the format string do not have any
special meaning to scanf, then it'll look for a literal match of those
characters in the input.

Here is an extract from man(3) scanf:

The format string consists of a sequence of directives which describe
how to process the sequence of input characters. If processing of a
directive fails, no further input is read, and scanf() returns. A
"failure" can be either of the following: input failure, meaning that
input characters were unavailable, or matching failure, meaning that
the input was inappropriate (see below).

A directive is one of the following:

· A sequence of white-space characters (space, tab,
newline, etc; see isspace(3)). This directive matches
any amount of white space, including none, in the
input.

· An ordinary character (i.e., one other than white space
or ?%?). This character must exactly match the next
character of input.

· A conversion specification, which commences with a ?%?
(percent) character. A sequence of characters from the
input is converted according to this specification, and
the result is placed in the corresponding pointer
argument. If the next item of input does not match the
the conversion specification, the conversion fails ? this
is a matching failure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top