Skip to main content

Section 16.4 Practice: Conditional Compilation

Check Your Understanding Check Your Understanding

1.

What is the output of the following code?
#define DEBUG 1
#include <stdio.h>
int main(void)
{
  int N, i, sum;
  i = 1;
  #if (DEBUG >=2)
    printf("It's a beautiful day! ");
  #elif (DEBUG >=1)
    printf("It's sunny! ");
  #elif (DEBUG >=0)
    printf("Not too bad! ");
  #else
    printf("Huh...! ");
  #endif
}
  • It’s sunny!
  • Correct
  • Huh...!
  • Not quite. Try again!
  • Not too bad!
  • Not quite. Try again!
  • It’s sunny! Not too bad!
  • Not quite. Try again!
  • It’s a beautiful day!
  • Not quite. Try again!
  • It’s a beautiful day! It’s sunny!
  • Not quite. Try again!
  • Not too bad! huh...!
  • Not quite. Try again!

2.

Please correct the following loop, which is supposed to add up the numbers from 1 to n:

Compile and run the code below in your own C environment.

#include <stdio.h>

int main(void) {
   int i, n, sum;

   printf("Please enter a positive integer: ");
   scanf("%d", &n);
    sum = 0;

   for(i=1; i<=n; i++);
          sum += i;
          
    printf("The sum from 1 to %d equals %d.", n, sum);
   
    return(0);
}
When your program performs correctly you’ll be given a keyword to enter in Canvas.

3.

Please correct the following loop, which is supposed to add up the numbers from 1 to n:

Compile and run the code below in your own C environment.

#include <stdio.h>

int main(void) {
   int i, n;
   int sum = -1;

   printf("Please enter a positive integer: ");
   scanf("%d", &n);
   printf("You entered %d.\n", n);

   for(i=1; i<=n; i++)
          sum += i;
          
    printf("The sum from 1 to %d equals %d.", n, sum);
    
    return(0);
}
When your program performs correctly you’ll be given a keyword to enter in Canvas.