Why following code doesn't compile? - c

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((x-a),2.0)+pow((y-b),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.

Related

Macros not producing required result

I am new to C (coming previously from Python). I am confused over this part of code:
#include <stdio.h>
#define square(x) x*x
int main()
{
int x = 36/square(6);
printf("%d", x);
return 0;
}
I don't know why macro square(x) is not producing output 1 and why is it printing 36? Can you shed some light on this?
You need to wrap the macro in parentheses, like this:
#define square(x) (x*x)
The way you've written it, 36/square(6) expands to 36/6*6, which is evaluated as (36/6)*6, or 36.
With parentheses, it will correctly be evaluated as 36/(6*6), or 1.

Difference between macros and Functions in C language

I'm new to C language. When I'm learning C language I learn something called macros. As I understood macros are like a functions in JavaScript that we can call when we needed. But then I learn that after compiling the program all the places that i have called to macros are replaced by the definition of the macro. So I'm confusing what is the difference between macro and a function in C language.
Also I wanna know whether I can write multi line macros in my code and if it is possible how it will be replaced when the code compiled.
As a example assume that I wanna macro to find the maximum value among two numbers.
Is it a good practice to write that process as a macro rather than write is as a function.
Everything stands - don't use macro. A better alternative inline the function - it would achieve the same efficiency you expect. But for fun, I worked a bit to use gcc statement expressions that means a non-portable gcc centric solution.
#include <stdio.h>
#define SUM(X) \
({ long s = 0; \
long x = (X) > 0 ? (X) : (-(X)); \
while(x) { \
s += x % 10; \
x /= 10; \
} \
s; \
})
int main(void) {
printf("%ld\n",SUM(13423) );
return 0;
}
This solution begs for a function. Using statement expression to have that return something feature inside macro. Well I said, go for inline function. That would serve the purpose much cleaner way.
Using macros is sometimes dangourous:
https://gcc.gnu.org/onlinedocs/cpp/Macro-Pitfalls.html#Macro-Pitfalls
you can read on the dangers of using it here..
but, for your question:
#include <stdio.h>
#define SUM(X, Y) (X + Y)
void main() {
printf("the sum of 3,4 is : %d\n", SUM(3,4));
}
will output:
the sum of 3,4 is : 7

C prog error: expected expression before int

This is the program:
#include <stdio.h>
#define round(a) ((a-0.5)<int(a))?int(a):int(a+1)
int main() {
double a = 5.2;
int m = round(a);
printf("%d", m); }
and it shows the error: expected expression before 'int'
round is a name reserved by the standard C library so it is undefined behaviour to call your macro that name (even if you don't include math.h).
Your algorithm could be better expressed like this:
#define my_round(a) ( (int)((a) + 0.5) )
which also has the benefit of only evaluating its argument once.
It would be preferable to use an inline function:
inline int my_round(double d)
{
return d + 0.5;
}
Note that both options cause undefined behaviour if a is outside the bounds of INT_MIN, INT_MAX roughly . If it's in a critical environment you should make your inline function check the bounds of d before doing the conversion to int.
This
#define round(a) ((a-0.5)<int(a))?int(a):int(a+1)
Has the brackets in the wron places
Should be
#define round(a) (((int)((a)-0.5))<(a))?(int)(a):(int)(a+1)
The problem is that int(a) is not valid C.
Redefine your macro as follows:
#define round(a) (((a)-0.5)<(int)(a))?(int)(a):(int)(a+1)
Note that I've also added parentheses around a in (a)-0.5.
P.S. What's the reason for making it a macro and not, say, a function?
The error is because of int(a). Syntactically it is wrong. It should be (int)(a).

macro arguments

What will the program print when the inputs are 2,3?
#include <stdio.h>
#define min(a,b) ((a) > (b) ? (b) : (a))
#define inc(a) a++
#define mult(a,b) (a * b)
int main(void) {
int x = 1, y = 2;
scanf("%d %d",&x,&y);
printf("min(%d,inc(%d))",x,y);
printf("=%d\n",min(x,inc(y)));
printf("min(mult(%d,%d+2),11)",x,y);
printf("=%d\n",min(mult(x,y+2),11));
return 0;
}
edit: I get funny answer for negative numbers i.e -1,-2.
Why is inc(-2) change y to zero instead of -1?
Think of a macro as simply string replacement. Just replace the macro name and parentheses with the body of the macro definition, replacing the macro parameters with what is passed in. An example is easier:
#define hello(a) a+a
...
int y = hello(x);
Would be replaced with:
int y = x+x;
To answer your question, do this manually, and very, very carefully. For nested macros, start with the inside one. Did I mention do this carefully? Don't add or remove any sets of parentheses.
The output would be:
min(2,inc(3))=2
min(mult(2,4+2),11)=11
What do you mean with overwrite?
If you define a function like you did above and call for example this:
inc(x);
.. then the compiler turns it into x++. The variable a is just a name for the "paramter" and will also be replaced by the real variable.
What operating system are you running? you can easily run this yourself and see the results
if your on Windows I would suggest getting CodeBlocks or Visual Studios
if your on Linux or MAC , learn to compile from terminal using gcc or g++

C program output

#include<stdio.h>
#define a(x) (x * x)
int main()
{
int i = 3, j;
j = a(i + 1);
printf("%d", j);
return 0;
}
I want to know why the program is not giving the output 16. (I'm getting the output 7.)
I understood the point very much but if the program is like this:
#include<stdio.h>
#define a(x) (x * x)
int main()
{
int i = 3, j, k;
j = a(i++);
k = a(++i);
printf("%d\n%d", j, k);
return 0;
}
Then why does the above program give the following output:
9
49
Because you made a bad macro:
a(i + 1)
expands to
i + 1 * i + 1
which is equivalent to
i + i + 1
or
2 * i + 1
Use parenthesis:
#define a(x) ((x) * (x))
And then you'll get it to expand to
(i + 1) * (i + 1)
which does what you want.
Read up on C operator precedence and think about what the macro a expands to in this case.
After preprocessing the line
j=a(i+1);
will be:
j=(i+1*i+1);
which when evaluated for i=3 will give j=7:
j=(3+1*3+1);
To get the desired result you need to define the macro as:
#define a(x) ((x)*(x))
which results in:
j=((i+1)*(i+1));
and gives the result 16 when i=3
Because a(i+1) gets preprocessed into (i+1*i+1).
And 3+3+1 = 7.
You might want to use parenthesis around x.
edit: Wow, is this redundant or what. :/
Another write-up for the explanation is at http://en.wikipedia.org/wiki/C_preprocessor#Precedence
Because your macro is wrong. Apparently it works, but the error is more subtle (not quite, but still), as the expanded code has some issues, by not following the expected order of operations.
j = a(i+1) will expand to j = i + 1 * i + 1 which is 7.
If you want to resolve your problem redefine your macro as:
#define a(x) ((x)*(x))
It's good that you've encountered this problem now, and not later. Those type of errors, are sometimes very hard to debug, but now you'll know how to write "professional" macros :).
Because a(i+1) gets preprocessed into (i+1*i+1).
And 3+3+1 = 7.
You might want to use parenthesis around x.
edit: Wow, is this redundant or what. :/
link|flag

Resources