473,902 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

++ devils

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....Ho wever,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!

May 21 '06
77 3757
Roberto Waltman <us****@rwaltma n.net> 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[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
May 21 '06 #11
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.
May 21 '06 #12
Ben Pfaff wrote:
int main(void){char
p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int
putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof
p-1;putchar(p[i]\ );}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
May 21 '06 #13
Eric wrote:
Ben Pfaff wrote:
int main(void){char
p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char
*strchr();int
putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof
p-1;putchar(p[i]\ );}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
May 21 '06 #14
Eric said:
Ben Pfaff wrote:
int main(void){char
p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int
putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof
p-1;putchar(p[i]\ );}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[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ"
"abcdefghijklmn opqrstuvwxyz. \n";
char *q = "kl BIcNBFr.NKEzjwC IxNJC";
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[i]);
}
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[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ"
"abcdefghijklmn opqrstuvwxyz. \n";
char *q = "kl BIcNBFr.NKEzjwC IxNJC";
int i=sizeof p/2;

while(*q)
{
i += strchr(p, *q++) - p;
if(i >= (int)sizeof p)
{
i -= sizeof p - 1;
}
putchar(p[i]);
}
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[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ"
"abcdefghijklmn opqrstuvwxyz. \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[i]);
}
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[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ"
"abcdefghijklmn opqrstuvwxyz. \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[i]);
}
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)
May 21 '06 #15
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.c om, 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/googlegroupsrep ly/>
May 22 '06 #16

"Richard Heathfield" <in*****@invali d.invalid> wrote in message
news:aI******** ************@bt .com...
Eric said:
Ben Pfaff wrote:
int main(void){char
p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int
putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof
p-1;putchar(p[i]\ );}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[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ"
"abcdefghijklmn opqrstuvwxyz. \n";
char *q = "kl BIcNBFr.NKEzjwC IxNJC";
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[i]);
}
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[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ"
"abcdefghijklmn opqrstuvwxyz. \n";
char *q = "kl BIcNBFr.NKEzjwC IxNJC";
int i=sizeof p/2;

while(*q)
{
i += strchr(p, *q++) - p;
if(i >= (int)sizeof p)
{
i -= sizeof p - 1;
}
putchar(p[i]);
}
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[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ"
"abcdefghijklmn opqrstuvwxyz. \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[i]);
}
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[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ"
"abcdefghijklmn opqrstuvwxyz. \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[i]);
}
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
May 22 '06 #17

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....Ho wever,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?

May 22 '06 #18
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>

May 22 '06 #19

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

May 22 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.