I/O is one of C's strengths. It is very close to the system so it is easy to interact with details of the environment.
Stream versus line I/O
Fortran does line i/o, due to its roots with input on Hollerith cards and output on line printers. This is a fundamental conceptual difference in the two languages. In Fortran some results might be computed and printed as follows:
This cannot be done in C since an implicit array write cannot be done. The natural way to do it in C is beginning writing to the stream, and put a new line on it at the end.
printf(" the results are ");
Printing n numbers per line
This is done with the remainder operator %. The expression a%b gives the remainder of a/b and is only zero (and so false) when a is divisible by b. To throw a new line every 8 numbers you would add the following code to the loop above:
if( !(i%8) ) printf("\n");
Standard I/O streams and buffered output
stdin - standard input stream (fort.5 in Fortran)
stdout - standard output stream (fort.6 in Fortran)
stderr - standard error output (nothing similar in Fortran)
stdout is buffered output, so that the results are saved in a buffer until it is full, and the write commit occurs. If the code crashes before this happens the output that is still in the buffer will disappear.
stderr is not buffered, and output to here will certainly appear, but will be very slow because of the nature of unbuffered output.
A write commit can be forced by calling fflush on the unit.
structure of output routines
This does a formatted print to stdout. The first argument is a string that
includes any text, and also formatting descriptors. Descriptors tell how numbers or
strings should be output, they begin with '%', and are within double quotes. Th
string should end with the new line character ('\n') if a carriage return is needed.
The (optional) quantities to be printed are in the fields after the string.
The same as printf, but the first parameter is an additional parameter, the pointer to the string (sprintf) or output stream (fprintf) where the output goes.
These specify how numbers or strings are to be converted, and are fairly analogous to Fortran. C converts float to double when passing by value to printf, so printing a float with a double specification will work.
Strings %s for best fit. %80s will be exactly 80 characters long, right justified with space fill if the string has less than 80 characters.
Character %c for a single character. This does not have a precision.
Integer %i for something declared int, and %li for a long integer (although %i will probably still work).
Exponential notation %e for double variables. %9.2e by analogy with Fortran.
Best representation. %g will print a double as either a float or exponential format depending on which is shorter.
Floating %f will produce a number like 5.6.
opening, writing to, closing files
fopen, fprintf, fclose
/* declare the pointers to the file - basically the io units */
Reading and printing entire file
while( fgets( chLine , sizeof(chLine) , stdin )!=NULL )