Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the side effect of the following macro in C ? Embedded C

Tags:

c

macros

#define MIN(A,B) ((A) <=  (B) ? (A) : (B))

this is the macro , I was asked what's the side effect if I used the following :

least = MIN(*p++, b);

Note: This was embedded c question

like image 262
xsari3x Avatar asked Sep 04 '11 12:09

xsari3x


People also ask

What is macro in embedded C?

The macro in C language is known as the piece of code which can be replaced by the macro value. The macro is defined with the help of #define preprocessor directive and the macro doesn't end with a semicolon(;).

What is side effect of a variable in C?

The formal definition of side effect in the C language (C17 5.1.2.3/2) is rather: Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment.

What is the use of macros in C?

In C, the macro is used to define any constant value or any variable with its value in the entire program that will be replaced by this macro name, where macro contains the set of code that will be called when the macro name is used in the program.

What is the data type of macro in C?

There are two kinds of macros. They differ mostly in what they look like when they are used. Object-like macros resemble data objects when used, function-like macros resemble function calls. You may define any valid identifier as a macro, even if it is a C keyword.


1 Answers

It evaluates p++ twice. Also, since the first evaluation changes p, the second time around it will point to a different element. So the returned value will be *(initialp + 1) or b.

You should try it yourself.

like image 84
cnicutar Avatar answered Sep 22 '22 17:09

cnicutar