Thread: What does this line do?

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122

    What does this line do?

    Hi, just reading through some code and got a little stuck as to the format in which this value is writing to a string...

    Code:
    char string[128];
    int i;
    
    for (i=0; i<10;i++){
      printf("%08X\n", i);
    }
    I don't get what the output would be when doing %08x rather than just %x, and I get that by doing %x, you would print the hexidecimal value for at each loop.

    Thanks!
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    %08x means "fill with zeros to make it 8 digits, in hex".

    Similarly, you can use %8x, in which case it's filled with spaces instead of zeros, and %-8x would mean "fill with spaces AFTER the number to make it 8 positions". I don't think "%-08x" does anything sensible. (That is, it's probably no different from "%08x" - but it may behave strangely different from bugginess in any given imlementation).

    --
    Mats

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >I don't get what the output would be
    Run it and take a look then. Really. It's just a printf statement.

  4. #4
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    Ok... so if i = 2, would output be:

    20000000

    or would it be

    00000002
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bobthebullet990 View Post
    Ok... so if i = 2, would output be:

    20000000

    or would it be

    00000002
    What do you think makes most sense (try it and see if you are right. you may want to count a tiny bit higher than 9 to make it show that it's hex!)

    --
    Mats

  6. #6
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    I would love to try it! ...would have saved me having to post, but I'm on a windows machine with no C compiler!!!!
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  7. #7
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    Well.. Im guessing it would pad before the digit! as padding after would be stupid! ...So i'm guessing that output would be:

    00000000
    ...
    00000009
    0000000A
    0000000B
    ....
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by bobthebullet990 View Post
    I would love to try it! ...would have saved me having to post, but I'm on a windows machine with no C compiler!!!!
    http://www.compilers.net/Dir/Free/Compilers/CCpp.htm
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to do encryption in C
    By sankarv in forum C Programming
    Replies: 33
    Last Post: 12-28-2010, 11:01 AM
  2. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-18-2009, 11:44 PM
  3. Pointer and Polymorphism help.
    By Skyy in forum C++ Programming
    Replies: 29
    Last Post: 12-18-2008, 09:17 PM
  4. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  5. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 05:44 PM