Summary of inputs and outputs (C language)

Source: Internet
Author: User
Tags stdin

C language has a variety of input and output methods, the following simple summary:

I. Three types of input

(1) scanf

The scanf function can be used in variables or in arrays, but it can also be used on pointers, which is a good input function. scanf is a format input, such as scanf ("%d-%d-%d", &i,&j,&z), When entering on the console, you should enter the format of the 2015-3-19 in double quotation marks, or it will go wrong!

Disadvantages of scanf:

There must be no space when the string is entered. Encountering a space automatically ends the input;

In addition, if the user enters a string longer than the array length, there is no space to save the Terminator!

Another drawback is that scanf belongs to an unsafe type of input method.

(2) Gets function Input method

Examples of Use:

Char arr[10];

Gets (arr);

printf ("%s", arr);

Advantage: string input, you can have a space;

Cons: Unsafe if the length of the input string is greater than the length of the array, it overflows

(3) fgets () function Input Method

Use of the Fgets () function: fgets (character array name, array length, stdin), where stdin refers to the standard input

Example: int arr[10];

Fgets (Arr,10,stdin);

Fgets () Advantage: If the input string length is greater than the length of the character array, Fgets automatically truncates

Note: The length of the fgets can only be saved for an array length minus one character, because he will use a character length to store ' \ n ';

When the input content is less than the length of the array, it receives a newline character (to verify that it receives a newline flag, the output is%d with the input ASCII number, ' \ n ' in the ASCII decimal number of 10), and its solution is to replace ' \ n ' with '

As follows:

(if (s[stlen[s]-1]) = = ' \ n ') {s[strlen[s]-1= ';}

Two. Three types of input functions

(1) printf () function

printf is formatted output, such as printf ("%d,%d,%d", i,j,k) on the console output format is 2015,3,19 (because the quotation marks are separated by parentheses, so the console is also separated by commas, if separated by a space, the console display is also separated by a space), In short, what is the format of printf and what format is displayed?

Cons: printf cannot wrap the line automatically

(2) puts () function output

The puts function overcomes the drawback that printf cannot wrap automatically, but it can not format output like printf.

Use the format: puts (array name);

(3) fputs () function output

Fputs () can output a string to a file in the following format:

Fputs (array name, stdout), where stdout standard output

Example: Fgets (S, stdout)

Disadvantage: fputs cannot be wrapped automatically.

Summary of inputs and outputs (C language)

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.