usefullness of function name in parenthesis excluding the parameters [duplicate] - c

This question already has answers here:
Invoke function instead of macro in C
(3 answers)
Why surround the function with parentheses?
(1 answer)
What do the parentheses around a function name mean?
(3 answers)
Closed 4 years ago.
I stumbled over a weird piece of code that I thought would not work the way it is intended, but it did. Example:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("%d\n", abs(-1)); // output: 1
printf("%d\n", (abs)(-1)); // output: 1
return 0;
}
Apparently putting parenthesis around the function name in a function call has no effect on the program. It just makes it look like a cast.
I am kinda interested if this is defined somehow. But I figure that it just not forbidden and that's why it works.
What I am really curious about is, is there a case where this kind of notation might yield an advantage in any kind? (structure of the code, readability, anything this might be useful for)
Or is it just a "weird" way to write code?

Try
int foo(int a, int b) { return a+b; }
#define foo(a,b) ((a)+(b)+1)
#include <stdio.h>
int main() {
printf("%d %d\n",
foo(3, 5), // Use macro if defined
(foo)(3, 5) // Always call function even if there's a macro
);
return 0;
}
Output:
9 8
See the difference?

Related

Define in C does not work when input is composite [duplicate]

This question already has answers here:
What is argument evaluation?
(2 answers)
Closed 6 years ago.
Problems using define in C. Works well when I call OP(9), but when I call OP(7+2) I get 23. Why?
#include<stdio.h>
#include<stdlib.h>
#define OP(x) x*x;
int main() {
int x,y;
x = 2;
y = OP(7+2);
printf("%d", y);
return 0;
}
Why prints 23 and not 81?
You should wrap x in parentheses in order to force precedence. However, it is also essential to wrap the entire expression in parentheses.
#define OP(x) ((x)*(x))

strange behavior of #define command [duplicate]

This question already has answers here:
Syntax error while using a #define in initializing an array, and as arguments to a function in C? [closed]
(2 answers)
Closed 5 years ago.
I've tried to figure out this behavior of the define command in C (I'm new with that). I've got this code and I don't know why I see in the output that myAge=15 and not 16 (I know it's 15, but I don't know why). Anybody can help me to find out why does it happen?
this is the code:
#include <stdio.h>
#include <stdlib.h>
#define AGE 15;
int main(void)
{
float myAge = AGE + 1;
printf("Hello!\n");
printf("My name is Raz and I am %d years old!\n", myAge);
system("PAUSE");
return 0;
}
Thanks for helping :)
#define is a textual replacement performed by the preprocessor prior to the compilation step. In this case, you're asking the preprocessor to expand the token AGE to 15;. The semicolon is part of the expansion, so this is the code you would get after the preprocessing step:
float myAge = 15; + 1;
As you can see, it does not expand to what you expect.
You can fix this issue by removing the semicolon from the #define:
#define AGE 15
Better yet, avoid using the preprocessor for simple numerical constants - consider using a const int instead:
const int age = 15;

Strange behavior of #define [duplicate]

This question already has answers here:
Strange behaviour of macros C/C++
(5 answers)
Closed 8 years ago.
I have the following piece of code.
#include <stdio.h>
#define d 10+10
int main()
{
printf("%d",d*d);
return 0;
}
As 10+10=20, I thought that d would be 20 everywhere in the program. But when I execute d*d, I have expected the result to be d*d=20*20=400. But the result gets printed as 120. Can anyone give me an explanation for this behavior?
Preprocessor is just doing simple "find & replace", so this code:
printf("%d",d*d);
changes to
printf("%d",10+10*10+10);
which is 10+100+10 = 120
That's why it's so important to add parens in defines:
#define d (10+10)
printf("%d",d*d);
Here d is replaced as printf("%d",10+10*10+10). so in this case it first executes 10*10 and adds with 10+100+10. so results 120.
To eliminate this-
#define d (10+10) // FIX
int main()
{
printf("%d",d*d);
return 0;
}
Change
#define d 10+10
to
#define d (10+10)
What you get without the proper parentheses is 10+10*10+10 which is the same as 10 + (10 * 10) + 10
printf("%d",d*d) will evaluate (after preprocessing) to printf("%d", 10+10*10+10);

what does the code `#define LOG_DBG(x)` mean? [duplicate]

This question already has answers here:
#define IDENTIFIER without a token
(4 answers)
Closed 8 years ago.
First, it declares the macro LOG_DBG(x) just like this:
#define LOG_DBG(x)
And then, use it like this:
LOG_DBG((LOG_MISC, 80, __FUNCTION__": select reports %d", res));
And I can't understand what the macro means ? What it will do ?
It does exactly nothing. It gets preprocessed to an empty string. This may be a somewhat convenient way of disabling/enabling debug log messages. The code was probably written like this at some point:
#define LOG_DBG(x) some_logging_function(x)
But then someone wanted to simply get rid of all the log messages in one shot.
It will do nothing. You're seeing the "disabled" form of the macro, which is used to make the logging calls go away on non-debug builds.
There should be another declaration of the macro somewhere, or the code is perhaps just "left" in the non-debugging state.
You've defined a macro that receives x as parameter but do nothing. Here's a concrete use of macro:
#include <stdio.h>
#define LOG_DBG(x) ((x)+5)
int main(int argc, char *argv[])
{
int x = LOG_DBG(3);
printf("%d\n", x);
return 0;
}
Here, we did something in the macro and hence the number 8 will be printed.
Of course, if you want the logarithm of x, it would be another expression.

Why is the return value not what I expected in this C program with macro? [duplicate]

This question already has answers here:
Strange behaviour of macros C/C++
(5 answers)
Strange behavior of Macro-expansion
(3 answers)
Closed 9 years ago.
When I run the following code, the return value is 11, but I was expecting it to return 25. Can someone explain this?
#include<stdio.h>
#define SQR(a) a*a
int main()
{
int i=3;
printf("%d",SQR(i+2));
return 1;
}
Needs more parentheses. This:
#define SQR(a) a*a
expands to this:
i+2*i+2
which is:
3+2*3+2
which is 11 because * has precedence over +.
You need to define your macro like this:
#define SQR(a) ((a)*(a))
to ensure that this kind of thing doesn't happen.
Macros are not the same as regular functions.
During the reprocessing all macros are replaced exactly by what they are define. In your case, the line:
printf("%d",SQR(i+2));
is replaced by the line:
printf("%d", i+2*i+2);
So, you see the unexpected result there.
The correct way is:
#define SQR(a) ((a)*(a))
The preprocessor result will be:
printf("%d", ((i+2)*(i+2)));
Try to learn on this mistake. Issues of this kind are quite hard to debug.

Resources