int a = 10;
int b = -1;
int c = +12345;
如何用sprintf将这种整型变量转换为7位带符号的字符型变量,例如:
char n[7];
memset(n, 0, sizeof(n));
sprintf(n, "%06d", a);
但这种格式是不带符号的,且对于a = 10这种不带+,但赋予n时应该带+号的,只能通过判断?
谢谢!
12 个解决方案
#1
[liangdong@bb-browser-test00.vm.baidu.com c_project]$ make
gcc -g -I. -c -o main.o main.c
gcc -o main main.o -lpthread
[liangdong@bb-browser-test00.vm.baidu.com c_project]$ ./main
-000010
[liangdong@bb-browser-test00.vm.baidu.com c_project]$ cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* const argv[]) {
int num = -10;
char str_num[10];
snprintf(str_num, 10, "%+07d\n", num);
str_num[sizeof(str_num) - 1] = '\0';
fputs(str_num, stdout);
return 0;
}
[liangdong@bb
#2
str_num[sizeof(str_num) - 1] = '\0';
这一行去掉.
这一行去掉.
#3
[code=C/C++]#include <cstdio>
int main()
{
int a = 89;
char n[7];
//memset(n, 0, sizeof(n));
sprintf(n, "%+06d", a);
puts(n);
return 0;
}code]
int main()
{
int a = 89;
char n[7];
//memset(n, 0, sizeof(n));
sprintf(n, "%+06d", a);
puts(n);
return 0;
}code]
#4
#include <cstdio>
int main()
{
int a = 89;
char n[7];
//memset(n, 0, sizeof(n));
sprintf(n, "%+06d", a);
puts(n);
return 0;
}
#5
可能我没说清楚,int a可能是19,可能是+19,也可能是-19,那sprintf如何动态确定符号是+还是-呢?
#6
且要求打印出来的char n必须是“一个符号位+6个数字”
#7
#8
Flags (in any order), which modify the specification:
o -, which specifies left adjustment of the converted argument in its field.
o +, which specifies that the number will always be printed with a sign.
o space: if the first character is not a sign, a space will be prefixed.
o 0: for numeric conversions, specifies padding to the field width with leading zeros.
o #, which specifies an alternate output form. For o, the first digit will become zero. For x or X, 0x or 0X will be prefixed to a non-zero result. For e, E, f, g, and G, the output will always have a decimal point; for g and G, trailing zeros will not be removed.
o -, which specifies left adjustment of the converted argument in its field.
o +, which specifies that the number will always be printed with a sign.
o space: if the first character is not a sign, a space will be prefixed.
o 0: for numeric conversions, specifies padding to the field width with leading zeros.
o #, which specifies an alternate output form. For o, the first digit will become zero. For x or X, 0x or 0X will be prefixed to a non-zero result. For e, E, f, g, and G, the output will always have a decimal point; for g and G, trailing zeros will not be removed.
#9
能不能通过if(a>0)else(a<0)判断呢?
#10
支持!
C程序员对printf的格式约定语法还是要做到“曲不离口、拳不离手”的。
#11
精辟,“+”就是表示打印总会带符号。
弱弱地问下这段解释出自哪里呢?类似的约定能否给个链接?谢谢!
弱弱地问下这段解释出自哪里呢?类似的约定能否给个链接?谢谢!
#12
mk:@MSITStore:C:\MSDN98\98VS\2052\vccore.chm::/html/_crt_flag_directives.htm#_crt_table_r..4
查MSDN是Windows程序员必须掌握的技能之一。
会用man命令查函数用法是Linux程序员必须掌握的技能之一。
查MSDN是Windows程序员必须掌握的技能之一。
会用man命令查函数用法是Linux程序员必须掌握的技能之一。
#1
[liangdong@bb-browser-test00.vm.baidu.com c_project]$ make
gcc -g -I. -c -o main.o main.c
gcc -o main main.o -lpthread
[liangdong@bb-browser-test00.vm.baidu.com c_project]$ ./main
-000010
[liangdong@bb-browser-test00.vm.baidu.com c_project]$ cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* const argv[]) {
int num = -10;
char str_num[10];
snprintf(str_num, 10, "%+07d\n", num);
str_num[sizeof(str_num) - 1] = '\0';
fputs(str_num, stdout);
return 0;
}
[liangdong@bb
#2
str_num[sizeof(str_num) - 1] = '\0';
这一行去掉.
这一行去掉.
#3
[code=C/C++]#include <cstdio>
int main()
{
int a = 89;
char n[7];
//memset(n, 0, sizeof(n));
sprintf(n, "%+06d", a);
puts(n);
return 0;
}code]
int main()
{
int a = 89;
char n[7];
//memset(n, 0, sizeof(n));
sprintf(n, "%+06d", a);
puts(n);
return 0;
}code]
#4
#include <cstdio>
int main()
{
int a = 89;
char n[7];
//memset(n, 0, sizeof(n));
sprintf(n, "%+06d", a);
puts(n);
return 0;
}
#5
可能我没说清楚,int a可能是19,可能是+19,也可能是-19,那sprintf如何动态确定符号是+还是-呢?
#6
且要求打印出来的char n必须是“一个符号位+6个数字”
#7
#8
Flags (in any order), which modify the specification:
o -, which specifies left adjustment of the converted argument in its field.
o +, which specifies that the number will always be printed with a sign.
o space: if the first character is not a sign, a space will be prefixed.
o 0: for numeric conversions, specifies padding to the field width with leading zeros.
o #, which specifies an alternate output form. For o, the first digit will become zero. For x or X, 0x or 0X will be prefixed to a non-zero result. For e, E, f, g, and G, the output will always have a decimal point; for g and G, trailing zeros will not be removed.
o -, which specifies left adjustment of the converted argument in its field.
o +, which specifies that the number will always be printed with a sign.
o space: if the first character is not a sign, a space will be prefixed.
o 0: for numeric conversions, specifies padding to the field width with leading zeros.
o #, which specifies an alternate output form. For o, the first digit will become zero. For x or X, 0x or 0X will be prefixed to a non-zero result. For e, E, f, g, and G, the output will always have a decimal point; for g and G, trailing zeros will not be removed.
#9
能不能通过if(a>0)else(a<0)判断呢?
#10
支持!
C程序员对printf的格式约定语法还是要做到“曲不离口、拳不离手”的。
#11
精辟,“+”就是表示打印总会带符号。
弱弱地问下这段解释出自哪里呢?类似的约定能否给个链接?谢谢!
弱弱地问下这段解释出自哪里呢?类似的约定能否给个链接?谢谢!
#12
mk:@MSITStore:C:\MSDN98\98VS\2052\vccore.chm::/html/_crt_flag_directives.htm#_crt_table_r..4
查MSDN是Windows程序员必须掌握的技能之一。
会用man命令查函数用法是Linux程序员必须掌握的技能之一。
查MSDN是Windows程序员必须掌握的技能之一。
会用man命令查函数用法是Linux程序员必须掌握的技能之一。