I'm trying to use printf to get the following programmatic output:
- 20
- 15
- 10
- 5
0
+ 5
+ 10
+ 15
+ 20
The key specifications are:
So far I have not been able to come up with a printf statement that will give me the desired results. The closest I have is:
for(int i = -20; i <= 20; i+=5)
{
printf("%-+4d \n", i);
}
which produces:
-20
-15
-10
-5
+0
+5
+10
+15
+20
Is there a way to do this without having to do any cumbersome string manipulation?
printf("%c%3d\n", i>0 ? '+' : i<0 ? '-' : ' ', abs(i));
Note the above fails for INT_MIN
, but that shouldn't be an issue since your values are only expected to be less than 1000 in magnitude.
A simple one-liner, not much different than others, yet with a nice look-up to a "- +"
string.
printf("%c%3d\n", "- +"[i >= 0 + i > 0], abs(i));
To handle values outside the [-999 ... + 999] range and especially the pesky i = INT_MIN
where the -i
is undefined behavior, the code could use:
printf("%c%3u\n", "- +"[i >= 0 + i > 0], (i < 0) ? 0u - i: 0u + i);
// or
printf("%c%3lld\n", "- +"[i >= 0 + i > 0], llabs(i));
For a pedantic full range solution
// Buffer size = iceil(bit-width * log2(10)) + sign + null character
#define INT_BUF_SIZE (CHAR_BIT * sizeof(int)*31/100 + 3)
int print_int(int x) {
char buf[INT_BUF_SIZE*2];
sprintf(buf, "%+d", x);
int width = 4;
return printf("%c%*s\n", x ? buf[0] : ' ', width - 1, buf + 1);
}
- 20
0
+ 20
+2147483647
-2147483648
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With