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. 7 8889
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.
"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
"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
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.
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().
"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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Martin Bless |
last post by:
Why can't we have an additional 'print' statement, that behaves
exactly like 'print' but doesn't insert that damn blank between two
arguments?
Could be called 'printn' or 'prin1' or 'prinn'...
|
by: stewart.midwinter |
last post by:
No doubt I've overlooked something obvious, but here goes:
Let's say I assign a value to a var, e.g.:
myPlace = 'right here'
myTime = 'right now'
Now let's say I want to print out the two...
|
by: Wim van Rosmalen |
last post by:
I've upgraded MS-Access 2002 to a MS-Access Project (adp), so now I have to
deal with more sophisticated queries (may I call them so?) like stored
procedures. I have a form with a combobox for...
|
by: jamesthiele.usenet |
last post by:
I recently ran into the issue with 'print' were, as it says on the web
page called "Python Gotchas"
(http://www.ferg.org/projects/python_gotchas.html):
The Python Language Reference Manual says,...
|
by: Randy |
last post by:
Hello all,
I'm trying to print using PrintPreviewDialog. What's happening is that the
PrintPreviewDialog shows the correct information to be printed, but when I
click the print icon, it prints a...
|
by: Yuriy |
last post by:
Hello! I need to print PDF Document by clicking a button
on .aspx page. The PDF should be printed on client printer
without displaing it on the window. I tried Process
namespace but it doesn't...
|
by: hosi |
last post by:
Hi, suppose I want to print third record from a subform.
The problem is, that when I want to print what I see on my monitor
(third record), the print preview resets
the form to the first record...
|
by: Phoe6 |
last post by:
print and softspace in python
In python, whenever you use >>>print statement it will append a
newline by default. If you don't want newline to be appended, you got
use a comma at the end (>>>print...
|
by: Gord |
last post by:
When I open a certain report, it runs some code that generates the records
that will be displayed in that report. This works fine. When I go to print
preview the report it appears that the code...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |