eCharcha.Com   Support eCharcha.Com. Click on sponsor ad to shop online!

Advertise Here

Go Back   eCharcha.Com > Entertainment > Puzzles

Notices

Puzzles Oh those mind bending brain twisters!

Reply
 
Thread Tools Display Modes
  #1  
Old September 30th, 2005, 03:12 AM
ShivSainik's Avatar
ShivSainik ShivSainik is offline
Senior eCharchan
 
Join Date: Apr 2001
Location: San Jose, CA
Posts: 5,253
ShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond repute
Interesting C puzzle

For all programmers


#include<stdio.h>
void change()
{
/*
* Write something in this function so that the output of printf in main
* function should give output '5' . Do not change the main function and main should
* complete its execution i.e. dont use exit() in change(). Also only one character should
* be printed after program execution i.e. '5'
*/
}

void main()
{
int i=5;
change();
i=10;
printf("%d",i);
}
__________________
"Once you start thinking too much about your place in history, you're at a point where you're no longer trying as hard as you should be"
Reply With Quote
  #2  
Old September 30th, 2005, 03:14 AM
ShivSainik's Avatar
ShivSainik ShivSainik is offline
Senior eCharchan
 
Join Date: Apr 2001
Location: San Jose, CA
Posts: 5,253
ShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond repute
Re: Interesting C puzzle

its has 2 solutions :

1> Using assembly
2> Using Simple C
__________________
"Once you start thinking too much about your place in history, you're at a point where you're no longer trying as hard as you should be"
Reply With Quote
  #3  
Old September 30th, 2005, 10:59 AM
echarcha's Avatar
echarcha echarcha is offline
Sutradhar {admin}
 
Join Date: Jul 2000
Location: CA, USA
Posts: 45,354
echarcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond repute
Question Re: Interesting C puzzle

Is this okay??

Code:
void change() { printf("%d",5); fclose(stdout); }
void main() { int i=5; change(); i=10; printf("%d",i); }
__________________
eCharcha.com
-Loud and Proud Desi Opinions
Reply With Quote
  #4  
Old October 2nd, 2005, 11:08 PM
ShivSainik's Avatar
ShivSainik ShivSainik is offline
Senior eCharchan
 
Join Date: Apr 2001
Location: San Jose, CA
Posts: 5,253
ShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond repute
Re: Interesting C puzzle

sahi jawab echarcha !
__________________
"Once you start thinking too much about your place in history, you're at a point where you're no longer trying as hard as you should be"
Reply With Quote
  #5  
Old October 3rd, 2005, 06:39 AM
TerminatorJR's Avatar
TerminatorJR TerminatorJR is offline
Banned
 
Join Date: Nov 2004
Posts: 4,006
TerminatorJR has much to be proud ofTerminatorJR has much to be proud ofTerminatorJR has much to be proud ofTerminatorJR has much to be proud ofTerminatorJR has much to be proud ofTerminatorJR has much to be proud ofTerminatorJR has much to be proud ofTerminatorJR has much to be proud of
Re: Interesting C puzzle

Sainik saab, kind request. plz ask easy question so everyone can participate.
lol
Nice one.
i thought of printf("%d", 5); but I did'nt know how to get out of the function without returning value to main.
Reply With Quote
  #6  
Old October 3rd, 2005, 07:05 AM
ShivSainik's Avatar
ShivSainik ShivSainik is offline
Senior eCharchan
 
Join Date: Apr 2001
Location: San Jose, CA
Posts: 5,253
ShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond reputeShivSainik has a reputation beyond repute
Re: Interesting C puzzle

Quote:
Originally Posted by TerminatorJR
Sainik saab, kind request. plz ask easy question so everyone can participate.
lol
Nice one.
i thought of printf("%d", 5); but I did'nt know how to get out of the function without returning value to main.
Chalo ye lo simple puzzle :

What is output of following program ?

main()
{
int a=3, b = 5;

printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
}
__________________
"Once you start thinking too much about your place in history, you're at a point where you're no longer trying as hard as you should be"
Reply With Quote
  #7  
Old October 3rd, 2005, 07:20 AM
TerminatorJR's Avatar
TerminatorJR TerminatorJR is offline
Banned
 
Join Date: Nov 2004
Posts: 4,006
TerminatorJR has much to be proud ofTerminatorJR has much to be proud ofTerminatorJR has much to be proud ofTerminatorJR has much to be proud ofTerminatorJR has much to be proud ofTerminatorJR has much to be proud ofTerminatorJR has much to be proud ofTerminatorJR has much to be proud of
Re: Interesting C puzzle

Try karta hoon

Seems like something to do with arrays.
I would think it will print everything in the double quotes ""

Ya!Hello! how is this?
junk/super
WHAT!
this beauty tool is sensitive CCCCCC

Okay, thats' enough C for a Monday.
Reply With Quote
  #8  
Old October 3rd, 2005, 12:21 PM
GunsNRoses's Avatar
GunsNRoses GunsNRoses is offline
Spear Barbarian
 
Join Date: Jun 2001
Posts: 1,263
GunsNRoses has much to be proud ofGunsNRoses has much to be proud ofGunsNRoses has much to be proud ofGunsNRoses has much to be proud ofGunsNRoses has much to be proud ofGunsNRoses has much to be proud ofGunsNRoses has much to be proud ofGunsNRoses has much to be proud ofGunsNRoses has much to be proud of
Re: Interesting C puzzle

Quote:
Originally Posted by ShivSainik
Chalo ye lo simple puzzle :

What is output of following program ?

main()
{
int a=3, b = 5;

printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
}
This one is pretty easy actually. The reason it works the way it does is that C considers the following expressions equivalent:
x[5]
5[x]
reason being that it internally converts it to &(x+5) or &(5+x) which comes out to the same thing. With that in mind, the output is pretty easy to figure out. Remember that arrays are 0-based!

P.S. the original puzzle has a few more ways to get around it. close(1) should work under POSIX. There are also a couple more ingenious (though not very portable) ways around it too. Will post some other techniques in a bit.
__________________
"Pick up your balls and load up your cannon
For a twenty-one gun salute."
-- For Those About To Rock (We Salute You)

Proud LLKC member since May 2002.
"We've been looking for the enemy for some time now. We've finally found him. We're surrounded. That simplifies things." Lt. Gen. Lewis "Chesty" Puller, USMC, Chosin Reservoir 1950

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Russian Roulette: An interesting Interview Puzzle landyaBhai Puzzles 19 June 30th, 2014 11:01 AM
Puzzle more Puzzle sunnykode Puzzles 1 April 21st, 2009 01:37 PM
Puzzle 1: Same sum 2gud Puzzles 12 November 22nd, 2005 08:01 AM
another puzzle vyomkeshsaxena Puzzles 16 July 5th, 2005 10:44 AM
A Puzzle Shringarey SoapBox 15 March 5th, 2003 02:55 PM


All times are GMT -7. The time now is 08:11 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Site Copyright © eCharcha.Com 2000-2012.