码迷,mamicode.com
首页 > 其他好文 > 详细

C 格式化字符串处理函数

时间:2019-09-24 15:52:23      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:字符串处理   amp   dep   item   sid   NPU   using   items   col   

1.提取字符串中的数据到变量

// crt_sscanf.c
// compile with: /W3
// This program uses sscanf to read data items
// from a string named tokenstring, then displays them.

#include <stdio.h>

int main( void )
{
   char  tokenstring[] = "15 12 14...";
   char  s[81];
   char  c;
   int   i;
   float fp;

   // Input various data from tokenstring:
   // max 80 character string:
   sscanf( tokenstring, "%80s", s ); // C4996
   sscanf( tokenstring, "%c", &c );  // C4996
   sscanf( tokenstring, "%d", &i );  // C4996
   sscanf( tokenstring, "%f", &fp ); // C4996
   // Note: sscanf is deprecated; consider using sscanf_s instead

   // Output the data read
   printf( "String    = %s\n", s );
   printf( "Character = %c\n", c );
   printf( "Integer:  = %d\n", i );
   printf( "Real:     = %f\n", fp );
}

结果

String    = 15
Character = 1
Integer:  = 15
Real:     = 15.000000

2.变量进字符串

int xPos = 1;
int yPos = 2;
char str[50];
sprintf(str, "x:%d,y:%d;", xPos, yPos);

 

C 格式化字符串处理函数

标签:字符串处理   amp   dep   item   sid   NPU   using   items   col   

原文地址:https://www.cnblogs.com/ScarecrowMark/p/11578526.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!