14
Jan

void main()

{

unsigned i=1;

signed j=-1;

if(i

printf("less");

else

if(i>j)

printf("greater");

else

if(i==j)

printf("equal");

}

o/p is less

can anyone help me how this works?


Answer:
Indeed.

To those of you who didn't bother to understand the question, this program works, but says that 1 < -1, which is not right.

The problem is "signedness" - when comparing signed and unsigned integers, the C complier casts both numbers to unsigned, which turns (-1) into 4294967295 (unsigned representation of -1).

Try this to see what I'm talking about:

void main()

{

unsigned i=1;

signed j=-1;

if(i

printf("less %u < %u", i, j);

else

if(i>j)

printf("greater %u > %u", i, j);

else

if(i==j)

printf("equal");

}

To avoid this ambiguity, tell the compiler explicitly how you want it to treat your numbers - as singed or unsigned, i.e.:

if ( (signed) i < (signed) j)

or

if ( (unsigned) i < (unsigned) j)


Answer:
It goes something like this…..

if i is less than j, then print "Less" to the screen.

if i is more than j, then print "more" to the screen.

if they are equal, then print "equal" to the screen.

I hope I didn't just do your homework for you and no, you don't have my permission to copy and paste it, if you're gonna cheat at least type it in yourself.


Answer:
Sure,

//ur program enters the main method here…

void main()

{

//u assign 2 variables i and j

unsigned i=1;

signed j=-1;

//this asks if i is less than j which it is not so it goes to the else

if(i

printf("less");

//this asks if i is greater than j which it is so it runs the code in the else block

else

if(i>j)

//this tells it to print greater

printf("greater");

else

//because the code went into the above else block it will not bother to check this if statement..

if(i==j)

printf("equal");

}


Answer:
Means: if i is less than j, print less

if i is more than j, print more

if i is equal to j, print equal

If u need any more help, email me: pravin.singh71@yahoo.com

(was that your homework)

Book Mark it-> del.icio.us | Reddit | Slashdot | Digg | Facebook | Technorati | Google | StumbleUpon | Window Live | Tailrank | Furl | Netscape | Yahoo | BlinkList

Tags: , , ,

This entry was posted on Monday, January 14th, 2008 at 10:14 pm and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or TrackBack URI from your own site.

Leave a reply

Name (*)
Mail (*)
URI
Comment