简体   繁体   中英

Why following code doesn't compile?

Hi can't find the mistake in my code. I'm still newbie in programming. So please don't be so mean to me.

The compiler says:

Line:23 error: expected ')' before ';' token

Line:24 error: expected ';' before ')' token

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

#include <stdbool.h>
#include <time.h>
#include <unistd.h>

#define CENTER(a,b,x,y,g)       g*sqrt(pow((x-a),2.0)+pow((y-b),2.0);

int size=5,location_x=10,location_y=10,s=NULL,l_x, l_y, status=2;

int main(void)
{
    srand(time(NULL));
    float location[l_x][l_y];
    int x[size], y[size], z[size];

    l_x=1+rand()%100;
    l_y=1+rand()%100;

    for (int i=0; i<size; i++){
        location[l_x][l_y] += CENTER(x[i], y[i], location_x, location_y, z[i]);
    }
    return 0;
}

The error message is because the parentheses on your macro are unbalanced:

//opening ->                    1   23             45
#define CENTER(a,b,x,y,g) g*sqrt(pow((x-a),2.0)+pow((y-b),2.0);
//closing ->                             1    2         3    4

and can be fixed by simply placing another closing parenthesis after the fourth closing one, assuming the expression is meant to be:
在此输入图像描述

And you really don't want to have a semi-colon on the end of it, a better example would be (but see below):

#define CENTER(a,b,x,y,g) g*sqrt(pow((x-a),2.0)+pow((y-b),2.0))

The semi-colon is particularly annoying if you use the macro anywhere other than the end of a statement which, luckily, you're not doing, but may well at some point in the future. For example, this construct will fail insidiously with the semicolon:

x = CENTER(1,2,3,4,5)+42;

Insidious since it will compile but won't do what you think it will, for example, the following code will not print out 100 :

#include <stdio.h>
#define FN(x) (x);
int main (void) {
    int xyzzy = FN(10) + 90;
    printf ("%d\n", xyzzy);
    return 0;
}

That's because, after pre-processing, you end up with:

    int plugh = (10); + 90;

which is actually two valid C statements, the first setting xyzzy to ten, the second evaluating (but throwing away) the expression + 90 .


However (and this is the "see below" bit mentioned above), I'd go further and say you probably shouldn't be using a macro here at all. The three main use cases for macros (from the earliest days of C) can generally be divided into:

  • conditional compilation;
  • inlining of code; and
  • constants.

With the second generally being obsolete because of insanely optimising compilers, and the third being less useful than enumerations (unless you want to pass in compile-time configurable constants such as gcc -DUNITS_PER_BIN=42 ... ), I'd suggest conditional compilation is the only place you should be using macros nowadays.

Instead, I would simply use the function:

inline float center (float a, float b, float x, float y, float g) {
    return g * sqrt (pow ((x - a), 2.0) + pow ((y - b), 2.0);
}

(but with more descriptive variables if possible).

I generally don't even use inline nowadays since the compilers can generally figure it out, but I've put it there for completeness.

This also gets rid of a lot of the problems of function-like macros such as when you pass something like var + 7 as your (unparenthesised) g variable and find out the expression isn't giving you want you wanted because of operator precedence:

var + 7 * sqrt (pow ((x - a), 2.0) + pow ((y - b), 2.0))

when what you really wanted was:

(var + 7) * sqrt (pow ((x - a), 2.0) + pow ((y - b), 2.0))

Perhaps this:

#define CENTER(a,b,x,y,g)       g*sqrt(pow((x-a),2.0)+pow((y-b),2.0);

...needs an extra parenthesis to close the expression, like this:

#define CENTER(a,b,x,y,g)       g*sqrt(pow((x-a),2.0)+pow((y-b),2.0))

You should also remove the trailing semi-colon.

you need one ) at the end of g*sqrt(pow((xa),2.0)+pow((yb),2.0);

use

#define CENTER(a,b,x,y,g)    g*sqrt(pow((x-a),2.0)+pow((y-b),2.0))
                                                                 ^

And remove ; as well at the end

to create a constant you use the #define , but this statement does not want a ; sign behind it's calcolation. lso you are missing a ) at the end of the statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM