%-d
Will left adjust the integer field, it won't flip the sign. Do this instead:
printf (" %d\n", -1977);
Here's the full extract from print(3)
under The flag characters:
- The converted value is to be left adjusted on the field bound‐
ary. (The default is right justification.) Except for n con‐
versions, the converted value is padded on the right with
blanks, rather than on the left with blanks or zeros. A - over‐
rides a 0 if both are given.
Update0
I see your true question now: To prepend output with the appropriate sign, use the +
flag character to explictly show the sign. Again here is the extract:
+ A sign (+ or -) should always be placed before a number produced
by a signed conversion. By default a sign is used only for neg‐
ative numbers. A + overrides a space if both are used.
And use it like this (The command line printf
is mostly identical):
matt@stanley:~/cpfs$ printf "%+d\n" 3
+3
matt@stanley:~/cpfs$ printf "%+d\n" -3
-3
matt@stanley:~/cpfs$ printf "%+u\n" -3
18446744073709551613
Be aware that explicitly requesting the sign won't imply treatment of the corresponding integer as signed as in the %u
example above.