Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
C: Input statements


1. C: Input statements

2. C: Input statements

3. Simple IPO model
input process outputA computer takes input, processes the input, and produces output. Here we look at input in terms of input statements. Input gets input into the computer (from outside the computer/processor).

For now, study the example programs for the labs and/or assignments to see how input can be done. In C, the scanf function is used to get input from the user (from the standard input).

4. Multiple input variables
Input can be read into multiple variables at once (e.g., separated by a space). Here is one way.
   // declarations    int x, y, z;    // input    scanf("%i %i %i", &x, &y, &z);    printf("You entered: %i %i %i", &x, &y, &z);    // process ...

Note: The ampersand "&" is required before each input variable.

5. Input testing
Suppose you wanted to read the month and year as integers from the user. Here is one way.
   // declarations    int month, year;    // input    scanf("%i %i", month, year);    printf("You entered: %i %i %i", &x, &y, &z);    // process ...


6. RIDES
The RIDES system will automatically run your program using the provided input data sets.

If you are not using the RIDES system, then a way to ease the burden of running your program with test data is described below.

7. Testing way
Note: This is for programming without the RIDES system.

It can be tedious to enter test input every time that the program is run. Here is one way to get started.
   int month, year;    month = 3; year = 2019;    // scanf("%i %i", month, year);    // .... process the input

That is, comment out the scanf statement and explicitly set the values for month and year using assignment statements. This is one time when one might want to put multiple assignment statements on one line.

To test with other input values, just change the assignment statements.

8. Before submission
Note: This is for programming without the RIDES system.

Before submission, comment and uncomment as follows. Then test. When it works as desired, submit.
   int month, year;    // month = 3; year = 2018;    scanf("%i %i", month, year);    // .... process the input


9. End of page