Constants in scanf() Format String? 
Author Message
 Constants in scanf() Format String?

Quick question...

I've got:

#define WIDTH 10

and I want to use it in the format string for a scanf() call.  Using:

int i;

[...]

scanf( "%WIDTHd", &i );

doesn't seem to work, but:

int i;

[...]

scanf( "%10d", &i );

works fine.

What am I doing wrong?

If you could, please send any reply to:

Todd Krissel



Thu, 05 Dec 1996 09:11:48 GMT  
 Constants in scanf() Format String?

|#define WIDTH 10
|
|and I want to use it in the format string for a scanf() call.  Using:
|
|int i;
|scanf( "%WIDTHd", &i );
|
|doesn't seem to work, but:
|
|int i;
|
|scanf( "%10d", &i );
|
|works fine.
|What am I doing wrong?

Unlike the printf() function which can read its width and precision
specifiers from its argument list (by sticking in a `*' where the
width/precision is expected), scanf can't do this. (a `*' causes the
scanf() function to suppress assignment to the corresponding argument.)

But who cares? a `%d' format specifier causes the scanf() function
to skip any leading white space before it attempts to read a decimal
integral number. A simple `%d' format specifier makes scanf() read
strings like `123', `    123'or `        123', etc. correctly.

If you _really_ insist doing this, you have to compose the format
string yourself. Something like this'll do:

        char buf[ENOUGH_CHARS];
        int  i;

        sprintf(buf, "%%%dd", WIDTH);
        scanf(buf, &i);

(please note the double `%%'; it cause the sprintf() function to
output a single `%' character.)

kind regards,


----------------------------------------------------------------------------
Doc, I wanted to talk to you about my problem with premature ... *whoops*



Thu, 05 Dec 1996 18:23:43 GMT  
 Constants in scanf() Format String?

Quote:
>I've got:

>#define WIDTH 10

>and I want to use it in the format string for a scanf() call.  Using:

>int i;
>[...]
>scanf( "%WIDTHd", &i );

>doesn't seem to work, but:

>int i;
>[...]
>scanf( "%10d", &i );

>works fine.
>What am I doing wrong?

You have committed to major errors:

1. The preprocessor doesn't expand macros inside strings.

2. Even outside strings, WIDTHd is _not_ a macro and the preprocessor is
not allowed to touch it.

You can use the '#' preprocessing operator to solve your problem, but the
solution is rather tricky. It is described in the comp.lang.c FAQ.

Dan
--
Dan Pop
Tel:   +41.22.767.5712

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Thu, 05 Dec 1996 19:42:57 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Examples of format strings for scanf/printf ?

2. Spaces in scanf format string

3. Integer macro constant -> string constant

4. using symbolic constants in scanf()?

5. using symbolic constants in scanf()?

6. Cross checking C/C++ string constants with an XML file that has sthe same strings

7. Format specifiers and scanf

8. scanf : floating point format not linked

9. tiny question on format of scanf and printf

10. Error !!! scanf : floating point formats not linked

11. Scanf Format Help!!

12. Fixed Format scanf ??

 

 
Powered by phpBB® Forum Software