Is this possible printf format (left sign and unsigned sign)

I am trying to use printf to get the following programmatic output:

- 20
- 15
- 10
-  5
   0
+  5
+ 10
+ 15
+ 20

Key features are:

  • The width of the field is always 4
  • The sign is always left-handed
  • The number is always justified.
  • Zero has no sign

So far, I have not been able to come up with a printf expression that will give me the desired results. The closest I have:

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 perform cumbersome string manipulations?

+4
source share
4 answers
printf("%c%3d\n", i>0 ? '+' : i<0 ? '-' : ' ', abs(i));

Please note that the above does not work for INT_MIN, but this should not be a problem, since your values ​​should be less than 1000.

+6

1 , , "- +".

printf("%c%3d\n", "- +"[i >= 0 + i > 0], abs(i));

[-999... + 999] pesky i = INT_MIN, -i undefined, :

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));

// 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
+3
for(int i = -20; i <=20; i+=5){
    if(i==0){
        printf("%4d\n", i);
    }
    else{
        (i > 0)?printf("+%3d\n", i):printf("-%3d\n", -i);

    }

}

Ugly but it works

0
source
for(int i = -20; i <= 20; i+=5) {
    if( i < 0 ) 
        printf("-%3d \n", -i);
    else if(i == 0)
        printf("  0 \n");
    else
        printf("+%3d \n", i);
}
0
source

Source: https://habr.com/ru/post/1685912/


All Articles