Printf Problem

Discussion in 'C Programming' started by Ico, Aug 20, 2006.

  1. Ico

    Ico Guest

    wrote:

    > Hi.I could not understand the following printf statement.Thanks for any
    > help.
    >
    > int a=3,b=5;
    > printf(&a["Ya!Hello!how is this?%s],&b["junk/super]);


    Neither does my compiler. If you post any code, please make sure it
    compiles. I will assume you missed some quotes and ment :

    printf(&a["Ya!Hello!how is this?%s"],&b["junk/super"]);

    This is an old trick, not to be used in real-life code. Due to the way
    arrays an pointers work in the C language, the following two lines
    have the same effect:

    "hello"[2]
    2["hello"]

    Both expressions return 'l'. If I recall correctly, the clc faq has a
    detailed explanation why this is the case.

    > I think a equals to*[a+i].But I could not understand the use of &


    In your example, the expression

    &a["Ya!Hello!how is this?%s"]

    is the same as

    &"Ya!Hello!how is this?%s"[a]

    A was set to 3 before, so this expression returns the address of the
    string+3, which happens to be "Hello!how is this?%s"

    The same happens with the second expression, which is used as an
    argument for the %s directive in the printf format string.


    --
    :wq
    ^X^Cy^K^X^C^C^C^C
    Ico, Aug 20, 2006
    #1
    1. Advertising

  2. Ico

    Guest

    Hi.I could not understand the following printf statement.Thanks for any
    help.

    int a=3,b=5;
    printf(&a["Ya!Hello!how is this?%s],&b["junk/super]);

    I think a equals to*[a+i].But I could not understand the use of &
    here.Bye.
    Regards,
    Eric
    , Aug 20, 2006
    #2
    1. Advertising

  3. Ico

    steve Guest

    please make sure that the code is compiled properly
    syntax for printf
    printf (" variables" , " control sting " );
    if you want to print an address of a variable use %i or % u

    thanx
    http://eecsatri.blogspot.com

    wrote:
    > Hi.I could not understand the following printf statement.Thanks for any
    > help.
    >
    > int a=3,b=5;
    > printf(&a["Ya!Hello!how is this?%s],&b["junk/super]);
    >
    > I think a equals to*[a+i].But I could not understand the use of &
    > here.Bye.
    > Regards,
    > Eric
    steve, Aug 20, 2006
    #3
  4. writes:
    > Hi.I could not understand the following printf statement.Thanks for any
    > help.
    >
    > int a=3,b=5;
    > printf(&a["Ya!Hello!how is this?%s],&b["junk/super]);
    >
    > I think a equals to*[a+i].But I could not understand the use of &
    > here.


    No, a means *(a+i), which is equivalent to *(i+a), which is
    equivalent to i[a].

    You should never use this trick in real code.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 20, 2006
    #4
  5. steve <> wrote:

    > please make sure that the code is compiled properly


    Good advice.

    > syntax for printf
    > printf (" variables" , " control sting " );


    It's difficult to tell what you meant by this, and difficult to
    believe that you in fact know that the prototype of printf is

    int printf( const char * format, ... );

    > if you want to print an address of a variable use %i or % u


    Wrong. The format specifier for printing an address (which must be
    cast to void * if it is not already of that type) is %p.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Aug 21, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. code_wrong

    Re: printf scanf problem

    code_wrong, Aug 2, 2003, in forum: C Programming
    Replies:
    1
    Views:
    414
    /dev/null
    Aug 2, 2003
  2. ben
    Replies:
    4
    Views:
    614
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,240
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    431
  5. guru
    Replies:
    8
    Views:
    280
Loading...

Share This Page