scanf

A

Ab

What does the "%%" format specifier in scanf do? eg if we write

char ch;
scanf("%%", &ch);

what change if any does it make to ch. If it does not whats the use.
 
N

Nick Keighley

Ab said:
What does the "%%" format specifier in scanf do?

expects a % character
eg if we write

char ch;
scanf("%%", &ch);

what change if any does it make to ch.
none

If it does not whats the use.

see above


--
Nick Keighley

"The Real Programmer wants a "you asked for it, you got it"
text editor--complicated, cryptic, powerful, unforgiving,
dangerous. TECO, to be precise."
 
E

Emmanuel Delahaye

Ab wrote on 19/08/05 :
What does the "%%" format specifier in scanf do? eg if we write

It's the 'escape' value for '%' in a format string.
char ch;
scanf("%%", &ch);

what change if any does it make to ch. If it does not whats the use.

Undefined Behaviour (The number of parameters doesn't match the number
of formaters). Anything could happen.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
K

Keith Thompson

Emmanuel Delahaye said:
Ab wrote on 19/08/05 :

It's the 'escape' value for '%' in a format string.


Undefined Behaviour (The number of parameters doesn't match the number
of formaters). Anything could happen.

Extra arguments to scanf() are evaluated but are otherwise ignored.
C99 7.19.6.2p2.
 
R

Robert Gamble

Emmanuel said:
Ab wrote on 19/08/05 :

It's the 'escape' value for '%' in a format string.


Undefined Behaviour (The number of parameters doesn't match the number
of formaters). Anything could happen.

If there are insufficient arguments the behavior is undefined, extra
arguments are ignored without invoking undefined behavior. The same
goes for the fprintf functions.

Robert Gamble
 
M

Malcolm

Ab said:
What does the "%%" format specifier in scanf do? eg if we write

char ch;
scanf("%%", &ch);

what change if any does it make to ch. If it does not whats the use.
You are reading some economics data, which contains inflation rates. Since
there are many definitions of inflation, the economist has prepared the data
by appending letters, so for instance Keynes'es definition is 2.6%k whilst
Friedman's definition is expressed as 2.6%f.

If you try to scan this data using scanf(), the %f will be interpreted as a
format conversion specifier. So you need to escape the % sign to indicate
that it is a percentage in the text, not a field specifier. To make it
easier, the %k also has to be escaped.
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top