++ devils

M

muttaa

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!
 
A

ash

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
 
R

Robert Gamble

muttaa said:
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
 
R

Robert Gamble

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.
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
 
M

muttaa

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!!
 
J

John F

muttaa said:
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.
 
R

Roberto Waltman

muttaa" said:
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. ;)
 
B

Ben Pfaff

muttaa said:
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)
 
J

Joe Smith

Robert Gamble said:
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
 
D

David Wade

muttaa said:
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.
 
B

Ben Pfaff

Roberto Waltman said:
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.)
 
R

Roberto Waltman

Ben said:
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.
 
E

Eric

Ben said:
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
 
J

John F

Eric said:
Ben said:
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... :)
 
R

Richard Heathfield

Eric said:
Ben said:
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
$
 
C

CBFalconer

muttaa said:
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/>
 
J

Joe Smith

Richard Heathfield said:
Eric said:
Ben said:
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
 
V

v4vijayakumar

Vladimir said:
v4vijayakumar opined:


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?!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top