Scanf, printf, fscanf, fprintf, sscanf, sprintf, etc.

Source: Internet
Author: User

(1) scanf and printf

 

Scanf slaveStdinInput is accepted, and the data is output to the variable in the specified format.

The original scanf function is: int scanf (_ Const char * _ restrict _ format,...) _ WUR, where... is a variable parameter with a variable length. for example, scanf ("% d % F", & I, & F) assigns a value to the variables I and F.

 

Printf willVariableOutputStdout

The prototype of the printf function is: int printf (_ Const char * _ restrict _ format,...), For example: printf ("% d", I) indicates to pass the value of variable I to the standard output.

(2) fscanf and fprintf

 

Fscanf slaveFileAccept input, output data in the specified formatVariable.

The fscanf function is prototype: int fscanf (File * _ restrict _ stream,_ Const char * _ restrict _ format,...) _ WUR, for example, fscanf (FP, "% d % F", & I, & F) indicates the current data represented by the file pointer, usually one line is output to the variables I and F.

 

Fprintf willVariableOutputFile

The fprintf function is prototype: int fprintf (File * _ restrict _ stream,_ Const char * _ restrict _ format,...), Such as fprintf (FP, "% d % F", I, F );

 

 

(3) sscanf and sprintf

 

Sscanf slaveStringAccept input, output data in the specified formatVariableTo convert strings to other variables.

The sscanf function is prototype: int sscanf (_ const char * _ restrict _ s, _ const char * _ restrict _ format ,...) _ Throw. For example, sscanf (P, "% d", & I) converts the string represented by P to an integer variable and assigns it to I, if the pointer P points to the string "12345", then I = 12345.

 

Sprintf outputs variables in the specified formatStringTo convert other types of variables into strings.

Sprintf function prototype: int sprintf (char * _ restrict _ s, _ const char * _ restrict _ format ,...) _ Throw, for example, sprintf (P, "% d", I). If I = 12345, the pointer P points to the memory and stores the string "12345 ".

 

 

These functions also support regular expressions, which are very powerful and often used.

 

Relevant information is worth reading:

 

Sprintf, how much do you know? Http://blog.csdn.net/sjf331/archive/2005/04/07/339254.aspx

Regular Expressions in sscanf, scanf, and fscanf

Http://ybmmwjl.blog.163.com/blog/static/656387812010102691254484/

Scanf and sscanf

Http://wenku.baidu.com/view/3e3250160b4e767f5acfce85.html

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.