Lab 3
This lab is NOT for credit. While there may be some suggested exercises to aid in your comprehension, you should not turn it in (though you should feel free to ask the GTA about it if you like!)
The Lab Assignment

This lab will explore basic details of the C Preprocessor. After learning about the preprocessor, you might consider asking yourself the following questions to test your comprehension

  • What is the #line directive? Give an example where the directive might be useful
  • What are the __LINE__ and __FILE__ macros? Give an example of how they might be useful.
  • Give an example of a preprocessor macro that you define that takes an argument. Typically, the use of such macros is discouraged. Why might that be?
  • What is the command-line option to make g++ print the preprocessed output to standard out?
  • Describe what the # operator does by explaining the output of the following program:
    (Note: The # operator is not described in the video, but it's pretty easy to just compile the below program and figure it out on your own.)
#include <iostream>
#define MYSTERY( x ) #x

int main () {
   int a = 1;
   int b = 2;
   int c = 3;
   std::cout << MYSTERY(a+b*c) << std::endl;

   return 0;
}
  • What is the command-line option to make g++ compile the following program so that it prints "bye" to std::cerr when run?
  • #include <iostream>
    #define GLUE(ARG1,ARG2) std::c##ARG1 << ARG2;
    int main(){
    	#ifdef VERBOSE
    		#if OUTMODE==1
    			GLUE(out,"hello\n")
    		#else
    			GLUE(err,"bye\n")
    		#endif
    	#endif
    return 0;
    }