Thread: int wrong value being printed?

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    Unhappy int wrong value being printed?

    Code:
    int test = 012452;
    printf("\n%d", test);
    why is the printed result:

    5418

    nb: it happens to ints that have a 0 as the first digit ??

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Zero is false, so in some terms it is skipped, I use C++, so I could be wrong

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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

  4. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    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?

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by Axel
    nb: it happens to ints that have a 0 as the first digit ??
    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).

    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);

  6. #6
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    hmm this time it omits the 0..
    Code:
    int test =  012345;
    printf("\n%o", test);
    12345

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    See the first line on pg 193, K&R 2nd Edition
    WT*?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Axel
    hmm this time it omits the 0..
    Code:
    int test =  012345;
    printf("\n%o", test);
    12345
    It would have to be this (if I remmember correctly):
    Code:
    int test =  012345;
    printf("\n%06o", test);
    But I'm not sure if you want to limmit test to just having digits 0-7.
    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.

  9. #9
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Or
    Code:
    int main()
    {
        int test =  012345;
        printf("\n%#o", test);
        return(0);
    }

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or:
    Code:
    #include <stdio.h>
    
    int main(void) {
        const char *test = "0123456";
        puts(test);
        return 0;
    }
    If you don't know how many zeros precede the number then you're stuck with using a string representation like my sample above.
    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.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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;}

  12. #12
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    that's saying, hey, here, i have a number in octal and on the printf, please represent it in decimal
    Code:
    printf("%d", test);
    and by any chance, 5418 is decimal for 12452 in octal


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM