Silent Man
Silent Man

Reputation: 1

Spacing in my Output slightly off in C

700   =   17 + 683
702   =   11 + 691
704   =   3 + 701
706   =   5 + 701

As you can see...the 3 and 5 is in the wrong place it should be:

700   =   17 + 683
702   =   11 + 691
704   =    3 + 701
706   =    5 + 701

My code for this:

fprintf(fpout, "%d   =   %d + %d\n", lower, primeNum1, primeNum2);

I'm assuming I need to use some special technique to resolve this issue. Could someone please help here.

Upvotes: 0

Views: 458

Answers (2)

Barmar
Barmar

Reputation: 781004

Specify the field widths:

printf(fpout, "%3d = %3d + %3d\n", lower, primeNum1, primeNum2);

Upvotes: 1

rob mayoff
rob mayoff

Reputation: 385610

You can specify a minimum field width by putting a number between the % and the d. When the converted value is shorter than the minimum field width, the field will be padded on the left with spaces.

fprintf(fpout, "%3d   =   %2d + %3d\n", lower, primeNum1, primeNum2);

Of course, you may need to give some thought to what the appropriate minimum field widths are based on your expected values for lower, primeNum1, and primeNum2.

Upvotes: 1

Related Questions