c语言scanf_05.C语言之get、puts,scanf、sscanf操作细节

scanf()所在头文件:stdio.h语法:scanf(“格式控制字符串”,变量地址列表);接受字符串时:scanf(“%s”,字符数组名或指针);gets()所在头文件:stdio.h语法:gets(字符数组名或指针);/* sscanf() - 从一个字符串中读进与指定格式相符的数据.   函数原型:   int sscanf( const char *, const char *, ...);   int sscanf(const char *buffer,const char *format,[argument ]...);   buffer 存储的数据   format 格式控制字符串   argument 选择性设定字符串   sscanf会从buffer里读进数据,依照argument的设定将数据写回。*//*    当数组长度不够。假设我们指定了数组长度,如:u8 str1[13]={"cxjr.21ic.org"};    由于字符组str1的长度为13,所以后面的信息会丢失,即'0'丢失。    如果在给数组赋值时,把每个字符单独用引号括起来。也会丢失'0'。如:    u8 str1[]={'c','x','j','r','.','2','1','i','c','.','o','r','g'};    如果希望数组以'0'结束,则可以写成以下三者之一:u8 str1[]={"cxjr.21ic.org"}; //字符串赋值u8 str1[]={'c','x','j','r','.','2','1','i','c','.','o','r','g','0'}; //人工添加u8 str1[14]={'c','x','j','r','.','2','1','i','c','.','o','r','g'}; //故意给数组预留一个空位*/#if 1#include int main(void) {  //  unsigned  char a[10]=   {'a','1','2','3','4','5','6','7','8','b'};  //没有结束符 ,数组必须以'0'结束   unsigned  char a[10]=   {'a','1','2','3','4','5','6','7','8','0'};   unsigned  int b[10]=   {0,1,2,3,4,5,6,7,8,9},i;   unsigned  char c[10];   sscanf(a, "%s", c);  while(i<10){  printf("c is %c",c[i]);      i++; }   puts(c); //printf("%c", c[10]);             //bug test  puts("input a new c string!");  gets(c);   //从键盘读取输入字符串到c数组   puts(c);   //打印字符串  return 0;}#endif/*c is ac is 1c is 2c is 3c is 4c is 5c is 6c is 7c is 8c is ba12345678b/input a new c string!4546ihi4546ihi--------------------------------Process exited after 7.166 seconds with return value 0请按任意键继续. . .*/#if 0#include  int main()  { const char* s = "iios/12DDWDFF@122"; char buf[20],a[20];sscanf( s, "%*[^/]/%[^@]", buf );//加*则是忽略第一个读到的字符串sscanf( "15892acD", "%[1-9a-c]", a ); printf( "%s", buf );printf( "%s", a ); sscanf("hello, world","%*s%s", buf);printf("%s", buf);             //worldreturn 0; }   #endif/* 12DDWDFF15892acworld--------------------------------Process exited after 0.1601 seconds with return value 0请按任意键继续. . .*/
1c6515dae11fca25fd6abae1ec8bb8a1.png

C语言基础

scanf()所在头文件:stdio.h语法:scanf(“格式控制字符串”,变量地址列表);接受字符串时:scanf(“%s”,字符数组名或指针);gets()所在头文件:stdio.h语法:gets(字符数组名或指针);/* sscanf() - 从一个字符串中读进与指定格式相符的数据.   函数原型:   int sscanf( const char *, const char *, ...);   int sscanf(const char *buffer,const char *format,[argument ]...);   buffer 存储的数据   format 格式控制字符串   argument 选择性设定字符串   sscanf会从buffer里读进数据,依照argument的设定将数据写回。*//*    当数组长度不够。假设我们指定了数组长度,如:u8 str1[13]={"cxjr.21ic.org"};    由于字符组str1的长度为13,所以后面的信息会丢失,即'0'丢失。    如果在给数组赋值时,把每个字符单独用引号括起来。也会丢失'0'。如:    u8 str1[]={'c','x','j','r','.','2','1','i','c','.','o','r','g'};    如果希望数组以'0'结束,则可以写成以下三者之一:u8 str1[]={"cxjr.21ic.org"}; //字符串赋值u8 str1[]={'c','x','j','r','.','2','1','i','c','.','o','r','g','0'}; //人工添加u8 str1[14]={'c','x','j','r','.','2','1','i','c','.','o','r','g'}; //故意给数组预留一个空位*/#if 1#include int main(void) {  //  unsigned  char a[10]=   {'a','1','2','3','4','5','6','7','8','b'};  //没有结束符 ,数组必须以'0'结束   unsigned  char a[10]=   {'a','1','2','3','4','5','6','7','8','0'};   unsigned  int b[10]=   {0,1,2,3,4,5,6,7,8},i;   unsigned  char c[10];   sscanf(a, "%s", c);  while(i<10){  printf("c is %c",c[i]);      i++; }   puts(c); //printf("%c", c[10]);             //bug test  puts("input a new c string!");  gets(c);   //从键盘读取输入字符串到c数组   puts(c);   //打印字符串  return 0;}#endif/*c is ac is 1c is 2c is 3c is 4c is 5c is 6c is 7c is 8c is a12345678/input a new c string!4546ihi4546ihi--------------------------------Process exited after 7.166 seconds with return value 0请按任意键继续. . .*/#if 0#include  int main()  { const char* s = "iios/12DDWDFF@122"; char buf[20],a[20];sscanf( s, "%*[^/]/%[^@]", buf );//加*则是忽略第一个读到的字符串sscanf( "15892acD", "%[1-9a-c]", a ); printf( "%s", buf );printf( "%s", a ); sscanf("hello, world","%*s%s", buf);printf("%s", buf);             //worldreturn 0; }   #endif/* 12DDWDFF15892acworld--------------------------------Process exited after 0.1601 seconds with return value 0请按任意键继续. . .*/

亲们有问题可以在评论区提出偶,我将不定期给出解答,让我们一起进步一起成长!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值