Talking about scanf
fisher at sc2a.unige.ch
fisher at sc2a.unige.ch
Fri Nov 16 21:55:04 AEST 1990
In article <960 at mwtech.UUCP>, martin at mwtech.UUCP (Martin Weitzel) writes:
> [...]
> BTW: Recently there occured a problem, to which I couldn't find an
> immediate solution. Take the following program fragment:
>
> #define X 100
> char word[X+1]; int z;
>
> scanf("%100[^:]:%d", word, &z);
> ^^^----------------------- I'd rather want X here; still
> better were `(sizeof word) - 1', so that I could ommit
> the #define for X completly
Yeah, it would be nice if we could have a `scanf("%*s", sizeof(word), word)'
printf-like type of construct, but then the `*' is already used for something
else...
As another workaround, it seems that some compilers *do* expand macros inside
strings and character constants, in which case you could use the `X' there.
If not, try to look in the the `Token Replacement' chapter of your compiler's
`User Guide'. In TC, for example, you can "stringize" a macro, with the `#'
symbol (i.e. `#X' is converted to `"100"'). Another feature of TC is the
automatic concatenation of string literals (i.e. `"hello " "world"' becomes
`"hello world"'). With these features, you can rewrite your example as
scanf("%" #X "[^:]:%d", word, &z);
Not really portable, you this can be useful.
Markus Fischer, Dpt of Anthropology, Geneva.
More information about the Comp.lang.c
mailing list