neversettle evolve
neversettle evolve

Reputation: 13

Printing leading '-' in C?

i was trying to print numbers in right justified format:

suppose,

input:

printf("\n%d", 1234567);
printf("\n%07d", 5);

output:

1234567
0000005

but what if i want to print

------5

i'm confused here, as '-' is used for left justification

i tried using

printf("\n%\-d", 5);

but console shows

warning: unknown escape sequence: '\-'

Upvotes: 0

Views: 206

Answers (4)

ryyker
ryyker

Reputation: 23218

This is the problem:

printf("\n%\-d", 5);
          ^^

The % expects one of the following printf format specifiers:

%c    character
%d    decimal (integer) number (base 10)
%e    exponential floating-point number
%f    floating-point number
%i    integer (base 10)
%o    octal number (base 8)
%s    a string of characters
%u    unsigned decimal (integer) number
%x    number in hexadecimal (base 16)
%%    print a percent sign

you can change that statement to:

 printf("\-%d", 5);// to get `-5`

To get the number of - to match the number of digits in the integer, you will need to do something similar to:

int b=123456789;
int a;
a = b;
int min_width = 15;

while (b != 0) {
    b /= 10;     // n = n/10
    printf("\-");;
}
printf("%d", a);

And if the output needs to be left justified, use a width specifier:

b /= 10;     // n = n/10
printf("%*s", min_width, "\-");   
while (b != 0) {
    b /= 10;     // n = n/10
    printf("\-");;
}
printf("%d\n", a);

Upvotes: 0

Rohan Bari
Rohan Bari

Reputation: 7726

There's one rough idea that does what you actually want. Why not count the number of digits in the given variable and print the backspace character and print back the number like this:

#include <stdio.h>

int main(void) {
    int num = 1234;
    int temp = num;
    int count = 0;

    if (num < 0)
        puts("The number is negative, don't ignore the last dash");

    printf("--------");

    while (temp != 0) {
        temp /= 10;
        ++count; // it'll count till '4' because 1234 has 4 digits
    }

    for (int i = 0; i < count; i++)
        printf("\b"); // so '4' backspaces

    printf("%d\n", num); // the dashes will be replaced with the number

    return 0;
}

A sample test case is as follows (depends upon 'num'):

----1234

Upvotes: 1

Tsak
Tsak

Reputation: 133

i don't think printf has something which would do this, you can try to have fun by writing your own printf ^^ you may have to put a loop to print the right number of - signs before printing your number, and you may need another function to determine the right number of - sign to print depending on the desired width and the width of number, something like:

int get_nb_width(int nb, int base)
{
    int ret;
    if (base < 2)
        return (0);
    ret = 1 + (nb < 0);
    for (;nb / base != 0;nb /= base)
        ret++;
    return (ret);
}
void print_minus_signs(int nb, int total_width, int base)
{
    int width;

    if (get_nb_width(nb, base) == 0)
        return ;
    for (width = get_nb_width(nb, base); width < total_width; width++)
        printf("-");
}
int main()
{
    int nb = -12;
    int width = 7;

    print_minus_signs(nb, width, 10);
    printf("%d", nb);
    return (0);
}

this code should print :

-----12

(four '-' printed by the function, and '-12' printed by the printf)

Upvotes: 0

Weather Vane
Weather Vane

Reputation: 34585

This is one way to do it, taking advantage of the %.*s format specifier.

#include <stdio.h>

void dashed(int width, int num)
{
    char buff[12];          // enough for 32-bit int
    int len = width - sprintf(buff, "%d", num);
    printf("%.*s%s", len < 0 ? 0 : len, "-----------", buff);
}

int main(void)
{
    dashed(7, 5);           // 1 digit
    puts("");
    dashed(7, 55);          // 2 digits
    puts("");
    dashed(7, 555555);      // 6 digits
    puts("");
    dashed(7, 5555555);     // 7 digits
    puts("");
    dashed(7, 55555555);    // 8 digits
    puts("");
    return 0;
}

Program output:

------5
-----55
-555555
5555555
55555555

Upvotes: 2

Related Questions