This is a discussion on leading zero's..... ( reading int ) within the C Programming forums, part of the General Programming Boards category; i have a question about leading zero's... now, if i have made a program, and declared everything to be of ...

now, if i have made a program, and declared everything to be of type int

if i prompt the user to enter in an integer value, and the user enters:

0005

then designed my program to read in the integer, using scanf(), then print the integer back using printf()

why would the leading zeros not be shown???

is it something to do w/ the properties of int?

2. Numbers are just numbers. In regular math, 1 is 01 is 001 is 0001. It's all just different ways of representing the number 1.

When you read in 0005 via scanf(), it's just 5. That's what the number really is, right? Therefore 5 is stored. When you print it back, it prints 5 back. Remember, memory is laid out in terms of bytes. You can't just tell it not to store the extra bits.

Pretending you read it into a one-byte memory address, 5 is stored as this:

00000101

The 0's and 1's have to be set. They can't just disappear.

Now if you want to print it back with leading zero's, you can do that via printf().

3. Originally Posted by MacGyver
Numbers are just numbers. In regular math, 1 is 01 is 001 is 0001. It's all just different ways of representing the number 1.

When you read in 0005 via scanf(), it's just 5. That's what the number really is, right? Therefore 5 is stored. When you print it back, it prints 5 back. Remember, memory is laid out in terms of bytes. You can't just tell it not to store the extra bits.

Pretending you read it into a one-byte memory address, 5 is stored as this:

00000101

The 0's and 1's have to be set. They can't just disappear.

Now if you want to print it back with leading zero's, you can do that via printf().

so if i want to display the leading zero's, i can keep type int?
but maybe just display something other than %d?

4. Code:
```#include <stdio.h>

int main(void)
{
int x = 5;

printf("&#37;05d\n",x);

return 0;
}```
Output:

Code:
`00005`
Google for information on printf()'s format specifiers.

5. Originally Posted by MacGyver
Code:
```#include <stdio.h>

int main(void)
{
int x = 5;

printf("%05d\n",x);

return 0;
}```
Output:

Code:
`00005`
Google for information on printf()'s format specifiers.

ohhhhhh ok, i get it
thanks !