Input and output are not part of the C language itself。
一、Standard Input and Output
a、The symbolic constant EOF is defined in <stdio.h>. The value is typically -1, bus tests should be written in terms of EOF so as to
a、The symbolic constant EOF is defined in <stdio.h>. The value is typically -1, bus tests should be written in terms of EOF so as to
be independent of the specific value.
b、"functions" like getchar and putchar in <stdio.h> and tolower in <ctype.h> are often macros, thus avoiding the overhead of a function call per character.
二、Formatted Output - printf
a、printf converts, formats, and prints its arguments on the standard output , It returns the number of characters printed
b、printf uses its first argument to decide how many arguments follow and what their type is.
a、printf converts, formats, and prints its arguments on the standard output , It returns the number of characters printed
b、printf uses its first argument to decide how many arguments follow and what their type is.
三、Variable-length Argument Lists
a、The standard header <stdarg.h> contains a set of macro definitions that define how to step through an argument list.
b、The implementation of this header will vary from machine to machine, but the interface it presents is uniform.
c、structure and operate
/* typedef struct {
char *a0; /* pointer to first homed integer argument */
int offset; /* byte offset of next parameter */
} va_list;
*/
typedef char *va_list;
#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )
#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
#define va_end(ap) ( ap = (va_list)0 )
四、Formatted Input - Scanf
a、scanf reads characters from the standard input, interprets them according to the specification in format, and stores the results
a、The standard header <stdarg.h> contains a set of macro definitions that define how to step through an argument list.
b、The implementation of this header will vary from machine to machine, but the interface it presents is uniform.
c、structure and operate
/* typedef struct {
char *a0; /* pointer to first homed integer argument */
int offset; /* byte offset of next parameter */
} va_list;
*/
typedef char *va_list;
#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )
#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
#define va_end(ap) ( ap = (va_list)0 )
四、Formatted Input - Scanf
a、scanf reads characters from the standard input, interprets them according to the specification in format, and stores the results
through the remaining arguments.
b、the other arguments, each of which must be a pointer, indicate where the corresponding converted input should be stored.
c、There is also a function sscanf that reads from a string instead of the standard input:
int sscanf(char *string, char *format, arg1, arg2, ...)
d、The function ferror returns non-zero if an error occurred on the stream fp.
五、File Access
a、This pointer, called the file pointer, points to a structure that contains information about the file
a、This pointer, called the file pointer, points to a structure that contains information about the file
b、The call to fopen in a program is
fp = fopen(name, mode);
The second argument is the mode, also a character string, which indicates how one intends to use the file. Allowable modes
include read ("r"), write ("w"), and append ("a"). If there is any error, fopen will return NULL.
c、the operating system environment is responsible for opening three files and providing pointers for them. These files are the
standard input, the standard output, and the standard error; the corresponding file pointers are called stdin, stdout, and stderr
d、Normally stdin is connected to the keyboard and stdout and stderr are connected to the screen, but stdin and stdout may be
redirected to files or pipes as described
e、fclose is the inverse of fopen, it breaks the connection between the file pointer and the external name that was established by
fopen, freeing the file pointer for another file.
f、fclose is called automatically for each open file when a program terminates normally.
六、Error Handling - Stderr and Exit
a、Output written on stderr normally appears on the screen even if the standard output is redirected.
b、The argument of exit is available to whatever process called this one
c、exit calls fclose for each open output file, to flush out any buffered output.
d、Within main, return expr is equivalent to exit(expr). exit has the advantage that it can be called from other functions
a、Output written on stderr normally appears on the screen even if the standard output is redirected.
b、The argument of exit is available to whatever process called this one
c、exit calls fclose for each open output file, to flush out any buffered output.
d、Within main, return expr is equivalent to exit(expr). exit has the advantage that it can be called from other functions
七、Line Input and Output
a、fgets reads the next input line (including the newline) from file fp into the character array line; at most maxline-1 characters will be
a、fgets reads the next input line (including the newline) from file fp into the character array line; at most maxline-1 characters will be
read. The resulting line is terminated with '\0'.
b、Confusingly, gets deletes the terminating '\n', and puts adds it.
八、Storage Management
The right way is to save whatever is needed before freeing: (list)
for (p = head; p != NULL; p = q)
{
q = p->next;
free(p);
}
The right way is to save whatever is needed before freeing: (list)
for (p = head; p != NULL; p = q)
{
q = p->next;
free(p);
}