C programming Puzzles!!!

Status
Not open for further replies.
OP
hafees

hafees

In the zone
thanx tuxfan :)

Here is another puzzle. what will be the output of the program and explain why?
Code:
int main()
{
	int x=10,y=10;
	if(x++ < 10 && y++<10)
		printf("condition true");
	printf("x=%d \t y=%d",x,y);
    return 0;
}
(this puzzle is for beginners. so pls dont post it directly. pm me the answer)
 

tuxfan

Technomancer
Since it is for beginners, I better not try ;)

But since I am out of touch completely now, I am as bad as a beginner :(, although I have figured out the answer. :)

As for printf() and %d and %c, I have another function called say(). I made it and used that in place of silly printf(). It was simply far more convenient. Pretty simple function. Shall post the code after answer to this puzzle is "published" :)
 

gambit

Broken In
Can a program be written in c or c++ thru which u can modify DOS commands? For eg: if in dos I give a command say, xcopy folder1 c: then it should delete folder1 instead of copying the folder. What I'm trying to say is can I use c to hack into the command.com file of DOS?
 

tuxfan

Technomancer
gambit said:
Can a program be written in c or c++ thru which u can modify DOS commands? For eg: if in dos I give a command say, xcopy folder1 c: then it should delete folder1 instead of copying the folder. What I'm trying to say is can I use c to hack into the command.com file of DOS?

Huh?? What are you saying? I couldn't understand. Please explain what you are trying to do.

I will just try to reply from what I understood. You want to replace some commands like xcopy, etc with your own ones or fine tune them.

You can replace them. Here's what you can do. But is very easy to say these things, but not so easy to implement :)
- Make a TSR (Terminate and Stay Resident) program.
- That program should intervene all key board activity and see if it is one of the functions/commands that you want to replace.
- If it is, then execute your commands
- If it is not, then hand over the control to the OS to do what it wants to do.

Fine tuning is not possible because Micro$oft hasn't revealed the code. That is what pinches the Free Software Foundation and Richard Stallman the most :) GNU and Linux were born out of this problem.
 

gambit

Broken In
Yes tuxfan that's exactly what I intend to do. I want to 'fine tune' some of the commands in dos. Is it possible? Cld u give me an example of such a program?
 

tuxfan

Technomancer
As I already said, fine tuning is not possible in any Microsoft softwares including OS. If you want to add functionality, you have to write your own code from scratch :(

Alternatively, write .bat files. For command line arguments, use %1, %2, etc. :)
 
OP
hafees

hafees

In the zone
For external commands u can write ur own programs. for eg: xcopy can be replaced by ur own program. for commands in the command.com (internal commands) u can't replace the commands. still u can do with some macros

doskey cls=<your commnad> etc.
Refer TSR programming thru C by Yashwant Kanitkar for tweaking command.com (i m not sure which type of tweaks. i 've to refer).

BTW, no one is answering the puzzle :(
 
OP
hafees

hafees

In the zone
From: technovice

hi hafees...is the answer
x=11, y=10
i think it checks for x++ and leaves y unincreased
or am i wrong?
Yes! u r right. and thnx for answering the puzzle.
here is a detailed description.

the && operator is the shortcircuit logical AND.

consider the expression

if(condition1 && condition2) ... here if condition1 is false then it will never check condition2, because if one condition is false it will always result in false.

Remember the Truth table of AND gate
input1 input2 result

FALSE FALSE FALSE
FALSE TRUE FALSE
TRUE FALSE FALSE
TRUE TRUE TRUE
i.e if any of the condition is false, then the whole expression will result in FALSE.

now consider the statement


if(x++ > 10 && y++ > 10)

here the first condition fails, since x is not > 10 (remember x++ is post increment, and hence the condition checks whether 10 > 10 and ofcourse it is false). but x will be incremented because of x++. But since the first condition failed, it will never execute the second condition (y++ > 10). so y++ statement is not invoked and hence no change in y value.

Therefore the result will be x=11 and y=10.

now consider this
Code:
 main()
 {
	int x=10,y=10;
		if(x++ >= 10 || y++ >=10) printf("\ncondition is true");
	printf("\nx=%d y=%d",x,y);
 	return 0;
 }

what will be the output?
 

shrohit

Broken In
A new puzzle

try this program and explain the output

Code:
main()
{
         int k=2;
         printf("\n%d ,%d ,%d",k++,++k,k++);
         getch();
}

the output is very interesting
 

technovice

Broken In
@shrohit

the o/p is 4...4...2
i think the reason is that for unary operators the printf statement gives precedence from right to left
im sure about the working:

ie it first printed k=2 and then evaluated k=k+1 (post increment)
then it evaluated k=k+1 once again (preincrement) and printed it ie k=4
and then printed k=4 and then post incremented it

as for the reason im guessing
correct me if im wrong!! :)
 

shrohit

Broken In
nice try :D

let some other people also try

as you said the the order of printing ,it should be 2 4 4 not 4 4 2

i will surely explain why it is 4 4 2

and do know a magic number which can convert a int defined variable number to float number, if multiplied with that number? 8)
 

technovice

Broken In
shrohit said:
nice try :D
as you said the the order of printing ,it should be 2 4 4 not 4 4 2
i will surely explain why it is 4 4 2

oops!! :oops:
i meant it evaluates from right to left but prints it from left to right...
now is that right?
 

parth

Broken In
no technovic,
it evaluate from right to left and printf as they ware evaluated on their position.
any way's that's nice try and good luck for this


what is out put of this

and why

int a,b=6;
a=b++ + b++ + ++b + ++b +b;
printf("\n%d\t %d",a,b);

can you explaine me how??
 

technovice

Broken In
brilliant question parth!! :)
was bamboozled to see the o/p :shock:

had me occupied for almost half an hour
i had to try out a dozen different combinations of pre & post increments before i undertood the o/p

neways
o/p:a= 40 b=10

explanation (oh boy!!!!)

a= b++ + b++ + ++b + ++b + b;

as expected the two b++ do not affect the value of b in the above statement so its 6 + 6

now the actual value of b after these two post increments would have been b=8

then for some reason....the values of both the pre increments are computed first and then the sum is evaluated
what i mean is first ++b yields b=9 and 2nd ++b gives b=10
but both the values of ++b in the statement are added as 10

then (this is the most confusing part) the value of b is taken as the value before the two post increments...but after the 2 pre increments ie ++(++6)
ie b=8;

hence a= 6 + 6 + 10 + 10 + 8

just to make it more clear ill give another example:

a=b++ + ++b + ++b + b;
would be evaluated as
a=6 + 9 + 9+ 8 = 32
b= 9;

a= b++ + ++b +b;
would be
a=6 + 8 + 7 = 21
& b=8


hope i've managed to confuse one and all :lol:

once again thanks to parth for such a lovely puzzle! :wink:
 

shrohit

Broken In
@ technovice :D

Code:
   the answer is as below
                   there are calls for how the functions will take arguments
                   they are stdcall (standard call)
                                fastcall(fast call)
                                pascalcall etc...
          
                   the printf statement takes arguments from right to left
                   because it follows pascal call which takes args from right 
                   to left
             
                   all the other calls takes args from left to right
                   
                   u were thinking in the right way nice...

@ parth

Code:
  i think we enough puzzles bout increments and decrements
                  so try something new
 

technovice

Broken In
Hey guys..
i remember one beginner's puzzle that one of my friends asked me

thought ill share it with u guys

print "Hello World!!" without using any semicolon;

pm me the answer

my answer will be displayed after 2 days
cheers! :)
 

aadipa

Padawan
Edited: Sorry that i had posted the answer directly...


@technovice

I have PM u the answer. Publish it whenever u want if it is correct.
 

technovice

Broken In
I have an apology to make first! :oops:

regarding parth's question
the explaination for the problem seems to be much simpler than i thought it was
i discussed the puzzle with one of my friends and he pointed out that the solution could be as simple as
the pre increments and pre decrements are firstly evaluated
and all the values of b ie b++, b--,b,++b,--b are assigned the same value
hence in
a=b++ + b++ + ++b + ++b + b;
the 2 preincrements are firstly evaluated
and :. b=++[++b]=8
:. a=8+8+8+8+8=40
and b=10;

similarly
a=b++ + b++ + ++b +b
is a= 7 + 7 + 7+ 7=28
b=9;

sorry for the erroneous post earlier although it still is logically sound the latter solution is more efficient and probable.

-------------------------------------------------

As for the puzzle i submitted
aadipa it wouldn't have mattered if you had posted the answer directly coz its absolutely correct.....exactly the solution that i have

aadipa said:
Lets try....

Donno if it works... No C compiler here in office but code seems to be correct.

Code:


void main(void) {
if( printf("Hello World") ) {}
}


btw, printf() returns no of chars printed.


Hope its correct.

way to go man!!
i can see why u're a braniac!!
 
Status
Not open for further replies.
Top Bottom