1. Relationship between printf and fprintf
1 printf ()
# Include <stdio. h>
Int printf (const char * format ,...);
The printf () function prints the output to stdout (standard output) and other parameters according to the format given by format. For the output format
% C characters
% D signed integer
% I signed integer
% E scientific notation, in lower case "e"
% E scientific notation, in uppercase "e"
% F floating point number
% G use a shorter value in % E or % F
% G use a shorter value in % E or % F
% O octal
% S a string of characters // The following parameter is a string pointer
% U unsigned integer
% X unsigned hexadecimal number, with lowercase letters
% X unsigned hexadecimal number, with uppercase letters
% P a pointer
The % N parameter should be a pointer to an integer.
Stdout is the default output device of printf, because three files are opened by default at the beginning of a process. The three files are stdin, stderr, and stdout.
The return value of printf () is the number of printed characters. If an error occurs, a negative value is returned.
2 fprintf ()
# Include <stdio. h>
Int fprintf (File * stream, const char * format ,...);
The fprintf () function sends information (parameters) to the file specified by stream (Stream) based on the specified format. Therefore, fprintf () can output information to the specified file. For example
Char name [20] = "Mary ";
File * out;
Out = fopen ("output.txt", "W ");
If (OUT! = NULL)
Fprintf (Out, "Hello % s \ n", name );
The output format parameters are the same as those of printf.
Fprintf () works like printf (). The returned value of fprintf () is the number of characters in the output. If an error occurs, a negative value is returned.
In some cases, printf (...) = fprintf (stdout ,...).
Errno global variable
To prevent confusion with normal return values, the system does not directly return error codes, but puts the error codes in a global variable named errno. If a system call fails, you can read the errno value to confirm the problem. Therefore, you can use perror (const char * format); to print the error information, and the system will print its corresponding error return code. the error messages represented by different errno values are defined in errno. h, you can view them by running the "Man 3 errno" command. It should be noted that the errno value is set only when a function error occurs. If the function does not have an error, the errno value is not defined and is not set to 0. In addition, it is best to store the value of errno into another variable before processing errno, because during error handling, the value of errno is changed even when a function like printf () fails.
From:Http://dev.firnow.com/course/6_system/linux/Linuxjs/200871/129591.html
Www.firnow.com time: 2008-07-01 Author: anonymous name