[previous_group] [previous] [up] [next] [next_group]

Reading Data at Run Time

After several tries, we finally got the simplified stock program to solve our problem to compile and run correctly in the file stock1.fixed.c.

Can we improve it?

Consider what would happen if we bought or sold some of our in XYZ Corporation, or if the price on the stock changed.

We would need to edit the file to change the assignment statements in the program, recompile it and run it again each time. In a volitile stock market, this could be tedious.

It might be better to write a more general program that would work for any data values, so we can compile it once and allow the user to enter the data at run time.

To make this change, we go back to step 3 and modify the algorithm to get the data values instead of setting them. The algorithm would become:

        Get the number of shares
	Get the selling price
	Print the data value
	Compute the portfolio value
	Print the result

How do we implement this in C?

The C library provides a built-in function to do this, called scanf(). We give scanf() a format string similar to printf(), telling it the type of conversion to do on the value read, and the variable we want the value to be stored in. We should also prompt the user to tell them that they should enter data here, and what data to enter. We can write the code as:
        printf("Enter number of shares: ");
        scanf("%d",shares);

One thing to note is that the printf() format string does not have a newline ('\n') character at the end. This is so that the curser will stay on the same line as the prompt.

But we said that printf() is "buffered" - that the output does not appear on the screen until a complete line (indicated by '\n') is output. In this case, since we next use scanf() to read data, scanf() will flush the contents of the buffer to the screen to make it available for its use.

Unfortunately, we still have a problem. If we added this to the program and ran it, when we typed a value for the number of shares at run time, we would get the the following message:

Enter number of shares: 57
Segmentation fault

This is a run time error, the program "crashed".

What happened?

When we call scanf() as:
        scanf("%d",shares);

We are passing the format string and the VALUE of the variable shares to scanf(). What scanf() needs to do is read an integer and put that value into the cell corresponding to shares. It needs to know where shares is, not its value.

C provides an operator to find the location of a variable, the address of operator &. So we really need to call scanf() as:

        scanf("%d",&shares);

The code for the modified program is in stock2.c.

A few other things to consider with scanf() input:

scanf() is buffered
the input data is not read until a full buffer has been entered, indicated by a return key (a newline).
Data for scanf() is "free form"
any leading whitespace (spaces, tabs or newlines) are ignored by scanf(). The first data item found in the input will be converted and stored in the address provided. Likewise, scanf() will not return until it has converted the requested number of data items.
Additional data in the buffer is not affected by scanf()
scanf() will read the data items requested from the buffer. If there is additional data still in the buffer, it will remain until read by subsequent calls to scanf().

How does this code run?


[up] to Overview.

[next]