Thread: Help with understanding snippet

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    6

    Help with understanding snippet

    Hi all.

    I am new to C and am trying to get to learn this programming language.

    I came across this peice of code and would like anyone of you to explain this to me:

    Code:
    int main() {
    char bbees[10], style[35], buf[35];
    int bbb = 16;
    int ccc = 0;
    sprintf(bbees, "%08x", &bbb);
    printf(bbees);
    printf("\n");
    sprintf(buf, "\\x%c%c\\x%c%c\\x%c%c\\x%c%c", bbees[6],
    bbees[7], bbees[4], bbees[5], bbees[2], bbees[3], bbees[0],
    bbees[1]);
    strcpy(style, buf);
    sprintf(buf,"%%08x.%%08x%%n");
    strcat(style, buf);
    printf("%s\n", style);
    printf(style, &bbb, &bbb, &ccc);
    }
    I would like to know what these lines mean:

    sprintf(buf, "\\x%c%c\\x%c%c\\x%c%c\\x%c%c", bbees[6],
    bbees[7], bbees[4], bbees[5], bbees[2], bbees[3], bbees[0],
    bbees[1]);


    sprintf(buf,"%%08x.%%08x%%n");

    printf(style, &bbb, &bbb, &ccc);

    If anyone can help, I am glad.

    THanks you

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the man page: sprintf


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by narendras View Post
    Hi all.

    I am new to C and am trying to get to learn this programming language.
    I would like to know what these lines mean:
    1) Get yourself a good tutorial and/or book and work through them page by page. You'll learn the language... not somebody's idea of a joke.

    2) You need to acquire and use the library documentation for your compiler. It's an invaluable resource no programmer should be without. Then you can look this stuff up for yourself.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by quzah View Post
    Read the man page: sprintf


    Quzah.
    Hi quzah,

    Thanks for your reply to me. I watched the page you linked me to and learned that sprintf is used to write to a character string. However what I cannot understand is what would this part of the line do:


    \\x%c%c\\x%c%c\\x%c%c\\x%c%c"

    Are you able to help me to understand this line please?

    Thanks you

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you read the sprintf() and/or printf() documentation you should be able to take that apart to figure out what it does...

    What does %c do?
    what does \\ do?

    It's not that hard...

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by CommonTater View Post
    If you read the sprintf() and/or printf() documentation you should be able to take that apart to figure out what it does...

    What does %c do?
    what does \\ do?

    It's not that hard...
    Hi Tater thanks for your message. Is there some documentation somewhere that I can look at? On the link that quzah sent me there is no mention of what those two items do

    What i think I learned is that %c means that the output is a character. But i have not learned what \\ does

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    23
    x Unsigned hexadecimal integer

    \x

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    \ is called an escape character and it is used to indicate the next character should be taken literally in the context of the string. It is used with characters that would otherwise indicate something special, eg:

    Code:
    puts("quote \"this\"");
    Which means that \ itself is a special character, so to render an actual \ in a C string you need to escape it, ie:

    Code:
    puts("Single backslash: \\");
    Quote Originally Posted by tahmod View Post
    x Unsigned hexadecimal integer

    \x
    Ah, but
    1) printf notation does not use \x, it uses %x.
    2) in the example it is actually "\\x", which will display "\x" literally.
    Last edited by MK27; 01-07-2012 at 09:56 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by tahmod View Post
    x Unsigned hexadecimal integer

    \x
    Hi Tahmod, thanks you for your message.

    I saw this: FAQ > Format output using printf() (C) - Cprogramming.com there are the formats. However there no is the \\ in the above code. Do you understand if that means something or not?

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by narendras View Post
    However there no is the \\ in the above code. Do you understand if that means something or not?
    Read the last part of post #8, I edited that in.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by MK27 View Post
    Read the last part of post #8, I edited that in.
    Thanks you MK27! That makes good sense

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    MK27, would the below line:
    printf(style, &bbb, &bbb, &ccc);

    Output the value of style, and the memory location of bbb, bbb and ccc?

    Thanks you

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by narendras View Post
    MK27, would the below line:
    printf(style, &bbb, &bbb, &ccc);

    Output the value of style, and the memory location of bbb, bbb and ccc?

    Thanks you
    Yep, altho you need an actual format string in there first.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by MK27 View Post
    Yep, altho you need an actual format string in there first.
    I think style is a dynamically-created format string.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by narendras View Post
    Hi Tater thanks for your message. Is there some documentation somewhere that I can look at? On the link that quzah sent me there is no mention of what those two items do

    What i think I learned is that %c means that the output is a character. But i have not learned what \\ does
    1) Yes the link does explain these things... I looked. Next time *actually read* what's there.

    2) Any half decent tutorial on C will tell you how to use printf and scanf almost right away... so you should go do some reading.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I adapt a snippet to this program?
    By mbomb007 in forum C Programming
    Replies: 7
    Last Post: 10-21-2009, 04:20 PM
  2. C++ snippet Archive
    By squako in forum C++ Programming
    Replies: 16
    Last Post: 12-17-2006, 12:12 PM
  3. C code snippet which I need help understanding
    By c_me in forum C Programming
    Replies: 3
    Last Post: 07-28-2006, 06:06 AM
  4. Snippet ascii->hex->binary
    By bikr692002 in forum C++ Programming
    Replies: 8
    Last Post: 02-09-2006, 03:09 PM
  5. a great little code snippet...
    By skeptik in forum Windows Programming
    Replies: 1
    Last Post: 09-24-2002, 11:45 PM