-3

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__))
istiaq2379
  • 113
  • 3
  • 13
  • 2
    `?:` is the conditional operator. Somebody refers to it as the ternary operator. – Yu Hao Sep 09 '14 at 11:24
  • http://en.wikipedia.org/wiki/%3F:#C – Michael Sep 09 '14 at 11:24
  • If you use assert_param(a), this macro will do nothing if a is non-zero, but print an error, if it is. The a ? e1 : e2; is an alternative form of if (a){ e1; }else{ e2; } A macro like this is frequently used to assert a pointer is not a null-pointer (which is zero-valued). It may also be of the form (!!a) ? e1 : e2; to normalize a. – midor Sep 09 '14 at 11:51
  • is it too hard to search for C operators – phuclv Sep 09 '14 at 12:15

2 Answers2

1

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.

Sarwan
  • 595
  • 3
  • 8
  • 21
1

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

antonpuz
  • 3,256
  • 4
  • 25
  • 48