Home
Username:
Password:
Advanced C Programming Tutorials

Preprocessor / Macro Follies




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

Macro definitions are simple string replacements. When you call a macro with or without arguments, the substitution takes place. Then a check is made on the entire file to determine whether another substitution should be made on the result. The process repeats until no substitution takes place. So it's possible to use a series of macros and build the results up from multiple substitutions. In this example the third line is expanded first to this and the second substitution then results in this. Now, that's confusing. You may need to study that a bit to see what happened. The substituted strings don't require balance parentheses so you can do anything you want. Look at this. You can invoke the macro this way. After the macro substitution takes place, the code looks like this. You can piece things together like this but I suggest that you don't. You won't be the only one you confuse. You need to be generous in the use of parentheses because you can have precedence problems. This macro is designed to perform integer division and round up instead of the normal operation which is to round down. But it could fail. Suppose you were to use it like this. This is intended to be a bit-wise anding of A and B and have its results divided by M. But it doesn't work that way. Because of operator precedence this expression is equivalent to this. The adding and subtracting is done first and then the anding. That will give a very different answer. You can fix it with parentheses like this. Then you can use any expression you wish in the arguments. The previous example would expand to this. This will do exactly what was originally intended. If you are generous enough with parentheses, you can know exactly what code gets generated. In the C programming language, semicolons are terminators not separators. Usually that works out well but there is at least one place that it can fool you. This macro definition uses backslashes at the end of lines so it can span several lines. It also uses braces around the statements so it can be used as a single statement. So far so good. But there's a sneaky little problem. It's true that the macro expands to a single statement but braces don't need a semicolon to be terminated so the semicolon in the code adds a null statement. In this case that causes two statements to appear between the if and the else and that's invalid code. But there is a way to write the macro so the semicolon won't produce that extra statement. The macro written this way will require a terminating semicolon. A do while loop is always executed at least once so the code will execute once. The optimizer will throw away the Do Loop code because it is invariate. So the end result is the same. One mistake that's easy to make is the duplication of side effects. This common macro is used to find the minimum of two values. As long as it's used with variables or constants it works fine. But if you use it with a function, the function is called twice. It expands to this code and the function may be called twice. If there are side effects to calling the function, the side effects will occur twice or even if there are no side effects, the function call could take a long time to execute and calling it twice could be costly. I don't know of a general cure for this one except for being aware of the way the macro works. The pre-processor has one good safety feature. If you use the name of a macro inside its own definition, it won't expand it. Therefore it won't get caught in a recursive loop. This is an example. If the pre-processor doesn't check for self reference, this expansion would be an infinite loop. But instead this statement expands only once. The result of this expression is the number 15 being added to the variable. This works but you should avoid doing things like this for the purpose of keeping your code.

Tutorial Information

Course: Advanced C Programming
Author: Arthur Griffith
SKU: 33965
ISBN: 1-935320-24-6
Release Date: 2009-01-30
Duration: 5.5 hrs / 82 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available