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 ??
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.
"The Internet treats censorship as damage and routes around it." - John Gilmore
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).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*?See the first line on pg 193, K&R 2nd Edition
"The Internet treats censorship as damage and routes around it." - John Gilmore
It would have to be this (if I remmember correctly):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);
Last edited by King Mir; 10-13-2006 at 09:21 AM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
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; }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
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);