C Language Input and Output Functions

Source: Internet
Author: User
There are many Input and Output Functions in C language. The standard I/O functions include the following common functions:
Scanf, printf, GETC, putc, getchar, putchar, gets, puts, fgets, fputs, fgetc, fputc, fscanf, fprintf, etc.

Int scanf (const char * format, arg_list)
Scanf obtains the parameter value from the standard input stream. format is the specified parameter format and parameter type, such as scanf ("% s, % d", STR, icount );
It requires that a string similar to "son of bitch, 1000" be input in the standard input stream, andProgramSend "son of bitch" to STR and 1000 to icount.
The Return Value of the scanf function is int, that is, the number of successfully assigned values. In the preceding example, if the function is successfully called, 2 is returned. Therefore, when writing a program
Statement if (scanf ("% s, % d", STR, icount )! = 2) {...} to determine whether the user input is correct.

Int printf (const char * format, arg_list)
Printf mainly outputs formatted strings to the standard output stream. Standard input and output are defined in the stdio. h header file, namely stdin and stdout.
Arg_list can be a variable name or expression, but it will be filled in the format as a value.

Int GETC (File * FP)
GETC is mainly used to read a character from a file. It is often used to determine whether the file is read. The statement is: (CH = GETC (FP ))! = EOF. EOF indicates the end of a file,
Defined in stdio. H, just as exit_success and exit_failure are defined in stdlib. H, the file can also be understood as a stream, so when FP is stdin
GETC (stdin) is equivalent to getchar.

Int putc (int ch, file * FP)
Putc mainly writes the CH character to the file FP. If the FP is stdout, putc is equivalent to putchar.

Int getchar (void)
Getchar reads a character from the standard input stream. The default standard input stream is stdin defined in stdio. h.
The buffer is involved, so it will not run when you press a character program on the screen. Generally, you can press the Enter key on the screen and then press the character before the carriage return.
If the string is placed in the buffer, getchar is a read character in the buffer. You can also specify the termination character in the while loop, as shown in the following statement:
While (C = getchar ())! = '#') This ends.

Int putchar (INT ch)
Putchar (CH) writes the CH character to the stdout of the standard stream.

Char * gets (char * Str)
Gets is mainly used to read strings from the standard input stream and display them back. It exits when it reads the linefeed and saves the linefeed.

Int puts (char * Str)
Puts writes the STR string to the stdout of the standard stream, and adds a line break to the end of the output.

Char * fgets (char * STR, int num, file * FP)
STR is the array pointer of characters to be read, num is the maximum number of characters allowed to be read, and FP is the file pointer. fgets is used to read a row of characters, the number of characters in this row
Not greater than the num-1. Because the fgets function will add an empty character at the end to form a string. In addition, fgets will not omit it after reading the linefeed.

Int fputs (char * STR, file * FP)
Fputs writes STR to FP. fputs. The difference between fputs and puts is that fputs do not add line breaks when printing.

Int fgetc (File * FP)
Fgetc reads a character from the current position of FP.

Int fputc (int ch, file * FP)
Fputc writes ch to the specified position of FP.

Int fscanf (File * FP, char * format ,...)
Fscanf reads data from the file in the specified format and assigns values to the parameter list.

Int fprintf (File * FP, char * format ,...)
Fprintf writes formatted data to a streaming file.

Int scanf (const char * format, arg_list)
Scanf obtains the parameter value from the standard input stream. format is the specified parameter format and parameter type, such as scanf ("% s, % d", STR, icount );
It requires that a string similar to "son of bitch, 1000" be input in the standard input stream, and the program will give "son of bitch" to STR, 1000 to icount.
The Return Value of the scanf function is int, that is, the number of successfully assigned values. In the preceding example, if the function is successfully called, 2 is returned. Therefore, when writing a program
Statement if (scanf ("% s, % d", STR, icount )! = 2) {...} to determine whether the user input is correct.

Int printf (const char * format, arg_list)
Printf mainly outputs formatted strings to the standard output stream. Standard input and output are defined in the stdio. h header file, namely stdin and stdout.
Arg_list can be a variable name or expression, but it will be filled in the format as a value.

Int GETC (File * FP)
GETC is mainly used to read a character from a file. It is often used to determine whether the file is read. The statement is: (CH = GETC (FP ))! = EOF. EOF indicates the end of a file,
Defined in stdio. H, just as exit_success and exit_failure are defined in stdlib. H, the file can also be understood as a stream, so when FP is stdin
GETC (stdin) is equivalent to getchar.

Int putc (int ch, file * FP)
Putc mainly writes the CH character to the file FP. If the FP is stdout, putc is equivalent to putchar.

Int getchar (void)
Getchar reads a character from the standard input stream. The default standard input stream is stdin defined in stdio. h.
The buffer is involved, so it will not run when you press a character program on the screen. Generally, you can press the Enter key on the screen and then press the character before the carriage return.
If the string is placed in the buffer, getchar is a read character in the buffer. You can also specify the termination character in the while loop, as shown in the following statement:
While (C = getchar ())! = '#') This ends.

Int putchar (INT ch)
Putchar (CH) writes the CH character to the stdout of the standard stream.

Char * gets (char * Str)
Gets is mainly used to read strings from the standard input stream and display them back. It exits when it reads the linefeed and saves the linefeed.

Int puts (char * Str)
Puts writes the STR string to the stdout of the standard stream, and adds a line break to the end of the output.

Char * fgets (char * STR, int num, file * FP)
STR is the array pointer of characters to be read, num is the maximum number of characters allowed to be read, and FP is the file pointer. fgets is used to read a row of characters, the number of characters in this row
Not greater than the num-1. Because the fgets function will add an empty character at the end to form a string. In addition, fgets will not omit it after reading the linefeed.

Int fputs (char * STR, file * FP)
Fputs writes STR to FP. fputs. The difference between fputs and puts is that fputs do not add line breaks when printing.

Int fgetc (File * FP)
Fgetc reads a character from the current position of FP.

Int fputc (int ch, file * FP)
Fputc writes ch to the specified position of FP.

Int fscanf (File * FP, char * format ,...)
Fscanf reads data from the file in the specified format and assigns values to the parameter list.

Int fprintf (File * FP, char * format ,...)
Fprintf writes formatted data to a streaming file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.