Learn How to Program in C

by
Dr. Kristin Switala

Introduction

Directives

Variables

Conditionals

Loops

Arrays

Pointers

Strings

Functions

Structures


Site Map

Review: Variables (b)

Instructions: Think about each question and write the answer in your notebook.


General Questions

1. What format specifier do we use for floating point variables?

2. What do we use to specify two decimal places (for money)?

3. Which preprocessor directive is used to set constants?

4. What is a constant?


Floating Point Variables

1. Fix the errors in this C source code. There are 2 errors.

#include <stdio.h>
void main( )
{
int speed = 10.2f
printf("\nShackleton's ice ship, the Endurance, could steam at a speed of %f knots.", speed);
}

2. Fix the errors in this C source code. There is 1 error.

#include <stdio.h>
void main( )
{
float time = 5.5f;
printf("\nAfter the Endurance was crushed, Shackleton kept his men alive on the ice for %d months.", time);
}


Working with Money

1. Fix the errors in this C source code. There are 2 errors.

#include <stdio.h>
void main( )
{
float price = 12.95f;
float tax = 0.07f
float total = 0.0f;
total = price + (price * tax);
printf("\nThank you for ordering Sara Wheeler's book, Terra Incognita: Travels in Antarctica.");
printf("\nWith tax, your total is $%f.", total);
}

2. Fix the errors in this C source code. There are five errors.

#include <stdio.h>
{
float price = 29.95f;
float tax = 0.0825f;
float total = 0.0f;
total = price + (price * tax);
printf("\nTo see Frank Hurley's stunning photos from the Endurance expedition, read:");
printf("\nThe Endurance: Shackleton's Legendary Antarctic Expedition, by Caroline Alexander.");
printf("/nHardback: $%.2f. With tax: $%f.");
}


Constants

1. Fix the errors in this C source code. There are 4 errors.

#include <stdio.h>
define HURLEY = 1919
void main( )
{
int year;
int time;
printf("\nWhat year is it? ");
scanf("%d", &year);
time = year - hurley;
printf("\nSouth, Hurley's silent film about the Endurance, is %f years old.", time);
}

Copyright
© 2001
Kristin Switala
Hosted by www.Geocities.ws

1