Lab 4
9/18 @ 11:59 PM
Not accepted late
The Lab Assignment

This lab will exercise your basic familiarity with the C Preprocessor. For this lab, you will only need to submit a single text file that contains answers to all of the below questions:

  • 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;
    } 
    
Submission Instructions

Create a tarball consisting of a single directory with all of your work in it, such as code or written answers. If you have any auxiliary files (READMEs, Makefiles, etc.), be sure to include those as well. Name your work directory l4 and name the tarball l4.tgz. Upload your tarball to the L4 Canvas assignment.

Labs will be graded under one of following criteria:

  1. Effort: You will automatically be given full credit for the labwork if the GTA determines that your participation in the lab was meaningful - i.e. you attended the lab session and used the time to make a good-faith attempt to complete the work. It is the sole discretion of the GTA to determine if you put in sufficient effort. Even if you expect an effort-based grade, you should turn in your (possibly incomplete) work.
  2. Correctness: If you do not participant meaningfully in lab (i.e. you do not attend the lab session), your grade will be assessed based on the correctness of your lab submission.

Advice: How to Approach Labs

The two-criteria grading scheme above is designed to avoid wasting your time. You should not feel obligated to attend the lab, and in fact should only do so if you want help from the GTA on the labwork assignment or whatever project is currently in progress. Here's a handy flowchart for how I suggest you approach lab: