ARM Technical Support Knowledge Articles

INLINE MACRO FUNCTIONS

Applies to: C251 C Compiler

Answer

QUESTION

I would like to create a function that can be written as a separate routine and appear in the code to be a separate function, but in compiling will be inserted directly in the code rather than called by a CALL instruction. Other C compilers often support macros or "inline" functions that accomplish this. Is there any way to do this with the Keil C251 Compiler?

ANSWER

Yes. The following example program shows how you can use #defines in C to insert C code inline. Note that parameters (a and b) are passed to these macros.

#define func_part_1(a,b)   a ^= b;  b ^= a;  a ^= b;
#define func_part_2(a,b)   if (a)    b = 0;  if (b)    a = 1;

void func (unsigned int a, unsigned int b)
{
func_part_1 (a,b);
func_part_2 (a,b);
}

Also note that the backslash character ('') is required to continue the macro on the next line.

Article last edited on: 2004-05-16 18:28:09

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential