Format input and output of standard IO

Source: Internet
Author: User
Tags sprintf

1. Format input

Return value: Three function return value is consistent, if successful, specify the number of entries entered, if the input error or the end of the file before any transformation to return EOF

(1). scanf

Prototype: int scanf (const char *restrict format, ...);

Most basic, from standard input input

(2). fscanf

Prototype: int fscanf (FILE *restrict FP, const char *restrict format, ...);

and (1) , you can specify the stream FP, no longer limit the standard input

(3). sscanf

Prototype: int sscanf (const char *restrict buf, const char *restrict fromat, ...);

and (2) In contrast, not from stream input, from array buf input

2. Formatted output

(1). printf

Most basic, write formatted data to standard output, without the problem of buffer overflow

Return value: Returns a negative if an error occurs if the output character number is successfully returned

(2). fprintf

Prototype: int fprintf (FILE *restrict FP, const char *restrict format, ...);

Return value: Returns a negative if an error occurs if the output character number is successfully returned

and (1) , you can specify the stream to write, no longer limit the standard output

(3). sprintf

Prototype: int sprintf (char *restrict buf, const *restrict format, ...);

Return value: If the number of characters in the array is successfully returned, negative values are returned if an error is encoded

In contrast to (2) , the write is no longer a stream, but an array buf, and sprintf automatically adds a null byte at the end of the array, but the byte is not included in the return value, because of the addition of a null byte, May cause a buffer overflow , the caller should be aware.

(4). snprintf

Prototypes:int sprintf (char *restrict buf, size_t N, const *restrict format, ...);

Return value: If the number of characters in the array is successfully returned, negative values are returned if an error is encoded

In addition to (3) , buffer length n is added, which resolves a buffer overflow problem , and the return value does not include a null byte

Format input and output of standard IO

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.