sprintf()函数标准化输出实用实例
2017-04-18 19:39
162 查看
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <iostream> using namespace std; void test1(char* str) { memset(str,0,100); sprintf(str,"%06s","1234"); printf("%s\n",str); } void test2(char* str) { memset(str,0,100); sprintf(str,"%010d",11111); printf("%s\n",str); } //指定宽度 void test3(char* str) { memset(str,0,100); sprintf(str,"%010.3f",1233.1415); printf("%s\n",str); } //不指定宽度 void test4(char* str) { memset(str,0,100); sprintf(str,"%.3f",1233.1415); printf("%s\n",str); } void test5(char* str) { //左对齐 memset(str,0,100); sprintf(str,"%-10.3f",1233.1415); printf("%s\n",str< 4000 /span>); //右对齐,不足补0 memset(str,0,100); sprintf(str,"%010.3f",1233.1415); cout<<"==========="<<endl; printf("%s\n",str); } void test6(char* str) { char s1[10]="asdf"; char s2[10]="qwer"; memset(str,0,100); sprintf(str,"%.3s%.3s",s1,s2); printf("%s\n",str); } void test7(char* str) { memset(str,0,100); sprintf(str,"%07d",11111); printf("%s\n",str); } int main() { char str[100]={0}; test1(str); test2(str); test3(str); test4(str); test5(str); test6(str); test7(str); return 0; }
下面是输出结果:
相关文章推荐
- 实例-sprintf() 函数详解-输出格式转换函数
- sprintf() 格式化输出函数
- putchar()和getchar()函数的一些简单实用实例!
- 【php】如何在sprintf函数中输出百分号(%)等特殊符号
- 一起talk C栗子吧(第一百七十八回:C语言实例--字符和字符串输出函数一)
- php格式输出文件var_export函数实例
- sprintf() 格式化输出函数(图形)
- 一起talk C栗子吧(第一百八十三回:C语言实例--在printf函数中设置输出宽度二)
- PHP 实用函数sprintf()详解
- APUE之dup,dup2函数重定向标准输出实例
- 一起talk C栗子吧(第一百八十一回:C语言实例--通过printf函数输出%)
- php中格式化输出函数vprintf printf sprintf sscanf
- 关于sprintf函数——两个格式输出问题
- C++中的文件的输入输出函数应用实例
- arduino上使用sprintf()函数输出float值出错
- c 函数不定参数(不限参数)使用实例 不定参数输出函数
- 给定一个字符串,里面用空格分开为多个(>=6)部分组合,如:01 02 03 04 05 06 07 …… 写一个函数返回任意6个组合的字符串: 输出格式实例: 01 02 03 04 05 06 0
- 分享一个关于php中explode()函数输出实例(简单的)
- 基于JS实现PHP的sprintf函数实例
- 一起talk C栗子吧(第一百八十二回:C语言实例--在printf函数中设置输出宽度一)