++ devils

Discussion in 'C Programming' started by muttaa, May 21, 2006.

  1. muttaa

    muttaa Guest

    Hello all,

    My doubt is going to be so primitive that i ask you all to forgive me
    beforehand....

    Here's the code snippet:

    int main()
    {
    int x=5;
    x=x++ + ++x;
    printf("%d",x);
    return 0;
    }

    I know that the printf is going to print some value of x which none can
    predict accurately for all times for all compilers....However,why i ask
    this is because such type of questions keep appearing in a lot of
    Question Papers....

    Help me suggest a way to tackle this..

    Thanks in advance!
    muttaa, May 21, 2006
    #1
    1. Advertising

  2. muttaa

    ash Guest

    as well as i know you should read this equation like this
    x=5
    =x++ + ++x
    = 5 + 6
    =11

    i have try it in some windows platform c and c++ and found no change.
    hope it will be useful
    ash, May 21, 2006
    #2
    1. Advertising

  3. muttaa wrote:
    > Hello all,
    >
    > My doubt is going to be so primitive that i ask you all to forgive me
    > beforehand....
    >
    > Here's the code snippet:
    >
    > int main()
    > {
    > int x=5;
    > x=x++ + ++x;
    > printf("%d",x);
    > return 0;
    > }
    >
    > I know that the printf is going to print some value of x which none can
    > predict accurately for all times for all compilers....However,why i ask
    > this is because such type of questions keep appearing in a lot of
    > Question Papers....


    The behavior is undefined, x can now be any value. I would suggest you
    read our FAQ at <http://c-faq.com/expr/index.html>, start with question
    3.2.

    Robert Gamble
    Robert Gamble, May 21, 2006
    #3
  4. Please quote relevant context when posting followups, if you had been
    lurking for a little which before you posted (which you should have
    done) then you would know this. Since you are using Google Groups,
    please read <http://cfaj.freeshell.org/google/> before posting again.

    ash wrote:
    > as well as i know you should read this equation like this
    > x=5
    > =x++ + ++x
    > = 5 + 6
    > =11
    >
    > i have try it in some windows platform c and c++ and found no change.
    > hope it will be useful


    Wrong. Just because it works one way on your compiler doesn't mean
    squat, the language isn't defined by your compiler. As I said in my
    response, the result is undefined, x can be anything including 11.
    Modifying the value of an object more than once between sequence points
    is undefined. I suggest you follow the same advice I gave the OP
    regarding the FAQ.

    Robert Gamble
    Robert Gamble, May 21, 2006
    #4
  5. muttaa

    muttaa Guest

    Ok,what should i do if i am asked such sort of questions in my
    interview or written exams in the future?

    thanks for your reply!!
    muttaa, May 21, 2006
    #5
  6. muttaa

    John F Guest

    muttaa wrote:
    > Ok,what should i do if i am asked such sort of questions in my
    > interview or written exams in the future?
    >
    > thanks for your reply!!



    Behavior is undefined. Read the FAQ about that. It is very clear.

    --
    J
    John F, May 21, 2006
    #6
  7. [Off-topic] Doubt about doubt

    "muttaa" wrote:
    >My doubt is going to be ...


    I frequently see posts were the word "doubt" is used instead of
    "question"
    I am just curious, what is the native language of these posters?
    Do the words "doubt" and "question" show up as having similar meanings
    in <Your-language> to English dictionaries?
    Inquiring minds want to know. ;)
    Roberto Waltman, May 21, 2006
    #7
  8. muttaa

    Ben Pfaff Guest

    "muttaa" <> writes:

    > Ok,what should i do if i am asked such sort of questions in my
    > interview or written exams in the future?


    Cite ISO 9899-1999 section 6.5, paragraph 2:

    Between the previous and next sequence point an object shall
    have its stored value modified at most once by the
    evaluation of an expression. Furthermore, the prior value
    shall be read only to determine the value to be stored.70)

    --
    "Am I missing something?"
    --Dan Pop
    Ben Pfaff, May 21, 2006
    #8
  9. muttaa

    Joe Smith Guest

    "Robert Gamble" <> wrote in message
    news:...
    > muttaa wrote:
    >> Hello all,
    >>
    >> My doubt is going to be so primitive that i ask you all to forgive me
    >> beforehand....
    >>
    >> Here's the code snippet:
    >>
    >> int main()
    >> {
    >> int x=5;
    >> x=x++ + ++x;
    >> printf("%d",x);
    >> return 0;
    >> }
    >>
    >> I know that the printf is going to print some value of x which none can
    >> predict accurately for all times for all compilers....However,why i ask
    >> this is because such type of questions keep appearing in a lot of
    >> Question Papers....

    >
    > The behavior is undefined, x can now be any value. I would suggest you
    > read our FAQ at <http://c-faq.com/expr/index.html>, start with question
    > 3.2.


    More relevant might be FAQ 20.21b in my version where I find Mr. Summit's
    commentary droll about those who would write 'a+++++b'. I have no idea what
    people are talking about with "posting from Google." It would seem that
    these persons don't work very hard at improving the quality of their posts.
    joe
    Joe Smith, May 21, 2006
    #9
  10. muttaa

    David Wade Guest

    "muttaa" <> wrote in message
    news:...
    > Ok,what should i do if i am asked such sort of questions in my
    > interview or written exams in the future?
    >


    Say that behaviour is undefined. And if they can't accept that look for
    another job. You don't want to work there.

    > thanks for your reply!!
    >
    David Wade, May 21, 2006
    #10
  11. muttaa

    Ben Pfaff Guest

    Re: [Off-topic] Doubt about doubt

    Roberto Waltman <> writes:

    > I frequently see posts were the word "doubt" is used instead of
    > "question"
    > I am just curious, what is the native language of these posters?


    It's part of the dialect of English spoken in India. (This has
    been discussed here before, multiple times. Please refer to an
    archive.)
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
    Ben Pfaff, May 21, 2006
    #11
  12. Re: [Off-topic] Doubt about doubt

    Ben Pfaff wrote:
    >Roberto Waltman writes:
    >> I frequently see posts were the word "doubt" is used instead of
    >> "question"
    >> I am just curious, what is the native language of these posters?

    >It's part of the dialect of English spoken in India. (This has
    >been discussed here before, multiple times. Please refer to an
    >archive.)


    Thanks - I did search before in my own (incomplete) archives, but I
    gave up after the first ~45 hits did not bring up anything relevant.
    Roberto Waltman, May 21, 2006
    #12
  13. muttaa

    Eric Guest

    Re: [Off-topic] Doubt about doubt

    Ben Pfaff wrote:

    > int main(void){char
    > p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    > \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int
    > putchar(\
    > );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof
    > p-1;putchar(p\ );}return 0;}


    Just curious what this is supposed to do, i compiled and ran it and i get:
    Itrs.Ylmreanx wbTVdWjsu

    Which means nothing to me...

    Thanks
    Eric
    Eric, May 21, 2006
    #13
  14. muttaa

    John F Guest

    Re: [Off-topic] Doubt about doubt

    Eric wrote:
    > Ben Pfaff wrote:
    >
    >> int main(void){char
    >> p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    >> \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char
    >> *strchr();int
    >> putchar(\
    >> );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof
    >> p-1;putchar(p\ );}return 0;}

    >
    > Just curious what this is supposed to do, i compiled and ran it and
    > i
    > get: Itrs.Ylmreanx wbTVdWjsu
    >
    > Which means nothing to me...


    ITRS: International Technology Roadmap for Semiconductors

    For the rest I have to pass too... :)

    --
    John
    John F, May 21, 2006
    #14
  15. Re: [Off-topic] Doubt about doubt

    Eric said:

    > Ben Pfaff wrote:
    >
    >> int main(void){char
    >> p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    >> \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int
    >> putchar(\
    >> );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof
    >> p-1;putchar(p\ );}return 0;}

    >
    > Just curious what this is supposed to do, i compiled and ran it and i get:
    > Itrs.Ylmreanx wbTVdWjsu
    >
    > Which means nothing to me...


    Let's take it apart, then - ruthlessly re-organising the code for clarity
    (sorry, Ben!). At no point (and I think this is important) will we assume
    that Ben is assuming ASCII.

    int main(void)
    {
    char p[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz. \n";
    char *q = "kl BIcNBFr.NKEzjwCIxNJC";
    int i=sizeof p/2;
    char *strchr();
    int putchar();
    while(*q)
    {
    i += strchr(p, *q++) - p;
    if(i >= (int)sizeof p)
    {
    i -= sizeof p-1;
    }
    putchar(p);
    }
    return 0;
    }

    Now we'll get rid of those prototypes, by including the relevant headers:

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    char p[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz. \n";
    char *q = "kl BIcNBFr.NKEzjwCIxNJC";
    int i=sizeof p/2;

    while(*q)
    {
    i += strchr(p, *q++) - p;
    if(i >= (int)sizeof p)
    {
    i -= sizeof p - 1;
    }
    putchar(p);
    }
    return 0;
    }

    Clearly p is an alphabet, which leads me to think Ben is being very mindful
    of portability here. So his q points to some kind of indexing mechanism,
    which will be used to select letters from the alphabet.

    His first processing step is to set i to 28 (i.e. the size of p, divided by
    2).

    Now he loops through the characters in the string literal pointed to by q.

    The first of these is 'k'. He calls strchr to find out how far into p[] 'k'
    can be found. (He bumps q at the same time, ready to go round again.)

    k is at offset 37, so he adds 37 to i, making it 65. He now subtracts the
    number of characters in the alphabet array - i.e. 55. This leaves 10. He
    now displays the 10th character of his alphabet, which is 'J'.

    Now he goes round again, this time looking for the offset of 'l' in p[].
    This is 38, so he adds this to i, making 48. The 48th character in p[] is
    'u', so he displays 'u', and goes round again.

    So we have the pattern. He is overloading p[] quite horribly :) by using it
    not just as a repository of displayable characters but also as a catalyst
    for converting a number into a letter, for obfuscatory reasons.

    If we use his technique to construct a different message, it may help you to
    understand the program.

    Message: "clc rocks\n"

    We have this skeleton program:

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    char p[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz. \n";

    char *q goes here, but we aren't yet sure what to put here.

    int i=sizeof p/2;

    while(*q)
    {
    i += strchr(p, *q++) - p;
    if(i >= (int)sizeof p)
    {
    i -= sizeof p - 1;
    }
    putchar(p);
    }
    return 0;
    }

    We want to start off with a 'c', so we count from offset 28 to the letter
    'c'. This happens to be 0 (by chance, that's all). Offset 0 in p[] is 'A',
    so the first letter in our indexing mechanism is 'A'. The next letter we
    want is 'l', which is 9 characters along from 'c', so we want to move 9
    letters further along the alphabet from our current 'A' position: that
    leads us to 'J'.

    Continuing in this manner, we end up with this string:

    char *q="AJuZt.rIIK";

    Plug it in:

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    char p[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz. \n";

    char *q="AJuZt.rIIK";

    int i=sizeof p/2;

    while(*q)
    {
    i += strchr(p, *q++) - p;
    if(i >= (int)sizeof p)
    {
    i -= sizeof p - 1;
    }
    putchar(p);
    }
    return 0;
    }

    Recompile, and run it:

    $ ./foo
    clc rocks
    $


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, May 21, 2006
    #15
  16. muttaa

    CBFalconer Guest

    muttaa wrote:
    >
    > Ok,what should i do if i am asked such sort of questions in my
    > interview or written exams in the future?


    You should first learn to include adequate context in your
    articles. For means to do so on the insipid google interface to
    usenet, see my sig. below.

    If asked such question the answer is "undefined behaviour". It may
    be the asker doesn't agree with that, and is an idiot.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
    CBFalconer, May 22, 2006
    #16
  17. muttaa

    Joe Smith Guest

    Re: [Off-topic] Doubt about doubt

    "Richard Heathfield" <> wrote in message
    news:...
    > Eric said:
    >
    >> Ben Pfaff wrote:
    >>
    >>> int main(void){char
    >>> p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    >>> \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int
    >>> putchar(\
    >>> );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof
    >>> p-1;putchar(p\ );}return 0;}

    >>
    >> Just curious what this is supposed to do, i compiled and ran it and i
    >> get:
    >> Itrs.Ylmreanx wbTVdWjsu
    >>
    >> Which means nothing to me...

    >
    > Let's take it apart, then - ruthlessly re-organising the code for clarity
    > (sorry, Ben!). At no point (and I think this is important) will we assume
    > that Ben is assuming ASCII.
    >
    > int main(void)
    > {
    > char p[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    > "abcdefghijklmnopqrstuvwxyz. \n";
    > char *q = "kl BIcNBFr.NKEzjwCIxNJC";
    > int i=sizeof p/2;
    > char *strchr();
    > int putchar();
    > while(*q)
    > {
    > i += strchr(p, *q++) - p;
    > if(i >= (int)sizeof p)
    > {
    > i -= sizeof p-1;
    > }
    > putchar(p);
    > }
    > return 0;
    > }
    >
    > Now we'll get rid of those prototypes, by including the relevant headers:
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main(void)
    > {
    > char p[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    > "abcdefghijklmnopqrstuvwxyz. \n";
    > char *q = "kl BIcNBFr.NKEzjwCIxNJC";
    > int i=sizeof p/2;
    >
    > while(*q)
    > {
    > i += strchr(p, *q++) - p;
    > if(i >= (int)sizeof p)
    > {
    > i -= sizeof p - 1;
    > }
    > putchar(p);
    > }
    > return 0;
    > }
    >
    > Clearly p is an alphabet, which leads me to think Ben is being very
    > mindful
    > of portability here. So his q points to some kind of indexing mechanism,
    > which will be used to select letters from the alphabet.
    >
    > His first processing step is to set i to 28 (i.e. the size of p, divided
    > by
    > 2).
    >
    > Now he loops through the characters in the string literal pointed to by q.
    >
    > The first of these is 'k'. He calls strchr to find out how far into p[]
    > 'k'
    > can be found. (He bumps q at the same time, ready to go round again.)
    >
    > k is at offset 37, so he adds 37 to i, making it 65. He now subtracts the
    > number of characters in the alphabet array - i.e. 55. This leaves 10. He
    > now displays the 10th character of his alphabet, which is 'J'.
    >
    > Now he goes round again, this time looking for the offset of 'l' in p[].
    > This is 38, so he adds this to i, making 48. The 48th character in p[] is
    > 'u', so he displays 'u', and goes round again.
    >
    > So we have the pattern. He is overloading p[] quite horribly :) by using
    > it
    > not just as a repository of displayable characters but also as a catalyst
    > for converting a number into a letter, for obfuscatory reasons.
    >
    > If we use his technique to construct a different message, it may help you
    > to
    > understand the program.
    >
    > Message: "clc rocks\n"
    >
    > We have this skeleton program:
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main(void)
    > {
    > char p[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    > "abcdefghijklmnopqrstuvwxyz. \n";
    >
    > char *q goes here, but we aren't yet sure what to put here.
    >
    > int i=sizeof p/2;
    >
    > while(*q)
    > {
    > i += strchr(p, *q++) - p;
    > if(i >= (int)sizeof p)
    > {
    > i -= sizeof p - 1;
    > }
    > putchar(p);
    > }
    > return 0;
    > }
    >
    > We want to start off with a 'c', so we count from offset 28 to the letter
    > 'c'. This happens to be 0 (by chance, that's all). Offset 0 in p[] is 'A',
    > so the first letter in our indexing mechanism is 'A'. The next letter we
    > want is 'l', which is 9 characters along from 'c', so we want to move 9
    > letters further along the alphabet from our current 'A' position: that
    > leads us to 'J'.
    >
    > Continuing in this manner, we end up with this string:
    >
    > char *q="AJuZt.rIIK";
    >
    > Plug it in:
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main(void)
    > {
    > char p[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    > "abcdefghijklmnopqrstuvwxyz. \n";
    >
    > char *q="AJuZt.rIIK";
    >
    > int i=sizeof p/2;
    >
    > while(*q)
    > {
    > i += strchr(p, *q++) - p;
    > if(i >= (int)sizeof p)
    > {
    > i -= sizeof p - 1;
    > }
    > putchar(p);
    > }
    > return 0;
    > }
    >
    > Recompile, and run it:

    I'm at a bit of a roadblock in taking this a step further. This choice of
    string literal:
    char *q = "dYLsIKJAAB";
    is to yield "Calais " with 2 spaces and a newline. BTW, the reason that p
    has order 56 is not that there 56 things between the quotes. I was quite
    certain an hour ago that I could permute the elements of p so as to make the
    output instead print "Normandy" with a newline. My certainty has been
    deflated by my failure to find a suitable p. joe
    Joe Smith, May 22, 2006
    #17
  18. Robert Gamble wrote:
    > muttaa wrote:
    > > Hello all,
    > >
    > > My doubt is going to be so primitive that i ask you all to forgive me
    > > beforehand....
    > >
    > > Here's the code snippet:
    > >
    > > int main()
    > > {
    > > int x=5;
    > > x=x++ + ++x;
    > > printf("%d",x);
    > > return 0;
    > > }
    > >
    > > I know that the printf is going to print some value of x which none can
    > > predict accurately for all times for all compilers....However,why i ask
    > > this is because such type of questions keep appearing in a lot of
    > > Question Papers....

    >
    > The behavior is undefined, x can now be any value. I would suggest you
    > read our FAQ at <http://c-faq.com/expr/index.html>, start with question
    > 3.2.
    >
    > Robert Gamble


    why programming languages have undefined behavior?
    v4vijayakumar, May 22, 2006
    #18
  19. muttaa

    Vladimir Oka Guest

    v4vijayakumar opined:

    >
    > Robert Gamble wrote:
    >> muttaa wrote:


    <snip>

    >> > x=x++ + ++x;


    <snip>

    >> The behavior is undefined, x can now be any value. I would suggest
    >> you read our FAQ at <http://c-faq.com/expr/index.html>, start with
    >> question 3.2.

    >
    > why programming languages have undefined behavior?


    Because it is not feasible to define behaviour of every possible
    language construct that the grammar allows -- especially the silly
    ones.

    --
    ...you could spend *all day* customizing the title bar. Believe me. I
    speak from experience."
    (By Matt Welsh)

    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
    Vladimir Oka, May 22, 2006
    #19
  20. Vladimir Oka wrote:
    > v4vijayakumar opined:
    >
    > >
    > > Robert Gamble wrote:
    > >> muttaa wrote:

    >
    > <snip>
    >
    > >> > x=x++ + ++x;

    >
    > <snip>
    >
    > >> The behavior is undefined, x can now be any value. I would suggest
    > >> you read our FAQ at <http://c-faq.com/expr/index.html>, start with
    > >> question 3.2.

    > >
    > > why programming languages have undefined behavior?

    >
    > Because it is not feasible to define behaviour of every possible
    > language construct that the grammar allows -- especially the silly
    > ones.


    <snip>

    It seems that these are problems(?!) with 'sequence points'. When
    operands (x++) could be potential expressions then why don't introduce
    more 'sequence points'?! any performance issues?!
    v4vijayakumar, May 22, 2006
    #20
    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.

Share This Page