A macro (see below) to return the greater of two numbers is added by 1. But, the effect of the addition is absent.
PS: I have also checked by adding excessive brackets to the macro.
#define max(a,b) (a >= b ? a : b);
void main(){
int result = max(5,10) 1; //result = 10
}
Solutions: CASE 1:
By manually replacing the macro in the expression (just as the pre-processor does, I think), the addition is performed correctly.
result = (5 >= 10 ? 5 : 10) 1; // Evaluates correctly to 11.
CASE 2: The expression also evaluates correctly when the order of the operands are reversed.
result = 1 max(5,10); //Evaluates correctly to 11.
Why is the original expression not working as intended while the two solution cases do?
CodePudding user response:
You have erroneously added ;
to your macro. So the expansion is actually
void main(){
int result = (5 >= 10 ? 5 : 10); 1;
}
Here your 1
became a separate statement which does nothing, and result
is initialized to 10
.
By the way, if you use gcc or Clang, there is an -E
flag which will run the actual preprocessor on your source code. The code above was obtained by running gcc -E a.c
on your code.
I also recommend compiling with the maximal possible level of warnings. E.g. gcc -Wall -Wextra -Werror a.c
hints that there is something wrong:
a.c:2:6: error: return type of 'main' is not 'int' [-Werror=main]
2 | void main(){
| ^~~~
a.c: In function 'main':
a.c:3:28: error: statement with no effect [-Werror=unused-value]
3 | int result = max(5,10) 1; //result = 10
| ^
a.c:3:9: error: unused variable 'result' [-Werror=unused-variable]
3 | int result = max(5,10) 1; //result = 10
| ^~~~~~
cc1.exe: all warnings being treated as errors
CodePudding user response:
It is because you placed a semicolon after the macro
#define max(a,b) (a >= b ? a : b);
^^^
Remove it.
The macro should be written like
#define max(a,b) ( ( a ) >= ( b ) ? ( a ) : ( b ) )
So in the first case you in fact has
int result = ( 5 >= 10 ? 5 : 10 );
1;
If you will place the declaration
int result = max(5,10) 1;
in the file scope before the function main then the compiler will issue an error because there will be statement
1;
and you may not place statements in the file scope.
As for this statement
result = 1 max(5,10);
then it expands to two statements
result = 1 ( 5 >= 10 ? 5 : 10 );
;
that is there is also a null statement that does not have an effect.
Pay attention to that according to the C Standard the function main without parameters shall be declared like
int main( void )