1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

help

Discussion in 'C' started by IndiraP, Nov 26, 2012.

  1. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    #include <stdio.h>
    char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";
    main()
    {
    printf(s,34,s,34);
    }

    outputs..
    char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";main(){printf(s,34,s,34);}";

    how does this work???
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    It's all in the printf format string. The first parameter to printf is s, the string itself.
    So this is equivalent to:
    Code:
    #include <stdio.h>
    char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";
    main()
    {
    printf("char*s=%c%s%c;main(){printf(s,34,s,34);}",34,s,34);
    }
    
    Now just match up the %-codes with the parameters:
    %c=>34
    %s=>s again
    %c=>34.

    34 is the ASCII code for ".
     
  3. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    ok..then for first %s...char*s=%c%s%c;main(){printf(s,34,s,34);} is printed..
    then for %c .. " is printed... then
    for the next %s..y only.. main(){printf(s,34,s,34);} is printed instead of entire char *s???
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Sorry, don't understand. What output are you expecting and why?
     
  5. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    i expect the below to be output..
    char*s=%c%s%c;main(){printf(s,34,s,34);}"char*s=%c%s%c;main(){printf(s,34,s,34);}"
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    What do you think the following code will display?
    Code:
    #include <stdio.h>
    char*s="AAA %c %s %c BBB";
    main()
    {
    printf(s,'\"',s,'\"');
    }
    
     
  7. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    i expect this..
    AAA %c %s %c BBB" AAA %c %s %c BBB"
     
  8. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    got it...
    s indicates... AAA %c %s %c BBB

    so AAA " AAA %c %s %c BBB "

    %c mapped to " n %s mapped to AAA %c %s %c BBB..
    rite??
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Oh I see. You're thinking that:
    Code:
    printf("A","B","C");
    
    will print ABC, right? It won't. The first parameter to printf is called a format string. It contains text and symbols that indicate the meaning of the parameters that follow. So %c is a character, %s is a string, %d is a number, and there are lots more, so the code:
    Code:
    printf("Character:%c;  String:%s;  Number:%d\n", 'f', "Hello world", 27);
    
    will display:
    Character:f; String:Hello world; Number:27
    and the \n means end of line.

    So if we look at my second bit of code:
    Code:
    char*s="AAA %c %s %c BBB";
    main()
    {
    printf(s,'\"',s,'\"');
    }
    
    printf will interpret the first parameter to mean:
    "AAA ";
    then a character because of %c;
    then a string because of %s;
    then another character because of %c;
    then " BBB".

    The %c, %s and %c match to '\"', s itself, and '\"' respectively, so the output will be:
    "AAA ";
    then '\"' because of %c;
    then "AAA %c %s %c BBB" because of %s;
    then '\"' because of %c;
    then " BBB".

    which in total will be:

    AAA "AAA %c %s %c BBB" BBB".

    It's a bit confusing because of the double usage of s. Let's specify the format string literally, and use "sausage" instead of duplicating the format string, and X instead of that escaped double quote.
    Code:
    char*s="sausage";
    main()
    {
    printf("AAA %c %s %c BBB",'X',s,'X');
    }
    
    The output of this will be: AAA X sausage X BBB. Clear?
     
    IndiraP likes this.
  10. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    ya...u r wonderful sir...:)
    thank u sir..i got it well...:)
     

Share This Page