This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
comma expressions with gcc 2.95.1
- To: gcc-bugs at gcc dot gnu dot org
- Subject: comma expressions with gcc 2.95.1
- From: Roland Winkler <roland dot winkler at physik dot uni-erlangen dot de>
- Date: Mon, 31 Jan 2000 16:35:05 +0100 (CET)
Hi
I wanted to have a macro `QU(x)' which gives me the square of its
argument. And I wanted to do this in a way that the argument is
evaluated only once. Thus my macro was based on a comma expression:
static double dqarg;
#define QU(x) (dqarg=(x), dqarg*dqarg)
For an expression like `z = QU(x) + QU(y);' the expanded code is
z = (dqarg = x, dqarg * dqarg)
+ (dqarg = y, dqarg * dqarg);
However, using gcc-2.95.1 this piece of expanded code is evaluated
like `z=2*x*x' and not `z=x*x+y*y', see the example program attached
below. My system is:
gcc --version 2.95.1
uname -a:
Linux tfkp03 2.2.10 #4 Wed Oct 13 12:35:36 CEST 1999 i686 unknown
libc 2.1.1
Distribution: SuSE Linux 6.2
=================================================================
/* Example programm */
#include <stdio.h>
#include <stdlib.h>
main (int argc, char *argv[])
{
double x, y, z, dqarg;
if (argc < 3) {
printf ("%s: two arguments required\n", argv [0]);
exit (1);
};
sscanf (argv [1], "%lf", &x);
sscanf (argv [2], "%lf", &y);
z = (dqarg = x, dqarg * dqarg)
+ (dqarg = y, dqarg * dqarg);
printf ("%f %f %f\n", x, y, z);
}
--
-----------------------------------------------------------------------
Dr. Roland Winkler
Lst. fuer Theoretische Festkoerperphysik Phone ++49-9131-85 28834
Institut fuer Technische Physik III Fax ++49-9131-85 28833
Universitaet Erlangen-Nuernberg
Staudtstr. 7
D-91058 Erlangen e-mail: roland.winkler@physik.uni-erlangen.de
Germany http://www.tfkp.physik.uni-erlangen.de/
-----------------------------------------------------------------------