question about printf

Nathan Sidwell nathan@acm.org
Thu Nov 19 01:07:00 GMT 1998


Kent B Mein wrote:
> 
> I have a kind of goofy question,
> Not sure if this is the place to talk about it or not.
> I scanned through the old posts and couldn't find anything about it.
> 
> Basically here is the problem  (Which occurs only on linux machines)
> 
> printf("%c %c %c %c\n",getchar(),getchar(),getchar(),getchar());
> 
> Preforms the getchar's backwards.  Is this a problem, or is it just
> a coding problem?
C (and C++) do not define the order in which function parameters are
evaluated (they may be in any order including intermixed)[1]. if getchar
is a macro and performs sideffects, you have invoked undefined effects.

see http://www.eskimo.com/~scs/C-faq/q3.2.html for more details.

> The fix is of course to store the getchar's in var's before
> printing them out.
yes.

nathan
[1] if they are functions, C++ does not allow the function evaluation to
be intermixed.
-- 
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
      You can up the bandwidth, but you can't up the speed of light      
nathan@acm.org  http://www.cs.bris.ac.uk/~nathan/  nathan@cs.bris.ac.uk



More information about the Gcc-bugs mailing list