z error

B

Bill Cunningham

I keep trying to compile this code and I don't see anything wrong. The
compiler complains about a syntax error involving z. There is more than one
z on the line in question and I am out of ideas.

/*oscillator*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 3) {
puts("os usage error");
exit(1);
}
double z = 0;
double x = strtod(argv[1], NULL);
double y = strtod(argv[2], NULL);
printf("%.2f\n", ((z = (x - y)) z * 100) / x);
return 0;
}

What's wrong?

Bill
 
M

Martin Ambuhl

Bill said:
printf("%.2f\n", ((z = (x - y)) z * 100) / x);
What's wrong?

Look at the 2nd argument to the printf call.

((z = (x - y)) z * 100) / x

The "expression" which is being divided by x is

(z = (x - y)) z * 100

The assignment is just fine:

z = (x - y)

This involves assigning a value to z and

z * 100

involves retrieving (some) value of z.

This in itself is an error, since there is no sequence point between
these uses of z, one for retrieving a value and one for storing a value
in it.

So let's move the subexpression out of the printf statement, giving us

z = (x - y)
printf("%.2f\n", (z z * 100) / x);

Notice that the _second_ error should now be obvious to you, since in

(z z * 100)

There is no operator between the two occurrences of z.

Your statement
There is more than one z on the line in question and I am out of ideas.
is strangely to the point, since every occurance of z is involved in two
errors.
 
C

CBFalconer

Bill said:
I keep trying to compile this code and I don't see anything wrong.
The compiler complains about a syntax error involving z. There is
more than one z on the line in question and I am out of ideas.

/*oscillator*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 3) {
puts("os usage error");
exit(1);
}
double z = 0;
double x = strtod(argv[1], NULL);
double y = strtod(argv[2], NULL);
printf("%.2f\n", ((z = (x - y)) z * 100) / x);
return 0;
}

What's wrong?

In C90 you have to declare the variables before the code block.
Turn up your error detection level. With gcc use at least "-W
-Wall -ansi -pedantic".
 
B

Bill Cunningham

This in itself is an error, since there is no sequence point between these
uses of z, one for retrieving a value and one for storing a value in it.

So let's move the subexpression out of the printf statement, giving us

z = (x - y)
printf("%.2f\n", (z z * 100) / x);

Notice that the _second_ error should now be obvious to you, since in

(z z * 100)

There is no operator between the two occurrences of z.

Your statement
is strangely to the point, since every occurance of z is involved in two
errors.

So is what I am doing in this equation equal to
2z * 100 ?

The second z must be taken out. I see. But don't do this.

2z * 100
-- ---
2 2

Or I would have x=50.

Bill
 
S

s0suk3

Bill said:
I keep trying to compile this code and I don't see anything wrong.
The compiler complains about a syntax error involving z. There is
more than one z on the line in question and I am out of ideas.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
    if (argc != 3) {
        puts("os usage error");
        exit(1);
    }
    double z = 0;
    double x = strtod(argv[1], NULL);
    double y = strtod(argv[2], NULL);
    printf("%.2f\n", ((z = (x - y)) z * 100) / x);
    return 0;
}
What's wrong?

In C90 you have to declare the variables before the code block.
Turn up your error detection level.

Turning up the error detection level won't disallow that.
 With gcc use at least "-W -Wall -ansi -pedantic".

The "-W -Wall" part is what will increase the warning level, but it
won't disallow mixing of statements and declarations. The "-ansi -
pedantic" part, which has nothing to do with warnings, is what will
disallow it, because it makes gcc conform to C89, and C89 didn't
support this feature.

Sebastian
 
M

Martin Ambuhl

So is what I am doing in this equation equal to
2z * 100 ?

No. (z z) with no operator between the two z's is completely
meaningless. Any attempt on your part to infer a meaning is completely
wasted (and wasteful) effort.
 
A

Antoninus Twink

In C90 you have to declare the variables before the code block.
Turn up your error detection level. With gcc use at least "-W
-Wall -ansi -pedantic".

As usual, CBF completely misses the point in a quite spectacular
fashion. The sooner the coming of the eternal void puts him out of his
misery, the better off we'll all be.
 
H

Harald van Dijk

Bill Cunningham said:
I keep trying to compile this code and I don't see anything wrong.
The
compiler complains about a syntax error involving z. There is more than
one z on the line in question and I am out of ideas.
[...]
printf("%.2f\n", ((z = (x - y)) z * 100) / x);
^^^
You some kind of operator or separator between the expressions.

Unless that operator is <,>, you don't know if the <z*100> is the new
or original value of z; but you're missing a second formatter if it is
<,>.

If a comma is added, then you get
printf("%.2f\n", ((z = (x - y)), z * 100) / x);
where printf is called with two arguments -- the second of which contains
a parenthesised comma operator -- and the format specifier matches the
second argument's type.
 
B

Ben Bacarisse

S M Ryan said:
Bill Cunningham said:
I keep trying to compile this code and I don't see anything wrong. The
compiler complains about a syntax error involving z. There is more than one
z on the line in question and I am out of ideas.

/*oscillator*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 3) {
puts("os usage error");
exit(1);
}
double z = 0;
double x = strtod(argv[1], NULL);
double y = strtod(argv[2], NULL);
printf("%.2f\n", ((z = (x - y)) z * 100) / x);
^^^
You some kind of operator or separator between the expressions.

Unless that operator is <,>, you don't know if the <z*100> is the new or
original value of z;

I don't think so. The original value will be used unless the missing
operator is a comma.
but you're missing a second formatter if it is <,>.

Again, I don't think so. There would still be only one expression
after the format string.

[BTW, I found your use of <> for quotes rather confusing until I
worked it out.]
 
H

Harald van Dijk

I don't think so. The original value will be used unless the missing
operator is a comma.

z is both read from and written to with the only possible intervening
sequence point introduced by the missing operator. If the operator is +,
for example, there is no sequence point, so the behaviour is undefined and
there is no requirement for the original value of z, its new value, or
even any value at all to be used.
 
O

osmium

Ben Bacarisse said:
S M Ryan said:
Bill Cunningham said:
I keep trying to compile this code and I don't see anything wrong.
The
compiler complains about a syntax error involving z. There is more than
one
z on the line in question and I am out of ideas.

/*oscillator*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 3) {
puts("os usage error");
exit(1);
}
double z = 0;
double x = strtod(argv[1], NULL);
double y = strtod(argv[2], NULL);
printf("%.2f\n", ((z = (x - y)) z * 100) / x);
^^^
You some kind of operator or separator between the expressions.

Unless that operator is <,>, you don't know if the <z*100> is the new or
original value of z;

I don't think so. The original value will be used unless the missing
operator is a comma.
but you're missing a second formatter if it is <,>.

Again, I don't think so. There would still be only one expression
after the format string.

[BTW, I found your use of <> for quotes rather confusing until I
worked it out.]

Bill would be far better off if he would quit trying to be cryptic until he
has at least *some* grasp of the fundamentals. But, as far as I can tell,
Bill is virtually immune to listen to advise from anyone. There is nothing
wrong with giving printf the name of a variable that already contains a
value to be printed.
 
B

Ben Bacarisse

Harald van Dijk said:
z is both read from and written to with the only possible intervening
sequence point introduced by the missing operator. If the operator is +,
for example, there is no sequence point, so the behaviour is undefined and
there is no requirement for the original value of z, its new value, or
even any value at all to be used.

Oh, sorry, yes. I missed a set of brackets. Ironically exactly the
same ones that I needed to see to make the other remark I made
correct!
 
T

Tim Rentsch

Ben Bacarisse said:
S M Ryan said:
Bill Cunningham said:
I keep trying to compile this code and I don't see anything wrong. The
compiler complains about a syntax error involving z. There is more than one
z on the line in question and I am out of ideas.

/*oscillator*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 3) {
puts("os usage error");
exit(1);
}
double z = 0;
double x = strtod(argv[1], NULL);
double y = strtod(argv[2], NULL);
printf("%.2f\n", ((z = (x - y)) z * 100) / x);
^^^
You some kind of operator or separator between the expressions.

Unless that operator is <,>, you don't know if the <z*100> is the new or
original value of z;

I don't think so. The original value will be used unless the missing
operator is a comma.

&& , ||
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top