The following sample program illustrates how you can accumulate several variable values. When using the program, you would separate each typed integer by a space, tab, or carriage return.
#includemain ( ) { int average, size_one, size_two, size_three; printf ("Please type three trade sizes.\n"); scanf ("%i", &size_one); scanf ("%i", &size_two); scanf ("%i", &size_three); average = (size_one + size_two + size_three) / 3; printf ("The average of the trade sizes is %i.\n", average); }
When you execute the program, presuming you have named it
average_size
, you could witness the following:
average_size <-- You type this Please type three trade sizes. <-- The program types this 600 100 200 <-- You type this The average of the trade sizes is 300. <-- The program types this
When you use the average_size
program, you can use spaces, tabs, or
carriage returns to separate the integers and to mark the end of the final
integer.
Note that, if you are using the Unix operating system, the characters
that you type accumulate temporarily in an input buffer before
delivery to your program. Delivery occurs only when you type a carriage
return. Accordingly, the average_size
program lies inert until you
not only type three integers, but also supply a carriage return following
the third one.
Spaces separate integers | | v v 600 100 200 <-- Carriage return marks the end of a line of input and delivers the accumulated characters to your program