Skip to main content
Logo image

Dive Into Systems: Exercises

Section 1.2 Input/Output (printf and scanf)

Exercise 1.2.1. Elements of the printf function.

Exercise 1.2.2. Debug I/O statements.

    What is wrong with the following code? It’s supposed to ask the user to input their age and then read from the keyboard into the variable named age.
    int age;
    
    printf("Enter your age: ");
    scanf("%d", age);
    
  • There is a missing & in front of age.
  • Correct! The second parameter to scanf is an address (pointer).
  • The %d should be %g
  • Incorrect. The type of age is int.
  • The %d should be %c
  • Incorrect. The type of age is int.
  • The printf should be scanf
  • Incorrect.
  • The scanf should be printf
  • Incorrect.
  • The scanf should be printf and the printf should be scanf
  • Incorrect.

Exercise 1.2.3. Parsons Problem: A Program with I/O.

Write a program that prompts the user to input the base and height of a right triangle (both of these should be floats) and then outputs the area of the triangle.