can i use + operator in Printf functions in C language?

Discussion in 'C Programming' started by sabarish, Nov 13, 2005.

  1. sabarish

    sabarish Guest

    hi friend,

    is it possible to use the code like below
    i=4,j=3,k=9;
    printf("%d"+printf("%d%d",i,j,k));
    if it works how it is possible.....here what is the role of + ?
    plz help me
    thanks
     
    sabarish, Nov 13, 2005
    #1
    1. Advertising

  2. In article <>,
    sabarish <> wrote:
    >is it possible to use the code like below
    >i=4,j=3,k=9;
    >printf("%d"+printf("%d%d",i,j,k));
    >if it works how it is possible.....here what is the role of + ?


    printf() returns the number of characters transmitted (or a negative
    value if there is an error.) Either way, a number.

    "%d" is a character string, which devolves to a pointer to a character.

    A pointer to a character plus a number results in a new pointer
    that many places further advanced in the object.

    As i and j are single digits, printf("%d%d", i, j) is going to
    print two characters; the k argument will be ignored because there
    is no corresponding format element. The return value will thus be the
    number 2 for these particular values.

    2 characters further along from the beginning of "%d" is going to be
    a pointer to the \0 that terminates the %d string.

    printf() with an empty string (that just has the terminator) will result
    in no characters being printed.

    So, the end result would be to print 43, ignore the k, and do nothing
    for the outer printf().
    --
    Programming is what happens while you're busy making other plans.
     
    Walter Roberson, Nov 13, 2005
    #2
    1. Advertising

  3. On 12 Nov 2005 23:14:24 -0800, in comp.lang.c , "sabarish"
    <> wrote:

    >hi friend,
    >
    >is it possible to use the code like below
    >i=4,j=3,k=9;
    >printf("%d"+printf("%d%d",i,j,k));
    >if it works


    In this specific case, by chance it works, because "%d"+2 = '\0' and
    so nothing is printed by the outer printf.

    If j had been 12, it would have been "%d"+3 which would point to
    memory you don't own, and your programme might have crashed.

    >how it is possible.....here what is the role of + ?


    It adds the numeric result of the rightmost printf onto the address of
    the "%d", ie it causes the leftmost printf to print a string whose
    address is x bytes fiurther along in memory. Chances are that this
    memory doesn't belong to you.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Nov 13, 2005
    #3
  4. sabarish

    Old Wolf Guest

    Mark McIntyre wrote:

    > On 12 Nov 2005 23:14:24 -0800, in comp.lang.c , "sabarish"
    > <> wrote:
    >
    > >hi friend,
    > >
    > >is it possible to use the code like below
    > >i=4,j=3,k=9;
    > >printf("%d"+printf("%d%d",i,j,k));
    > >if it works

    >
    > In this specific case, by chance it works, because "%d"+2 = '\0' and
    > so nothing is printed by the outer printf.
    >
    > If j had been 12, it would have been "%d"+3 which would point to
    > memory you don't own, and your programme might have crashed.


    Another possibility is that stdout is closed, or has some other error,
    in which case the first printf() will return 0, so the second one will
    cause undefined behaviour because there isn't an argument to go
    with the %d .
     
    Old Wolf, Nov 13, 2005
    #4
  5. sabarish

    pete Guest

    Old Wolf wrote:

    > Another possibility is that stdout is closed, or has some other error,
    > in which case the first printf() will return 0,


    [#3] The printf function returns the number of characters
    transmitted, or a negative value if an output or encoding
    error occurred.

    --
    pete
     
    pete, Nov 13, 2005
    #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. Kaush
    Replies:
    5
    Views:
    394
  2. ben
    Replies:
    4
    Views:
    634
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

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

    printf affects following printf/s

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

Share This Page