why is the printed result:Code:
int test = 012452;
printf("\n%d", test);
5418
nb: it happens to ints that have a 0 as the first digit ??
Printable View
why is the printed result:Code:
int test = 012452;
printf("\n%d", test);
5418
nb: it happens to ints that have a 0 as the first digit ??
Zero is false, so in some terms it is skipped, I use C++, so I could be wrong
I think 0 in the front means the number is counted as octal.
is the only way of getting around it is to make test a string? or can i keep as an int and somehow manipulate it?
Yes, the 0 prefix denotes that the number is represented in the octal number system (See the first line on pg 193, K&R 2nd Edition).Quote:
Originally Posted by Axel
In printf, %d means decimal integer, so it's printing it in the decimal format.
Try printing it in octal form using
Code:printf("\n%o", test);
hmm this time it omits the 0..
12345Code:int test = 012345;
printf("\n%o", test);
WT*?Quote:
See the first line on pg 193, K&R 2nd Edition
It would have to be this (if I remmember correctly):Quote:
Originally Posted by Axel
But I'm not sure if you want to limmit test to just having digits 0-7.Code:int test = 012345;
printf("\n%06o", test);
Or
Code:int main()
{
int test = 012345;
printf("\n%#o", test);
return(0);
}
Or:
If you don't know how many zeros precede the number then you're stuck with using a string representation like my sample above.Code:#include <stdio.h>
int main(void) {
const char *test = "0123456";
puts(test);
return 0;
}
Axel: you know what octal is?
King Mir: it is just an integer. If it's in octal, then its digits are [0-7]. If decimal, then [0-9]. If hex then [0-9a-f]. It's the same integer in either case. Digital representation has nothing to do with the values an integer can take on.
that's saying, hey, here, i have a number in octal and on the printf, please represent it in decimaland by any chance, 5418 is decimal for 12452 in octalCode:printf("%d", test);
:D