0

my question is short, if I have the following 2 lines of code:

int var = 01;
printf("%d", var);

the output is : 1

how do I get 01 rather than 1?

BeNicePlz
  • 43
  • 6
  • 1
    Please note that `01` doesn't mean a `1` with a `0` in front, but it means _write 1 using octal base notation_. Thus `010` will print `8`. In general, don't assume that the language works in the way you want, learn how it actually works instead. – Lundin Nov 12 '20 at 08:58
  • You thought to save the number as a string? If you don't have to use it elsewhere it is probably the most flexible solution – lorenzozane Nov 12 '20 at 09:05

1 Answers1

1

Use left padded format string.

Solution

int var = 1;
printf("%02d", var);
Jiho Lee
  • 957
  • 1
  • 9
  • 24