CSC161 2011S Imperative Problem Solving

Reading: Macros

Reading: K&R 4.11

Please submit a question by 9 p.m. on Thursday, 10 March 2011.


Conditioals

Can you explain what #if, #endif, #elif, and #else do? I've noticed the #endif thing used in header files... what is its purpose?"

You may recall that when I gave the steps of compilation talk, I said that the first step of compilation is preprocessing. The #if stuff is a conditional that is evaluated at preprocessing time.

When do I use #if? And where are the located in the code when I use them? Would #endif appear at the end before the whole code starts or after closing main?

It can appear in multiple places in the code. There's a weird standard about header files in which you surround the whole header file with and #ifdef (see the header files I've written). However, it also gets used in many other places.


#undef

When do we use #undef?

If getchar and putchar are defined as macros in <stdio.h>, why would you want to #undef to declare it as a function?

What is a reason to want to undefine something? Is it inefficient to just stop using a defined constant when we don't need it anymore?


Miscellaneous

Can you give some examples of what # and ## do when they are included in the replacement text of a macro?

There's an example in today's lab.

Why is #define square(x) x*x wrong?

There's an example in today's lab.

What kind of bug does #include avoid? It says that the preferred way to tie the declarations together for a large program is with #include meaning that there are other ways. Do the other methods not avoid the "nasty kind of bug" (what is this bug, too)?

Can you re-explain what ## does (as seen on pages 90 and 91)?

I have noticed that in our most recent assignment you make the preprocessor call

#ifndef __MYIO_H_

What is the purpose of the underscores as they do not mention anything about this in the reading?

I'm confused about how macro substitution is used with conditionals. If you define HDR does it create a HDR.h?

No.

Can macros contain helper functions defined in the same source code or in an included library?

What is the point of the code in section 4.11.3 that first checks if a header is included, and, if not, includes it? Does including a header twice cause a negative side-effect?

If you define a macro that needs an 'internal' variable (i.e. for swapping two values, you need a temporary storage variable), where should you define/declare that variable? Since condition inclusion (such as #if) do not use parentheses around the test, does that mean they can only be given one expression to test?

The use of #if for SYSTEM on page 91. I don't really understand what it's doing, could you please explain it?

Can you go over macro substitution and conditional inclusion. I didn't get them. I don't really understand what a macro is. Is it just a define statement that needs to evaluate an expression to determine its value? How can you have an expression with variables in a define statement? The variables can change, so at what point does a define with an expression determine its value? Won't the variables not be initialized yet when the compiler reads the define?


Without classes, how are disparate sets of code assembled at compilation?

I'm not sure what that has to do with this reading. But it's probably easier without classes. Anyway, each set of code has a set of names with associated info (e.g., square names a function which takes a double as an input and produces a double as output). The linker (the last stage in compilation) associates a memory address with each name and then goes through the code and fills in the memory address everywhere the name is used.

Currently, how we have used #define is like #define 'what is defined' and 'what it is'..or something very similar to that. so we can use #define as in like #define 'what is defined' and then 'what test to make to get define thing' ?

I'm sorry. I can't parse this. (I'm sorrier that some of you probably feel the same way about some of the things I write.)

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri Mar 11 09:38:46 2011.
The source to the document was last modified on Fri Mar 11 09:38:44 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Readings/macros.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu