Jump to content
xisto Community
cse-icons

C Code, Can U Solve This? Another interesting problem

Recommended Posts

Hello,

Look at the code given below

void fun(void)[/br]{[br]/* Put your code here so that main does not print 20 */[/br]}[br][/br]int main()[br]{[/br] int i = 20;[br] fun();[/br] printf("i = %d\n", i);[br] return 0;[/br]}



1. You are allowed to put your code ONLY in fun.
2. You are not allowed to change even one character in main.
3. You should not use functions like exit(),abort() in the function fun.

How do I manipulate and change the value of i?
Have fun...

Share this post


Link to post
Share on other sites

actually, if u use pointers, u cannot be sure that it will work on all systems.coz memory allocation is compiler and architecture specific....btw, this is not an assignment and also I got the answer without using pointers....just give it a shot....

Share this post


Link to post
Share on other sites

I haven't tested this code:

[br]void fun() {[br][/br]int aaaaaa;[/br]int *q= &aaaaaa;[br][/br]while ( *(q--)!=20 );   // we look for variable which value is 20 (it couldn't be 'i'!)[br][/br]*q = 10; // we change it[br][/br][br]}[br][/br]main() { ... }[/br]

Hm.... It is only suggestion.

Share this post


Link to post
Share on other sites
#include <stdio.h>[br][/br]void fun(void)[br]{[/br]#define fun() i=10[br]}[br][/br]int main()[br]{[/br]int i = 20;[/br]fun();[br]printf("i = %d\n", i);[/br]return 0;}

Share this post


Link to post
Share on other sites

yeah, that cud be a solution.... i have not tried out...
My solution is also on similar lines..... here it is:


fun()[/br]{[br]#define printf( i , j ) printf( i , j+1 )[/br]}[br][/br]main ()[br]{[/br]int i =10;[br]fun();[/br]printf("i = %d",i);}

Share this post


Link to post
Share on other sites

8bit, you can't do that (can have only 1 return 0;)Cse-icons you devil, who the hell woulddo that? Just playing around of course, Ididn't get it. I'm too practical :D

Share this post


Link to post
Share on other sites

void fun(){int i=0;while(true) i++;}overflow and program will end... or user will kill the program after waiting for a bit.--------------------------------void fun(){int *i=0;while(true) *(i++)=i;}same as above plus there is a chance that program corrupts some critical data and crashes

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.