440,129 Members | 2,300 Online
Need help? Post your question and get tips & solutions from a community of 440,129 IT Pros & Developers. It's quick & easy.

# How do i set zero as a Integer?

 P: n/a I mean, anything that follows a 0 is automatically turned into a octal number. I want to have a integer variable that will hold numbers as integers even if they begin with a zero. for example: int 08453 should print 08453 if i tried to use printf() on it, and not some octal number. Thanks. Feb 10 '06 #1
33 Replies

 P: n/a gk245 a écrit : I mean, anything that follows a 0 is automatically turned into a octal number. Badly phrased. <> I want to have a integer variable that will hold numbers as integers even if they begin with a zero. for example: int 08453 This is not C. What do you meant ? int x = 08453; ? 'Invalid octal representation' int x = 8453; Is a valid decimal representation. should print 08453 if i tried to use printf() on it, and not some octal number. How is it important to you to have this leading 0 in a source code ? -- A+ Emmanuel Delahaye Feb 10 '06 #2

 P: n/a gk245 wrote: I mean, anything that follows a 0 is automatically turned into a octal number. I want to have a integer variable that will hold numbers as integers even if they begin with a zero. I'm not sure I understand what you think is the problem, as integer variables /always/ store values as integers, but let's try this: for example: int 08453 This has no meaning in C. Did you mean: int i = 08453; should print 08453 if i tried to use printf() on it, and not some octal number. In which case you should have written either of these: int i = 8453; int i = 020405; int i = 0x2105; An integer variable holds an integer /value/. This is different from integer /constants/ (literals) which can be represented in different bases. All the following are equivalent: int i = 07; int i = 0x7; int i = 7; They all assign value 7 (seven, as in "seven apples") to `i`. This means, when you print `i` using printf(), you get its /value/ out in whatever format you specify: printf("%d\n",i); /* decimal */ printf("%x\n",i); /* hex; it's /not/ preceeded by 0x, though */ ... So, you can have it any way you want it, really. -- BR, Vladimir Every improvement in communication makes the bore more terrible. -- Frank Moore Colby Feb 10 '06 #3

 P: n/a Vladimir S. Oka wrote: gk245 wrote: I mean, anything that follows a 0 is automatically turned into a octal number. I want to have a integer variable that will hold numbers as integers even if they begin with a zero. I'm not sure I understand what you think is the problem, as integer variables /always/ store values as integers, but let's try this: for example: int 08453 This has no meaning in C. Did you mean: int i = 08453; D'oh! You caught me out here! The above is not a valid octal number. Octal digits are [0..7] only. Everything else still holds, though... In which case you should have written either of these: int i = 8453; int i = 020405; int i = 0x2105; An integer variable holds an integer /value/. This is different from integer /constants/ (literals) which can be represented in different bases. All the following are equivalent: int i = 07; int i = 0x7; int i = 7; They all assign value 7 (seven, as in "seven apples") to `i`. This means, when you print `i` using printf(), you get its /value/ out in whatever format you specify: printf("%d\n",i); /* decimal */ printf("%x\n",i); /* hex; it's /not/ preceeded by 0x, though */ ... So, you can have it any way you want it, really. -- BR, Vladimir Another good night not to sleep in a eucalyptus tree. Feb 10 '06 #4

 P: n/a On Fri, 10 Feb 2006 13:17:34 -0500, Emmanuel Delahaye wrote: gk245 a écrit : I mean, anything that follows a 0 is automatically turned into a octal number. Badly phrased. <> I want to have a integer variable that will hold numbers as integers even if they begin with a zero. for example: int 08453 This is not C. What do you meant ? int x = 08453; ? 'Invalid octal representation' int x = 8453; Is a valid decimal representation. should print 08453 if i tried to use printf() on it, and not some octal number. How is it important to you to have this leading 0 in a source code ? Sorry, i meant int x = 08453; Well, say you want the user to enter a number that starts with 0, then have that number displayed properly using printf(). Is that not something a variable set to integer would do? Or should i be using char instead? Ofcourse, you couldn't do any calculations with char in the future if you wanted to, so i was thinking it was better to leave it as a integer. Is there a way, then, to truncate the leading zero when a user enters a zero? for example, like this: printf("Enter Integer: \n"); scanf("%i\n", &var1); Now, what if the user entered a few zeros before entering a Integer? I can't use if-else, since its not allowed yet for me to use. Feb 10 '06 #5

 P: n/a gk245 a écrit : Sorry, i meant int x = 08453; Well, say you want the user to enter a number that starts with 0, then have that number displayed properly using printf(). This is completely different. Use fgets() and strtol() or strtoul(). If you choose the base (say 10), the leading zeros will not be interpreted a an octal representation heading, and the conversion will succed. -- A+ Emmanuel Delahaye Feb 10 '06 #6

 P: n/a gk245 wrote On 02/10/06 13:04,: I mean, anything that follows a 0 is automatically turned into a octal number. I want to have a integer variable that will hold numbers as integers even if they begin with a zero. for example: int 08453 should print 08453 if i tried to use printf() on it, and not some octal number. You're mixing up two different things. First, an integer variable just holds integer values. As it happens, C requires integer variables to use binary representation, but that's very nearly irrelevant: if it were not for a few operators like ^ and >>, whose behavior is difficult to describe in base three, say, there would be no need for C to have such a requirement. If you're not using these "bit-defined" operations, you can (and, I'd say, should) simply forget about the number base in most instances. So: an integer variable holds integer values, not decimal values or octal values. Second, C programs use various source-code notations that allow the programmer to specify values. There are different notations for different types of value (for example, "abc" is the notation for a string value, 1.5 is the notation for a double value). Some types have more than one notation: 1.5 and 0.15e1 and 15e-1 are different ways of writing one-and-a-half as a double constant. For integers there are three ways[*] to denote an integer constant: in decimal, octal, or hexadecimal base. (Actually, there are a few other ways. Without any intent to ridicule you, I suspect you may not be ready to learn about them yet and I'd prefer not to add to your confusion just now.) The important thing to note is that the form of the notation is part of the C language. When you start a number with a zero digit, you have announced to the compiler that you intend to use octal notation to express the desired value. Start it with zero and X and you declare that you're using hexadecimal. Start it with a non-zero digit and you say you're using decimal. That's it, the unalterable It: it's the convention of C source code notations. The conventions are in a sense arbitrary (some other languages, for example, use notations like 'X'C and 'O'14 -- I have even seen twelve written as "(2)300", the double quotes being part of the notation). However, once the conventions are chosen they cannot be changed. They exist as a means of communicating your intent to the compiler; if you say one thing but mean another, the compiler will fail to understand you properly. Finally, observe that these conventions apply only in C source code (and to a few library functions). There is no rule saying that the numbers you print must follow the same conventions that C source does. For example, try printf("%05d\n", 8453) and see if you like what you get. -- Er*********@sun.com Feb 10 '06 #7

 P: n/a On Fri, 10 Feb 2006 13:31:57 -0500, Vladimir S. Oka wrote: Vladimir S. Oka wrote: gk245 wrote: I mean, anything that follows a 0 is automatically turned into a octal number. I want to have a integer variable that will hold numbers as integers even if they begin with a zero. I'm not sure I understand what you think is the problem, as integer variables /always/ store values as integers, but let's try this: for example: int 08453 This has no meaning in C. Did you mean: int i = 08453; D'oh! You caught me out here! The above is not a valid octal number. Octal digits are [0..7] only. Everything else still holds, though... In which case you should have written either of these: int i = 8453; int i = 020405; int i = 0x2105; An integer variable holds an integer /value/. This is different from integer /constants/ (literals) which can be represented in different bases. All the following are equivalent: int i = 07; int i = 0x7; int i = 7; They all assign value 7 (seven, as in "seven apples") to `i`. This means, when you print `i` using printf(), you get its /value/ out in whatever format you specify: printf("%d\n",i); /* decimal */ printf("%x\n",i); /* hex; it's /not/ preceeded by 0x, though */ ... So, you can have it any way you want it, really. I just found something out...you used %d, and i was using %i. For some reason or other, %d displays the integer after truncating the leading zeros. %i doesn't...if you feed it anything beginning with a 0, it turns the output into. So, i had this: printf("%i\n",i); Changing it to: printf("%d\n",i); Works wonderfully. Thanks. Not sure why %i doesn't work. Feb 10 '06 #8

 P: n/a gk245 wrote: Sorry, i meant int x = 08453; Why are you interested in octal numbers, and how do you plan to deal with them, if you don't know they can't have `8` in them? Well, say you want the user to enter a number that starts with 0, then have that number displayed properly using printf(). Is that not something a variable set to integer would do? Or should i be using char instead? Ofcourse, you couldn't do any calculations with char in the future if you wanted to, so i was thinking it was better to leave it as a integer. If you know what base your user will use, you can use appropriate format in scanf(): %d for decimal, %o for octal, and %x for hex. Is there a way, then, to truncate the leading zero when a user enters a zero? You cold get user input as a string (NB, do /not/ use scanf() for this, as it can overrun space you allocate for the string -- rather, use fgets()/sscanf(); for the same reason do /not/ use gets()). Once you have that, you can parse that string to your heart's content. for example, like this: printf("Enter Integer: \n"); scanf("%i\n", &var1); ("%i" is rarely used these days) Now, what if the user entered a few zeros before entering a Integer? I can't use if-else, since its not allowed yet for me to use. As you've written it, the number will still be interpreted as decimal. It's the format (conversion) specifier in scanf() that matters. -- BR, Vladimir Another good night not to sleep in a eucalyptus tree. Feb 10 '06 #9

 P: n/a gk245 wrote: I just found something out...you used %d, and i was using %i. For some reason or other, %d displays the integer after truncating the leading zeros. %i doesn't...if you feed it anything beginning with a 0, it turns the output into. So, i had this: printf("%i\n",i); Changing it to: printf("%d\n",i); Works wonderfully. Thanks. Not sure why %i doesn't work. I don't understand: %i should be the same as %d for printf(). It /is/ different for scanf() (at least on my gcc/libc; couldn't find it in C99 -- any takers?), and it'll give you the behaviour you say don't like (i.e. it'll interpret 0nnn as octal, and 0xnnn as hex). One lives and learns... -- BR, Vladimir In seeking the unattainable, simplicity only gets in the way. -- Epigrams in Programming, ACM SIGPLAN Sept. 1982 Feb 10 '06 #10

 P: n/a Emmanuel Delahaye wrote: gk245 a écrit : Sorry, i meant int x > Well, say you want the user to enter a number that starts with 0, then have that number displayed properly using printf(). This is completely different. Use fgets() and strtol() or strtoul(). If you choose the base (say 10), the leading zeros will not be interpreted a an octal representation heading, and the conversion will succed. -- A+ Emmanuel Delahaye Or try scanf with "%d" instead of "%i". Feb 10 '06 #11

 P: n/a gk245 a écrit : Not sure why %i doesn't work. It works, but differently. actually scanf() with "%i", like strto[u]l() with base 0, interprets the string like in a C-source (handy if you are writing a c-compiler!). if 0x hexa else if 0 octal else decimal endif Why don't you read a C-book ? It's well explained... -- A+ Emmanuel Delahaye Feb 10 '06 #12

 P: n/a tmp123 a écrit : Or try scanf with "%d" instead of "%i". Use scanf() and die.. -- A+ Emmanuel Delahaye Feb 10 '06 #13

 P: n/a Yeah, i was using scanf("%i" &var1); to get the input from the user, then i passed that to printf("%i", var1);. This causes the wrong behaviour if the input from the user to scanf() begins with 0's. Not sure what printf() was putting out with %i, but i had assumed it was octals. Guess %i might be obsolete..or the compiler just doesn't like it (MinGW). "Vladimir S. Oka" wrote in message news:ds**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com... gk245 wrote: I just found something out...you used %d, and i was using %i. For some reason or other, %d displays the integer after truncating the leading zeros. %i doesn't...if you feed it anything beginning with a 0, it turns the output into. So, i had this: printf("%i\n",i); Changing it to: printf("%d\n",i); Works wonderfully. Thanks. Not sure why %i doesn't work. I don't understand: %i should be the same as %d for printf(). It /is/ different for scanf() (at least on my gcc/libc; couldn't find it in C99 -- any takers?), and it'll give you the behaviour you say don't like (i.e. it'll interpret 0nnn as octal, and 0xnnn as hex). One lives and learns... -- BR, Vladimir In seeking the unattainable, simplicity only gets in the way. -- Epigrams in Programming, ACM SIGPLAN Sept. 1982 Feb 10 '06 #14

 P: n/a gk245 a écrit : Not sure what printf() was putting out with %i, but i had assumed it was octals. Guess %i might be obsolete..or the compiler just doesn't like it (MinGW). %i and %d with printf() and an int have exactly the same behaviour. -- A+ Emmanuel Delahaye Feb 10 '06 #15

 P: n/a Vladimir S. Oka wrote: Vladimir S. Oka wrote: gk245 wrote: for example: int 08453 This has no meaning in C. Did you mean: int i = 08453; D'oh! You caught me out here! The above is not a valid octal number. Octal digits are [0..7] only. Everything else still holds, though... Actually as a historical odity, if you use K&R C then 8 *is* a valid octal digit. To quote from Appenx C: Summary of Changes of K&R2 (top of page 260 in my copy), "Everyone's favourite trivial change: 8 and 9 are not octal digits." -- Flash Gordon Living in interesting times. Although my email address says spam, it is real and I read it. Feb 10 '06 #16

 P: n/a gk245 wrote: Yeah, i was using scanf("%i" &var1); to get the input from the user, then i passed that to printf("%i", var1);. This causes the wrong behaviour if the input from the user to scanf() begins with 0's. Not sure what printf() was putting out with %i, but i had assumed it was octals. Guess %i might be obsolete..or the compiler just doesn't like it (MinGW). Please don't top-post... No, it's not wrong. It's just as it's supposed to be. Using %d or %i with printf() should produce the same result, AFAIK (for octal use %o, for hex %h). It's the scanf() that behaves differently for the two (do heed all the warnings you got for not using scanf(), though). -- BR, Vladimir To iterate is human, to recurse, divine. Feb 10 '06 #17

 P: n/a gk245 wrote: On Fri, 10 Feb 2006 13:17:34 -0500, Emmanuel Delahaye wrote: gk245 a écrit : I mean, anything that follows a 0 is automatically turned into a octal number. Badly phrased. <> I want to have a integer variable that will hold numbers as integers even if they begin with a zero. for example: int 08453 This is not C. What do you meant ? int x = 08453; ? 'Invalid octal representation' int x = 8453; Is a valid decimal representation. should print 08453 if i tried to use printf() on it, and not some octal number. How is it important to you to have this leading 0 in a source code ? Sorry, i meant int x = 08453; Well, say you want the user to enter a number that starts with 0, then have that number displayed properly using printf(). Is that not something a variable set to integer would do? Or should i be using char instead? Ofcourse, you couldn't do any calculations with char in the future if you wanted to, so i was thinking it was better to leave it as a integer. Is there a way, then, to truncate the leading zero when a user enters a zero? for example, like this: printf("Enter Integer: \n"); scanf("%i\n", &var1); Now, what if the user entered a few zeros before entering a Integer? I can't use if-else, since its not allowed yet for me to use. %i reads an int * in base 0, meaning that (strtol) 0xABC reads a hexa number and 0ABC reads an octal number and anything else reads a base 10 number. The leading 0 has no meaning for an integer number. If you want to use int as a code for something that should allow 0 as the leading digit then you can't do that. You should store the digits as char *. -- Ioan - Ciprian Tandau tandau _at_ freeshell _dot_ org (hope it's not too late) (... and that it still works...) Feb 10 '06 #18

 P: n/a Flash Gordon wrote: Vladimir S. Oka wrote: Vladimir S. Oka wrote: gk245 wrote: for example: int 08453 This has no meaning in C. Did you mean: int i = 08453; D'oh! You caught me out here! The above is not a valid octal number. Octal digits are [0..7] only. Everything else still holds, though... Actually as a historical odity, if you use K&R C then 8 *is* a valid octal digit. To quote from Appenx C: Summary of Changes of K&R2 (top of page 260 in my copy), "Everyone's favourite trivial change: 8 and 9 are not octal digits." That's interesting! Unfortunately, I don't have a copy of K&R1, and (probably for that reason) have never bother reading Appendix C in K&R2. And what values did 8 and 9 yield according to K&R1? -- BR, Vladimir Any philosophy that can be put in a nutshell belongs there. -- Sydney J. Harris Feb 10 '06 #19

 P: n/a On Fri, 10 Feb 2006 13:42:16 -0500, Eric Sosman wrote: gk245 wrote On 02/10/06 13:04,: I mean, anything that follows a 0 is automatically turned into a octal number. I want to have a integer variable that will hold numbers as integers even if they begin with a zero. for example: int 08453 should print 08453 if i tried to use printf() on it, and not some octal number. You're mixing up two different things. First, an integer variable just holds integer values. As it happens, C requires integer variables to use binary representation, but that's very nearly irrelevant: if it were not for a few operators like ^ and >>, whose behavior is difficult to describe in base three, say, there would be no need for C to have such a requirement. If you're not using these "bit-defined" operations, you can (and, I'd say, should) simply forget about the number base in most instances. So: an integer variable holds integer values, not decimal values or octal values. Second, C programs use various source-code notations that allow the programmer to specify values. There are different notations for different types of value (for example, "abc" is the notation for a string value, 1.5 is the notation for a double value). Some types have more than one notation: 1.5 and 0.15e1 and 15e-1 are different ways of writing one-and-a-half as a double constant. For integers there are three ways[*] to denote an integer constant: in decimal, octal, or hexadecimal base. (Actually, there are a few other ways. Without any intent to ridicule you, I suspect you may not be ready to learn about them yet and I'd prefer not to add to your confusion just now.) The important thing to note is that the form of the notation is part of the C language. When you start a number with a zero digit, you have announced to the compiler that you intend to use octal notation to express the desired value. Start it with zero and X and you declare that you're using hexadecimal. Start it with a non-zero digit and you say you're using decimal. That's it, the unalterable It: it's the convention of C source code notations. The conventions are in a sense arbitrary (some other languages, for example, use notations like 'X'C and 'O'14 -- I have even seen twelve written as "(2)300", the double quotes being part of the notation). However, once the conventions are chosen they cannot be changed. They exist as a means of communicating your intent to the compiler; if you say one thing but mean another, the compiler will fail to understand you properly. Finally, observe that these conventions apply only in C source code (and to a few library functions). There is no rule saying that the numbers you print must follow the same conventions that C source does. For example, try printf("%05d\n", 8453) and see if you like what you get. Wow, thx for the big explanation. Feb 10 '06 #20

 P: n/a A little off-topic question: Should i be using 'Reply-All' or 'Reply' in these groups? I know that i shouldn't top-post, so might as well know which reply button to use. Thx. Feb 10 '06 #21

 P: n/a Vladimir S. Oka wrote: Flash Gordon wrote: Actually as a historical odity, if you use K&R C then 8 *is* a valid octal digit. To quote from Appenx C: Summary of Changes of K&R2 (top of page 260 in my copy), "Everyone's favourite trivial change: 8 and 9 are not octal digits." That's interesting! Unfortunately, I don't have a copy of K&R1, and (probably for that reason) have never bother reading Appendix C in K&R2. And what values did 8 and 9 yield according to K&R1? I don't know, I don't have a copy of K&R1. -- Flash Gordon Living in interesting times. Although my email address says spam, it is real and I read it. Feb 10 '06 #24

 P: n/a On 2006-02-10, gk245 wrote: for example, like this: printf("Enter Integer: \n"); scanf("%i\n", &var1); Now, what if the user entered a few zeros before entering a Integer? I can't use if-else, since its not allowed yet for me to use. You could use %d instead of %i. Feb 10 '06 #25

 P: n/a On 2006-02-10, Vladimir S. Oka wrote: Flash Gordon wrote: Vladimir S. Oka wrote: Vladimir S. Oka wrote: gk245 wrote: > for example:>> int 08453 This has no meaning in C. Did you mean: int i = 08453; D'oh! You caught me out here! The above is not a valid octal number. Octal digits are [0..7] only. Everything else still holds, though... Actually as a historical odity, if you use K&R C then 8 *is* a valid octal digit. To quote from Appenx C: Summary of Changes of K&R2 (top of page 260 in my copy), "Everyone's favourite trivial change: 8 and 9 are not octal digits." That's interesting! Unfortunately, I don't have a copy of K&R1, and (probably for that reason) have never bother reading Appendix C in K&R2. And what values did 8 and 9 yield according to K&R1? I would assume 8 [010] and 9 [011] times the appropriate power of eight [010]. Feb 10 '06 #27

 P: n/a Jordan Abel wrote: On 2006-02-10, Vladimir S. Oka wrote: Flash Gordon wrote: To quote from Appenx C: Summary of Changes of K&R2 (top of page 260 in my copy), "Everyone's favourite trivial change: 8 and 9 are not octal digits." That's interesting! Unfortunately, I don't have a copy of K&R1, and (probably for that reason) have never bother reading Appendix C in K&R2. And what values did 8 and 9 yield according to K&R1? I would assume 8 [010] and 9 [011] times the appropriate power of eight [010]. Yes, that seems logical. I couldn't think about any other either, but still though I'd ask. -- BR, Vladimir If you are over 80 years old and accompanied by your parents, we will cash your check. Feb 10 '06 #30

 P: n/a On 2006-02-10, Vladimir S. Oka wrote: Jordan Abel wrote: On 2006-02-10, Vladimir S. Oka wrote: Flash Gordon wrote: To quote from Appenx C: Summary of Changes of K&R2 (top of page 260 in my copy), "Everyone's favourite trivial change: 8 and 9 are not octal digits." That's interesting! Unfortunately, I don't have a copy of K&R1, and (probably for that reason) have never bother reading Appendix C in K&R2. And what values did 8 and 9 yield according to K&R1? I would assume 8 [010] and 9 [011] times the appropriate power of eight [010]. Yes, that seems logical. I couldn't think about any other either, but still though I'd ask. Could be 7 like in bc Feb 10 '06 #31