Val K
Val K

Reputation: 11

Store leading zeroes in C

For starters, I'm new to programming

I'd like to know how to store a number with leading zeroes in it with scanf instead of modifying the printf. Is it related to data types? If so, what is it? and how do I use it? I currently only know int, double and float

For example, I'd like to input the number "02" and receive "02" as the output, and when I input "2" the output will also be "2".

Upvotes: 1

Views: 4159

Answers (5)

Eric Postpischil
Eric Postpischil

Reputation: 224586

The result of using scanf specifiers d, i, o, u, x, a, e, f, g is a mathematical number. There are no leading zeros. Conceptually, there are no digits in a number; it is a pure mathematical entity. (As Jonathan Leffler notes, leading zeros in the input matter the i specifier; a leading zero changes the base to octal while interpreting the numeral. This is for input only; the result is still a number with no associated digits or base.)

To preserve the leading zeros of a numeral, you must treat it as a string. scanf can be used to read strings or individual characters with the s, c, and [ specifiers, although you might wish to use a simple get-character function such as getchar. You would have to count the leading zeros yourself. The remaining digits could also be handled as a string, or you could convert them to a number.

Upvotes: 4

chux
chux

Reputation: 154592

how to store a number with leading zeroes (?)
I currently only know int, double and float

To store an integer and the leading zero count is then 2 pieces of information. When reading user input, record the number and its length or textual width.

" " to consume whitespace.
"%n" to record the offset of characters scanned so far. This does not affect the return value of scanf().

int num;
int n1, n2;
if (scanf(" %n%d%n", &n1, &num, &n2) != 1) {
  puts("Failed to read an int");
}

Otherwise, print it.
"0": pad with zeros.
"*": minimum width to print derived from argument list.

else {
  int length = n2 - n1;
  printf("%0*d\n", length, num);
}

Input/output

  007
007

Upvotes: 4

Andreas DM
Andreas DM

Reputation: 11028

As already explained, numbers don't have leading zeros.

You can however treat it as a string

printf("Enter num: ");
char buf[50];
scanf("%s", &buf);
printf("You entered: %s", buf);

Upvotes: 1

Arkku
Arkku

Reputation: 42159

If you store the number as an integer (e.g., int) or floating point type, any formatting information is inevitably lost: only the binary representation of the number is stored. (It may help to consider it as saving the idea of the number, e.g., "two", not how it looks when written.)

You need to store any additional information elsewhere, such as by saving the original string and using that for output, or by saving the number of digits and applying that to the printf format. There are also decimal number libraries that can internally save the decimal representation of the number, but that is considerably heavier than using the native types.

(Alternatively, just settle on a canonical output format and use that regardless of input. Is the goal really to preserve any input formatting as is, or would it suffice to always add the leading zeroes to the output?)

Upvotes: 1

tadman
tadman

Reputation: 211740

If you've got an int value, internally it's always represented as a certain number of bits (or whatever your CPU uses to store numbers) so you have no control over this.

There's always "leading zeroes" in terms of the internal format, because, for example, because 2099 is actually 0b00000000000000000000100000110011 in 32-bit form. This gets more complicated due to endian issues, but the principle still holds.

The only time leading zeroes makes sense is if you're using binary coded decimal, which has fallen out of style, or string notation such as produced by sprintf type functions.

It's only humans that care about digits. As far as a computer's concerned it's equally difficult to add 1+1 as it is 6916863870493370158+6471945999301299985 because they're both 64-bit operations.

Upvotes: 0

Related Questions