473,569 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9092
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.c om> 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.c om> 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.c om> 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_Keit h) 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
4139
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' anything similar you like. Would make my life so much easier. Often I start with a nice and short print statement, then there happen to be more...
23
63637
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 vars, along with their names. I could easily do this: print "myPlace = %s, myTime = %s" % (myPlace, myTime)
6
2315
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 selections and a textbox to enter a certain value. Let us say I call the combobox @select and the textbox @find. The combobox always shows the first of...
2
5121
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, about the print statement, A "\n" character is written at the end, unless the print statement ends with a comma. What it doesn't say is that...
2
5854
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 blank page. If I comment out the printPreviewDialog1.ShowDialog(); and just do a printDocument1.Print(); instead, it prints all the pages like a...
2
1934
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 work. Please, help! Thank you.
1
1906
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 (wants to print the whole form beginning). Is the any possibility to print a particular record, without making screenshots? Please, help me!!!
2
10998
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 10,) When, you have a list of characters and want them to be printed together a string using a for loop, there was observation that no matter what...
11
7301
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 is run again? This is causing certain error problems. 1 Why does the code run for a print preview when the report already exists? 2 What...
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.