FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
I found something funny in C

 
Post new topic   Reply to topic    Cambodia eXPlore Forum Index -> Client - Server Side Development
Author Message
Singachea
fourth grade


Joined: 19 Nov 2005
Posts: 258

PostPosted: Wed May 10, 2006 9:14 pm    Post subject: I found something funny in C Reply with quote

Code:
#include<stdio.h>

void main(void)
{
   
   int i;
   for(i = 0; i = 3; i++)
   {
      printf("funny?\n");
   }

}


What is the output of this code in C language? Wink

Back to top
guitarman
fourth grade


Joined: 04 Nov 2005
Posts: 724

PostPosted: Thu May 11, 2006 1:09 pm    Post subject: Reply with quote



an Endless loop???
Wow, Never try the condition of Eqaul sign before ehh..
Innovative Smile

Back to top
Singachea
fourth grade


Joined: 19 Nov 2005
Posts: 258

PostPosted: Thu May 11, 2006 11:55 pm    Post subject: Reply with quote

Can anyone explain why?

Back to top
guitarman
fourth grade


Joined: 04 Nov 2005
Posts: 724

PostPosted: Fri May 12, 2006 12:22 am    Post subject: Reply with quote

So... my answer is right??

Back to top
Singachea
fourth grade


Joined: 19 Nov 2005
Posts: 258

PostPosted: Fri May 12, 2006 12:48 am    Post subject: Reply with quote

yes, you are right brother, but i just wonder why. Is it the flaw in C or what?

Back to top
guitarman
fourth grade


Joined: 04 Nov 2005
Posts: 724

PostPosted: Fri May 12, 2006 1:10 am    Post subject: Reply with quote

for(i = 0; i = 3; i++)

I think first i is initialized to 0, next it checks the condition i = 3 (this is always true, as i was assigned to 3), so the first print statement fires. AFterwhich, i is incremented to 1, then the condition is still true.. next... next... always true.

I think the cue point is the condition>> i = 3. Instead, it should be i==3.

Back to top
Singachea
fourth grade


Joined: 19 Nov 2005
Posts: 258

PostPosted: Fri May 12, 2006 11:07 am    Post subject: Reply with quote

I think it's not an assignment.

for(i = 0; i = 3; i++)

i = 3 is the condition

and another: for(i = 0, i = 3; ; i++)

i = 3 is an assignment where the condition is always true.

Actually we can see this:

Code:
for(initialization; condition; update)
{
     statements;
}


can be transformed into

Code:
initialization
while(condition)
{
     statements;
     update;
}


Thus if the statement is written as for(i = 0; i = 3; i++), it should be the same if we write:

Code:
i = 0
while(i = 3)
{
     statements;
     i++;
}


A mistake should be detected at while(i = 3)

We can't compile this statement for(i = 0; i = 3; i++) in java because it detects an error (incompatible type.)

If the condition is changed to i == 3, then there is nothing printed out.

Back to top
guitarman
fourth grade


Joined: 04 Nov 2005
Posts: 724

PostPosted: Fri May 12, 2006 2:50 pm    Post subject: Reply with quote

Nice explanation about transforming into while loop Smile

It happens a lot to me when I code in PHP. I think use .NET is better .

Back to top
Display posts from previous:   
Post new topic   Reply to topic    Cambodia eXPlore Forum Index -> Client - Server Side Development All times are GMT + 8 Hours
Page 1 of 1



by phpBB