scanf, fscanf, and sscanf provides formatted input (scanning or reading). For
example, the statement
fscanf(fpimp,"%f",&bn);
reads from an arbitrary file pointed by the file pointer fpimp to a variable of address
&bn, and %f indicates the number is floating-point data. In addition, the formatted I/O
functions also recognize %d for decimal integers, %x for hexadecimals, %c for characters,
and %s for character strings.
The function fwrite writes binary data. That is, fwrite writes blocks of data
without formatting to a file that has been opened in binary mode. The data written may
be integers or floating-point numbers in binary form (they may represent digital sounds
or pictures). Conversely, the function fread is used for reading unformatted binary
data from a file. The function fread requires four arguments. For example, the
statement
fread(&xn, sizeof(float), 1, fpin);
reads 1 item, each item of size float data type, into the array xn from the file
pointed to by the file pointer fpin. The fread function returns the number of items
successfully read. The operator sizeof(object)has a value that is the amount of
storage required by an object. The values of sizeof for different data types may vary
from system to system. For example, in a workstation, the value of sizeof(int)is 4
(bytes), whereas on fixed-point DSP systems, the value of sizeof(int)is typically 2
(bytes).
The function fwrite expects four arguments. For example, the statement
fwrite(&yn, sizeof(float), 1, fpout);
writes the binary form float array yn of size sizeof(float)to the file pointed to by
the pointer fpout. The difference between using fwrite to write a floating-point
number to a file and using fprintf with the %f format descriptor is that fwrite
writes the value in binary format using 4 bytes, whereas fprintf writes the value as
ASCII text which usually need more than 4 bytes.
C.4 Control Structures and Loops
C language has a complete set of program control features that allow conditional
execution or repetition of statements in loops based on the result of an expression.
C.4.1 Control Structures
The C language provides two basic methods for executing a statement or series state-
ments conditionally: the if statement and the switch-case statement. The if
statement allows us to test a condition and then execute statements based on whether
the given condition is true or false. The if statement has the following general
format:
CONTROL STRUCTURES AND LOOPS
481