What will be output of the code and why

0 views
Skip to first unread message

gaurav gupta

unread,
Aug 11, 2009, 1:43:27 PM8/11/09
to DS & Algo@itbhu
#include <stdio.h>
#include <conio.h>
int main ()
{
printf ("%d\n",'-'-'-'-'/'/'/');
    getch();
}

//Can anyone explain the output of above code
 

--
GAURAV GUPTA
B.Tech IV Yr. , Department of Computer Science & Engineering
IT BHU , Varanasi
Contacts
Phone No: +91-99569-49491

e-mail :
gaurav...@acm.org
gaurav.gu...@itbhu.ac.in

Shishir Mittal

unread,
Aug 11, 2009, 1:46:53 PM8/11/09
to ds--al...@googlegroups.com
On Tue, Aug 11, 2009 at 11:13 PM, gaurav gupta <1989....@googlemail.com> wrote:
#include <stdio.h>
#include <conio.h>
int main ()
{
printf ("%d\n",'-'-'-'-'/'/'/');
This equivalent to printf("%d\n",45-45-47/47);

    getch();
}

//Can anyone explain the output of above code
 

--
GAURAV GUPTA
B.Tech IV Yr. , Department of Computer Science & Engineering
IT BHU , Varanasi
Contacts
Phone No: +91-99569-49491

e-mail :
gaurav...@acm.org
gaurav.gu...@itbhu.ac.in





--
Shishir Mittal
Ph: +91 9936 180 121

gaurav gupta

unread,
Aug 11, 2009, 1:49:01 PM8/11/09
to ds--al...@googlegroups.com
Great Mittal..........

Thanks.

kinshuk chandra

unread,
Aug 11, 2009, 1:49:46 PM8/11/09
to ds--al...@googlegroups.com
output is v.simple:
-1

because
'-' -'-'  is 0
and -'/' /  '/' is -1

because what here happening is that instead of '-' or '/' we have ascii value for these character values and rest operations are performed normally

On Tue, Aug 11, 2009 at 11:13 PM, gaurav gupta <1989....@googlemail.com> wrote:



--
Kinshuk Chandra
Senior Undergraduate
Computer Science and Engineering
IT-BHU
Contact No. (+91)9307134205
webpage: http://kinshuk.ram.googlepages.com/

Gaurav Gupta

unread,
Aug 21, 2009, 8:37:02 AM8/21/09
to DS & Algo@itbhu
printf("\n32<<-1 = %x 32<<-0=%x 32>>-1 = %x 32>>-0=%x",
32<<-1,32<<-0,32>>-1,32>>-0);

Karan Jain

unread,
Aug 21, 2009, 9:07:00 AM8/21/09
to ds--al...@googlegroups.com
Assuming integer size to be 32 bits, whenever you left or right shift a number with a negative count (except -0 which is actually 0), it automatically converts it to (32-count).
 
So, 32<<-1 = 32<<31  = 0 (0x00000000)
32<<-0 = 32<<0 = 32 (0x00000020)
32>>-1 = 32>>31 = 0 (0x00000000)
32>>-0 = 32>>0 = 32 (0x00000020)
 
Hence output should be:
 
32<<-1 = 0 32<<-0 = 20 32>>-1 = 0 32>>-0 = 20

--
Regards,

Karan

gaurav gupta

unread,
Aug 21, 2009, 9:09:33 AM8/21/09
to ds--al...@googlegroups.com
sorry for my misunderstanding I was interpreting the output as decimal number.

Shishir Mittal

unread,
Aug 21, 2009, 10:19:04 AM8/21/09
to ds--al...@googlegroups.com
On Fri, Aug 21, 2009 at 6:37 PM, Karan Jain <karanja...@gmail.com> wrote:
Assuming integer size to be 32 bits, whenever you left or right shift a number with a negative count (except -0 which is actually 0), it automatically converts it to (32-count).

Refer Pg 306, Dennis Ritchie. The result is undefined if the right operand is negative or greater than or equal to the number of bits in the left expression's type.

Ajay

unread,
Aug 21, 2009, 10:25:29 AM8/21/09
to ds--al...@googlegroups.com
In my book (The C Programming Language, 2nd Edition by K&R), there are only 272 pages (including index).


--
Ajay Kr. Gautam
IDD, Part V
Dept. of Computer Science & Engg., IT-BHU
Varanasi-221005, UP
India
-----------------------------------------------------------------
"Easy reading is damned hard writing."
~ Nathaniel Hawthorne

Karan Jain

unread,
Aug 21, 2009, 10:27:39 AM8/21/09
to ds--al...@googlegroups.com
11
11
kool




--
Regards,

Karan

Shishir Mittal

unread,
Aug 21, 2009, 10:28:12 AM8/21/09
to ds--al...@googlegroups.com
On Fri, Aug 21, 2009 at 7:55 PM, Ajay <ajay....@gmail.com> wrote:
In my book (The C Programming Language, 2nd Edition by K&R), there are only 272 pages (including index).
Sorry for the typo, its 206! or you may also refer pg 49.

Ajay

unread,
Aug 21, 2009, 10:33:25 AM8/21/09
to ds--al...@googlegroups.com
Well its implementation dependent like many other things.
For ex-
function argument execution is undefined in ANSI C but gcc follows right to left associativity.
Reply all
Reply to author
Forward
0 new messages