Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #11 (permalink)  
Old August 7th, 2004, 03:22 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't agree: since the behaviour is undefined, there is no such thing as a "wrong" answer any more than there is a "right" answer.

You can play games, guessing how different implementations might handle it, but compilers are not required to give answers consistent with your guesses about specific implementations. (No matter how reasonable your assumptions seem to you.)

The point of the exercise is not to decide which is the more nearly correct answer, but to caution programmers that operators or functions with side effects can give results that mislead you.

Note that there is nothing illegal about the syntax of any of the statements, but they are symantic nonsense, since the C standard is very explicit that order of evaluation of all functions and of certain operators (including ++) is undefined.

Here's a simple rule of thumb. If you see an expression with ++ or -- applied more than once to the same variable, raise the red flag: change it!!!

Regards,

Dave
Reply With Quote
  #12 (permalink)  
Old August 7th, 2004, 04:04 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

yes,okay
>>what values for a,b,c,r after below instruction(r=b=c=0,a=-1)
r=++a||++b||++c
---
answer:
a=0,b=1,c=0,r=1
Do you know why c is zero?because in OR when b gets 1,compiler doesn't consider ++c
(I dont know what could happand to them in Borland)
Cheers.

--------------------------------------------
Mehdi.:)
Reply With Quote
  #13 (permalink)  
Old August 7th, 2004, 08:59 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Now here's one that has an unambiguously unequivocal "right" answer, since the C standard says that expressions connected by the "||" are evaluated left to right, and whenever a non-zero value is found for an term in the expression, no further evaluation takes place. Note that ++ has a higher precedence than ||, so the first thing that is evaluated is ++a. This sets a to a new value 1, and the value of the expression is the new value of a, and since this is non-zero, no further evaluation takes place past the ||.

Borland gives the same (correct) answer, as does Gnu gcc, and all of the other valid C compilers, including Microsoft Visual C++.

What's the answer?

compile and run this:

Code:
#include <stdio.h>

int main()
{

  int a = 0;
  int b = 1;
  int c = 0;
  int r;

  r=++a||++b||++c;

  printf("a = %d, b = %d, c = %d, r = %d\n", a, b, c, r);
  return 0;
}
If you don't get the following results, ask for your money back on your C compiler:

a = 1, b = 1, c = 0, r = 1


Dave




Reply With Quote
  #14 (permalink)  
Old August 7th, 2004, 09:18 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by davekw7x
 Now here's one that has an unambiguously unequivocal "right" answer, since the C standard says that expressions connected by the "||" are evaluated left to right, and whenever a non-zero value is found for an term in the expression, no further evaluation takes place. Note that ++ has a higher precedence than ||, so the first thing that is evaluated is ++a. This sets a to a new value 1, and the value of the expression is the new value of a, and since this is non-zero, no further evaluation takes place past the ||.

Borland gives the same (correct) answer, as does Gnu gcc, and all of the other valid C compilers, including Microsoft Visual C++.

What's the answer?

compile and run this:

Code:
#include <stdio.h>

int main()
{

  int a = 0;
  int b = 0;
  int c = 0;
  int r;

  r=++a||++b||++c;

  printf("a = %d, b = %d, c = %d, r = %d\n", a, b, c, r);
  return 0;
}
If you don't get the following results, ask for your money back on your C compiler:

a = 1, b = 0, c = 0, r = 1


Dave




Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
A beginner who needs need help Jamesbizprocom Beginning PHP 1 June 28th, 2007 11:02 PM
c++ beginner, need help please jmarsh56 Visual C++ 0 December 7th, 2005 10:38 AM
Beginner Tilak_1 C# 2 January 21st, 2005 11:19 PM
Beginner [email protected] Beginning PHP 4 December 23rd, 2004 03:22 PM
Beginner programmed XML 3 February 21st, 2004 05:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.