468,738 Members | 2,676 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

How do I print a 0 in the beginning?

I have this:

#include <stdio.h>

int main (void)
{

int number;

printf ("Enter number: ");
scanf ("%i", &number);
printf ("%05i", number);

return 0;
}

So, if i enter a number like '00768', it should print out the same
'00768'. But its not doing that...its printing out 5 zeros instead.
I have tried using %06d, %.5i, %.5d in the printf statement. None of
them work properly. I can't use any bigger functions, just printf to
do this. Thanks.
Feb 16 '06 #1
7 6689
gk245 explained :
I have this:

#include <stdio.h>

int main (void)
{

int number;

printf ("Enter number: ");
scanf ("%i", &number);
printf ("%05i", number);

return 0;
}

So, if i enter a number like '00768', it should print out the same '00768'.
But its not doing that...its printing out 5 zeros instead.
I have tried using %06d, %.5i, %.5d in the printf statement. None of them
work properly. I can't use any bigger functions, just printf to do this.
Thanks.


woops, sorry, i meant if you typed in a number like 5674, it would add
a zero in front of it, and give a final result of 05674.
Feb 16 '06 #2
"gk245" <to*****@mail.com> writes:
int main (void)
{

int number;

printf ("Enter number: ");
scanf ("%i", &number);
printf ("%05i", number);

return 0;
}

So, if i enter a number like '00768', it should print out the same
'00768'. But its not doing that...its printing out 5 zeros
instead.


00768 does not have the value 768 when parsed by %i on scanf().
It has the value 62. This is because the leading 0 makes it an
octal constant. The trailing 8 is, I believe, ignored.

Use %d instead of %i to force scanf() to read your integer as a
decimal constant.
--
"For those who want to translate C to Pascal, it may be that a lobotomy
serves your needs better." --M. Ambuhl

"Here are the steps to create a C-to-Turbo-Pascal translator..." --H. Schildt
Feb 16 '06 #3
"gk245" <to*****@mail.com> writes:
gk245 explained :
#include <stdio.h>

int main (void)
{

int number;

printf ("Enter number: ");
scanf ("%i", &number);
printf ("%05i", number);

return 0;
}
[...]

woops, sorry, i meant if you typed in a number like 5674, it would add
a zero in front of it, and give a final result of 05674.


So: you want it to print out exactly what you typed in? Then
read it as a string and print it as a string. If you read and
print it as an integer, leading zeros won't be retained, because
they are not part of the value of an integer.
--
"Give me a couple of years and a large research grant,
and I'll give you a receipt." --Richard Heathfield
Feb 16 '06 #4
Ben Pfaff wrote on 2/16/2006 :
"gk245" <to*****@mail.com> writes:
int main (void)
{

int number;

printf ("Enter number: ");
scanf ("%i", &number);
printf ("%05i", number);

return 0;
}

So, if i enter a number like '00768', it should print out the same
'00768'. But its not doing that...its printing out 5 zeros
instead.


00768 does not have the value 768 when parsed by %i on scanf().
It has the value 62. This is because the leading 0 makes it an
octal constant. The trailing 8 is, I believe, ignored.

Use %d instead of %i to force scanf() to read your integer as a
decimal constant.


Alright, thx. It looks like i have to do it as a character then.
Feb 16 '06 #5

gk245 wrote:
I have this:

#include <stdio.h>

int main (void)
{

int number;

printf ("Enter number: ");
scanf ("%i", &number);
printf ("%05i", number);

return 0;
}

So, if i enter a number like '00768', it should print out the same
'00768'. But its not doing that...its printing out 5 zeros instead.
I have tried using %06d, %.5i, %.5d in the printf statement. None of
them work properly. I can't use any bigger functions, just printf to
do this. Thanks.


Inputting a number with leading zeroes, I guess, converts it to an
octal number. Easy way out here would be to read the number as a string
probably using fgets().

Feb 17 '06 #6
"Jaspreet" <js***********@gmail.com> writes:
gk245 wrote:
I have this:

#include <stdio.h>

int main (void)
{

int number;

printf ("Enter number: ");
scanf ("%i", &number);
printf ("%05i", number);

return 0;
}

So, if i enter a number like '00768', it should print out the same
'00768'. But its not doing that...its printing out 5 zeros instead.
I have tried using %06d, %.5i, %.5d in the printf statement. None of
them work properly. I can't use any bigger functions, just printf to
do this. Thanks.


Inputting a number with leading zeroes, I guess, converts it to an
octal number. Easy way out here would be to read the number as a string
probably using fgets().


Scanf's "%i" format expects a string in the same format expected by
strtol() with a base of 0, which means a number with a leading 0 is
treated as octal and a number with a leading 0x or 0X is treated as
hexadecimal. If you only want decimal input, use "%d". (Better yet,
don't use scanf(); use fgets() and parse the input line with
sscanf().)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 17 '06 #7
Keith Thompson wrote on 2/16/2006 :
"Jaspreet" <js***********@gmail.com> writes:
gk245 wrote:
I have this:

#include <stdio.h>

int main (void)
{

int number;

printf ("Enter number: ");
scanf ("%i", &number);
printf ("%05i", number);

return 0;
}

So, if i enter a number like '00768', it should print out the same
'00768'. But its not doing that...its printing out 5 zeros instead.
I have tried using %06d, %.5i, %.5d in the printf statement. None of
them work properly. I can't use any bigger functions, just printf to
do this. Thanks.


Inputting a number with leading zeroes, I guess, converts it to an
octal number. Easy way out here would be to read the number as a string
probably using fgets().


Scanf's "%i" format expects a string in the same format expected by
strtol() with a base of 0, which means a number with a leading 0 is
treated as octal and a number with a leading 0x or 0X is treated as
hexadecimal. If you only want decimal input, use "%d". (Better yet,
don't use scanf(); use fgets() and parse the input line with
sscanf().)


Yeah, the thing was i couldn't use functions like fgets() or sscanf().
Only allowed to use scanf() and printf(). I thought %d treated numbers
entered beginning with a 0 as ocatal too...trying to figure out what
the difference is between %d and %i.
Feb 18 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

30 posts views Thread by Martin Bless | last post: by
23 posts views Thread by stewart.midwinter | last post: by
6 posts views Thread by Wim van Rosmalen | last post: by
2 posts views Thread by jamesthiele.usenet | last post: by
2 posts views Thread by Yuriy | last post: by
2 posts views Thread by Phoe6 | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.