This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: question about printf


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]