"?" mark in C macro definition [duplicate] - c

This question already has answers here:
What does this do? [duplicate]
(4 answers)
Closed 8 years ago.
I got a line in the generic code of stm32f0 so often and I can't get it clearly. what does it mean by the line below. I know its complicated to understand this way. But my point is the question mark(?) in the define. Can somebody explain that.
#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))

it is a short form of condition,
you can write
int k = (i > j) ? i : j;
this will asign i to k if i > j else it will asign j.
this is the way we choose between two options inside a macro

Here a conditional operator is used .
It says if expr is true then the value of the Macro assert_param(expr) will be zero or otherwise
it will call another macro or function assert_failed() with two parameters as input .
1st is FILE macro which is the filename from which the macro has been called and another is LINE
macro which is the line number in the file from where the macro has been called.
The conditional operator format is like this:
a?b:c
It means if a is true then b will be the result otherwise c will be the result.
Accordingly you can separate these statements in the expression.

Related

How to append literal C-string to literal C-string by a macro? [duplicate]

This question already has answers here:
How do I concatenate const/literal strings in C?
(17 answers)
Closed 1 year ago.
Considering that macros in C are processed by the preprocessor before compiling the project, as well as what is in macros with variables, etc. work as with text, is it possible in some way to add another to one line, but in such a way that it is performed at the level of complementing the source code, and not at runtime?
Example:
#define VERSION_CODE "1.0"
#define ADD_VERSION(base) (base!" !"VERSION_CODE)
int main() {
printf(ADD_VERSION("MyProgram"));
return 0;
}
Here, the !" symbol denotes the removal of the quote to the right of the argument and to the left of the existing line.
I would not like to perform this action at runtime, since all the constituent strings are already known.
You don't need to remove the quotes to do this - the compiler will remove them by itself. One of the features of the C-compiler is that it itself does the concatenation of literal strings. This works in both C and C ++.
You can write like this:
#define VERSION_CODE "1.0"
#define ADD_VERSION(base) (base VERSION_CODE)
int main() {
printf(ADD_VERSION("MyProgram"));
return 0;
}
As a result, you will receive the following output:
MyProgram1.0

What is happening in this C code? (From interview) [duplicate]

This question already has answers here:
Square of a number being defined using #define
(11 answers)
Closed 5 years ago.
I was given a variation of this C code in an interview recently, and asked what would be returned by the function.
#define fun(a) a*a
int main() {
return(fun(4+5));
}
I've run it with a printf("%d"...) in place of the return and it prints 29. No other types than "%d" showed a result. Can someone explain what is going on here?
a is expanded, not evaluated as a. So you get:
4+5*4+5
and with operator priorities; you get 4 + 20 + 5 => 29
better way (with extra outside parenthesis for protecting against outside operators too thanks to Thomas comment):
#define fun(a) ((a)*(a))
but all parenthesis of the world still don't protect against i++, function calls (with side effects, preferably)... so argument reusing in macros is always a problem (there's no syntax to declare & assign a temp variable either). Prefer inlined functions in that case.

Macro argument with different results [duplicate]

This question already has answers here:
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 7 years ago.
So I have been having this rather 'unyielding' problem with a macro call in my C program.
The macro I used was :
#define MACRO(X) X*X
And the problem is when I do this
printf("%d",MACRO(3));
it displays 9 as a result (which is correct). Only when I pass 3 as 2+1 as follows :
printf("%d",MACRO(2+1));
It strangely displays an outcome of 5.
Can anyone please have me informed why?
printf("%d",MACRO(2+1));
After preprocessing it will be become
printf("%d",2+1*2+1);
Since multiplication has high precidenece over addition, it will print 5;
To resolve the issue you have to define your macro as follows
#define MACRO(X) (X)*(X)
You need to define your macro like so
#define MACRO(X) (X)*(X)
A macro is just a text expansion, so your following code is being interpreted like this...
printf("%d", 2+1*2+1);
And following order of operations you can see how you get 5 as your result.

C: Ternary operator giving error when leaving one expression empty [duplicate]

This question already has answers here:
Why would you use the ternary operator without assigning a value for the "true" condition (x = x ?: 1)
(7 answers)
Closed 10 years ago.
I am attempting to use some code utilising a ternary statement in order to run a specific part, however, when I do this, I get the warning:
expression result unused
And the code in that particular section does not run.
The code in this case is:
i != a ?: printf("|%*s\\\n", i, "");
Why would that be? According to here, this form of the ternary operator, in which there is no alternative for the case, should function, however, it simple skips over it here. Any help is appreciated.
Your code is equivalent to
(i != a) ? (i != a) : printf(...);
Note that you won't end up using the i != a result, hence the warning. Best to write this as an if statement:
if(i==a) printf(...);

Why i am not getting the expected output in the following c programme? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does “#define STR(a) #a” do?
Macros evaluation in c programming language
#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int main()
{
printf("%s\n",h(f(1,2)));
printf("%s\n",g(f(1,2)));
return 0;
}
I was expecting the output to be same for both the printf. But what I am getting is different(given below)
12
f(1,2)
can someone explain what is the reason and why is it happening in detail?
I extended your program with an additional line
printf("%d\n",f(1,2));
which, in turn, results into
printf("%d\n",12);
(called with gcc -E).
Your two lines result into
printf("%s\n","12");
printf("%s\n","f(1,2)");
What happens here?
f(1,2) is clear - 1 and 2 just get sticked together.
g(something) just reproduces something as a string, without treating it specially -> "f(1,2)".
h(something), in turn, lets the result of g(something) expand.
C standard states that macro arguments aren't expanded if they are stringified or concatenated. That is why g(YOUR_MACRO) YOUR_MACRO isn't expanded. However in h(YOUR_MACRO) case - h() does stringification indirectly and so it complies with C macro arguments expansion rules and is expanded further.
First one:
printf("%s\n",h(f(1,2)));
becomes:
g(12)
which in turn becomes
"12"
Second one:
printf("%s\n",g(f(1,2)));
becomes
"f(1,2)"
since # converts the argument to a string parameter.

Resources