Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C preprocessor
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Limitations== === Text substitution limitations === Text substitution has a relatively high risk of causing a [[software bug]] as compared to other programming constructs.<ref>{{cite web |title = The power of ten - Rules for developing safety critical code |url = http://spinroot.com/gerard/pdf/P10.pdf |author = Gerard J. Holzmann |page = 4 |work = safety of macros }}</ref><ref name="processor">{{cite journal |title = An Empirical Analysis of C Preprocessor Use |author1=Michael D. Ernst |author2=[[Greg J. Badros]] |author3=David Notkin |url = http://dl.acm.org/citation.cfm?id=631305 |date = December 2002 |journal = C Preprocessor, Macros |volume=28 |issue=12 |pages=1146β1170 |doi=10.1109/TSE.2002.1158288 |url-access=subscription }}</ref> ;Hidden multiple evaluation Consider the common definition of a ''max'' macro: <syntaxhighlight lang=C> #define max(a,b) (((a) > (b)) ? (a) : (b)) </syntaxhighlight> The expressions represented by ''a'' and ''b'' are both evaluated two times due to macro expansion, but this aspect is not obvious in the code where the macro is referenced. If the actual expressions have constant value, then multiple evaluation is not problematic from a logic standpoint even though it can affect runtime performance. But if an expression evaluates to a different value on subsequent evaluation, then the result may be unexpected. For example, given <code>int i = 1; j = 2;</code>, the result of <code>max(i,j)</code> is 2. If ''a'' and ''b'' were only evaluated once, the result of <code>max(i++,j++)</code> would be the same, but with double evaluation the result is 3. ;Hidden order of operation Failure to bracket arguments can lead to unexpected results. For example, a macro to double a value might be written as: <syntaxhighlight lang=C> #define double(x) 2 * x </syntaxhighlight> But <code>double(1 + 2)</code> expands to <code>2 * 1 + 2</code> which due to order of operations, evaluates to 4 when the expected is 6. To mitigate this problem, a macro should bracket all expressions and substitution variables: <syntaxhighlight lang=C> #define double(x) (2 * (x)) </syntaxhighlight> ===Not general purpose=== The C preprocessor is not [[Turing completeness|Turing-complete]], but comes close. Recursive computations can be specified, but with a fixed upper bound on the amount of recursion performed.<ref>{{cite web |url=https://stackoverflow.com/a/10526117 |title=Is the C99 preprocessor Turing complete? |url-status=live |archive-url=https://web.archive.org/web/20160424212425/https://stackoverflow.com/questions/3136686/is-the-c99-preprocessor-turing-complete/10526117 |archive-date=2016-04-24}}</ref> However, the C preprocessor is not designed to be, nor does it perform well as, a general-purpose programming language. As the C preprocessor does not have features of some other preprocessors, such as recursive macros, selective expansion according to quoting, and string evaluation in conditionals, it is very limited in comparison to a more general macro processor such as [[m4 (computer language)|m4]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)