[previous] 0880 [next] Using # and ## operators in the same macro definition.
Macro Definition REFERENCE - ISO:C90-6.8.3.2 The # Operator - Semantics

# and ## operators are both present in this macro definition

The order in which # and ## operators are evaluated during preprocessing is unspecified. In most situations this will be irrelevant, but it may be wise to avoid using more than one of these operators within the same macro because a macro which works with one compiler may fail with another.

In the following example, the macro MAKE_WSL has been written with the intention of performing 2 operations:
  1. First its parameter is to be converted to a normal string literal using the # operator.
  2. Secondly the normal string literal is to be converted to a wide string literal by using the ## operator to prepend an 'L'.

The macro will only work if these 2 operations are performed in this order, but unfortunately this sequence cannot be guaranteed. If the operations are performed in the reverse order, undefined behaviour will result.


/*PRQA S 878,2017,3211,3408,3410,3429,3453,3602 ++*/

#include <stddef.h>

#define MAKE_WSL(X) L ## #X                     /* Message 0880  */

const wchar_t * pws = MAKE_WSL(abc);            /* L"abc" or ??? */


MISRA-C:2004 Rules applicable to message 0880:

Rule  19.12  (Required) There shall be at most one occurrence of the # or ## preprocessor operators in a single macro definition.


See also:

QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents