Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!cernvax!chx400!ugun2b!ugsc2a!fisher From: fisher@sc2a.unige.ch Newsgroups: comp.lang.c Subject: Re: Talking about scanf Message-ID: <1990Nov16.125504.305@sc2a.unige.ch> Date: 16 Nov 90 10:55:04 GMT References: <16582@netcom.UUCP> <960@mwtech.UUCP> Organization: University of Geneva, Switzerland Lines: 30 In article <960@mwtech.UUCP>, martin@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.