twitter
    Find out what I'm doing, Follow Me :)

Now To Read In Some Integers

Load and display the file named intin.c for an example of reading in some formatted data.The structure of this program is very similar to the last three except that we define an "int" type variable and loop until the variable some how acquires the value of 100.

#include "/sys/stdio.h"

main( )
{
int valin;
printf("Input a number from 0 to 2 billion, stop with 100.\n");
do {
scanf("%d",&valin); /* read a single integer value in */
printf("The value is %d\n",valin);
} while (valin != 100);
printf("End of program\n");
}



Instead of reading in a character at a time, as we have in the last three files, we read in an entire integer value with one call using the function named "scanf". This function is very similar to the "printf" that you have been using for quite some time by now except that it is used for input instead of output. Examine the line with the "scanf" and you will notice that it does not ask for the variable "valin" directly, but gives the address of the variable since it expects to have a value returned for the function. Recall that a function must have the address of a variable in order to return the value to the calling program. Failing to supply a pointer in the "scanf" function is probably the most common problem encountered in using this function.

The function "scanf" scans the input line until it finds the first data field. It ignores leading blanks and in this case, it reads integer characters until it finds a blank or an invalid decimal character, at which time it stops reading and returns the value. Remembering our discussion above about the way the 1616/OS input buffer works, it should be clear that nothing is actually acted on until a complete line is entered and it is terminated by
a carriage return. At this time, the buffer reading is input, and our program will search across the line reading all integer values it can find until the line is completely scanned. This is because we are in a loop and we tell it to find a value, print it, find another, print it, etc. If you enter several values on one line, it will read each one in succession and display the values.

Entering the value to 100 will cause the program to terminate, and the increased responsibility you must assume using C rather than a higher level language such as Pascal, Modula-2, etc. The above paragraph is true for the HiTechC compiler for the Applix, which uses 32 bit integers.
MS-DOS compilers typically use an integer value of 16 bits, and thus accept input values only up to 32767. If your compiler is different, the same principles will be true but at different limits than those given above.
Compile and run this program, entering several numbers on a line to see the results, and with varying numbers of blanks between the numbers. Try entering numbers that are too big to see what happens, and finally enter some invalid characters to see what the system does with non-decimal characters.