ciffobcrf的区别和定义(C简单理解sprintf和sscanf)

sprintf()和sscanf()声明在stdio.h中,属于输入输出一类操作,今天小编就来聊一聊关于ciffobcrf的区别和定义?接下来我们就一起去研究一下吧!

ciffobcrf的区别和定义(C简单理解sprintf和sscanf)

ciffobcrf的区别和定义

sprintf()和sscanf()声明在stdio.h中,属于输入输出一类操作。

先从理解printf()和scanf()开始。

printf()和scanf()也是声明在stdio.h中,属于输入输出一类操作,表示与控制台(console)的标准输入输出。

我们知道,对于输入输出而言,直接操作的都是属于文本一类的数据,但我们输入输出涉及到的数据类型却不只是文本,还有数值型数据,如整型,浮点型点。

由此,涉及到类型转换,C的做法是使用一个“格式化字符串”做参数,这个字符串内可以包含“数据类型转换符”。这类函数称为格式化输入输出函数,如printf()、scanf(),其中的字母f表示“format”,第一个参数为一个“格式化字符串”,其中的“数据类型转换符”由符号“%”引导(其中可以选择性地包含宽度、精度等)。函数通过分析这一个字符串来确定参数列表对应的数据类型。

int printf( const char *format, ... ); // format是格式化字符串,...表示参数列表 int scanf( const char *format, ... ); // format是格式化字符串,...表示参数列表 %说明符(specifier)对应数据类型 描述 %d / %i int 输出类型为有符号的十进制整数,i 是老式写法 %o unsigned int 输出类型为无符号八进制整数(没有前导 0) %u unsigned int 输出类型为无符号十进制整数 %x / %X unsigned int 输出类型为无符号十六进制整数,x 对应的是 abcdef,X 对应的是 ABCDEF(没有前导 0x 或者 0X) %f / %lf double 输出类型为十进制表示的浮点数,默认精度为6(lf 在 C99 开始加入标准,意思和 f 相同) %e / %E double 输出类型为科学计数法表示的数,此处 "e" 的大小写代表在输出时用的 “e” 的大小写,默认浮点数精度为6 %g double 根据数值不同自动选择 %f 或 %e,%e 格式在指数小于-4或指数大于等于精度时用使用 %G double 根据数值不同自动选择 %f 或 %E,%E 格式在指数小于-4或指数大于等于精度时用使用 %c char 输出类型为字符型。可以把输入的数字按照ASCII码相应转换为对应的字符 %s char * 输出类型为字符串。输出字符串中的字符直至遇到字符串中的空字符(字符串以 '\0‘ 结尾,这个 '\0' 即空字符)或者已打印了由精度指定的字符数 %p void * 以16进制形式输出指针 %% 不转换参数 不进行转换,输出字符‘%’(百分号)本身

(只有字符或文本的输入可以使用getchar()、putchar()、gets()、puts()等函数。)

printf()和scanf()也是标准输入输出函数,其中的标准是指数据的输入输出的目标设备或来源设备,也就是标准输入输出设备stdin、stdout。

输入输出数据的目标或来源可不可以是存储在内存中的字符串变量,答案是可以,其对应的函数也就是sprintf()和sscanf(),其第一个字符s,即表示string,其第一个参数也是一个字符串,表示数据的目标或来源,第2个参数,如同printf()和scanf(),是一个“”格式化字符串。

int sprintf(char *string, const char *format [,argument,...]);//根据参数列表将format解析为string int sscanf(const char *buffer,const char *format, [ argument ] ...);//根据format将buffer解析到参数列表。

sprintf()除了用于字符串输出以外,其实也可以用作字符格格式化。

对于scanf(),当发生输入的类型不匹配或溢出时,可能会发现不可预料的结果。建议的做法是使用gets()输入到一个字符串,然后使用sscanf()对这个字符串解析出目标类型的值:

#include <stdio.h> void main (void) { char input[256], name[256]; int age; printf ("What is your name, user?\n"); fgets (input, 256, stdin); sscanf (input, "%s", name); printf ("Hello, %s. How old are you?\n", name); while(1) { fgets (input, 256, stdin); if(sscanf (input, "%d", &age) == 1) break; printf ("I don't recognise that as an age - try again!\n"); } printf ("Well, %s, you look young for %d...\n", name, age); }

printf函数的目标设备是标准输出设备(抽象为stdout文件),sprintf函数的目标对象是内存中的字符串变量,还有以文件(FILE结构体描述)为输出目标的函数,称为fprintf函数:

#include <stdio.h> void main (void) { FILE *fp; fp = fopen ("/home/pi/output.txt", "wb"); if(fp) { fprintf (fp, "This is some text.\n"); fclose (fp); } }

相关内容参考:

Sprintf function can writes arbitrary text to string variables. The only difference with printf function is that the first argument it takes is the name of a string variable, and it writes to that instead of to the terminal.

Sprintf函数可以将任意文本写入字符串变量。与printf函数的唯一区别是,它采用的第一个参数是字符串变量的名称,并且它写入字符串变量而不是终端。

(可以理解printf()有一个省略的stdout参数,因为是标准输出,被省略。)

The sprintf function will automatically add the terminating zero at the end of any string you create with it.

sprintf函数将自动在使用它创建的任何字符串的末尾添加终止零。

We can use sprintf to write variables into a string; what about being able to read variables back out of a string? The function sscanf (‘string scan formatted’) does that for you.

我们可以使用sprintf将变量写入字符串;从字符串中读取变量呢?函数sscanf(“字符串扫描格式”)为您实现了这一点。

sscanf uses exactly the same format specifiers as printf. One important difference, though, is that the arguments to sscanf must all be pointers to variables, rather than variables themselves. As always, a function can never change the values of variables provided as arguments, but it can write to their destinations if they are pointers.

sscanf使用与printf完全相同的格式说明符。然而,一个重要的区别是,sscanf的参数必须都是指向变量的指针,而不是变量本身。与往常一样,函数永远不能更改作为参数提供的变量的值,但如果变量是指针,则可以写入它们的目标。

sscanf reads numeric values and words out of a formatted string, allowing you to parse text from elsewhere. Remember that all the arguments to sscanf must be pointers.

sscanf从格式化字符串中读取数值和单词,允许您从其他地方解析文本。记住,sscanf的所有参数都必须是指针。

-End-

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页