Previous in Forum: Needed: Earthlink Technical Help   Next in Forum: Need EarthLink Help - Part 2
Close
Close
Close
21 comments
Rate Comments: Nested
Associate

Join Date: Jan 2008
Location: Bangalore , India
Posts: 52

How is This Output Coming?

08/22/2013 4:03 PM

I know this must be simple for all the programmers ...

i want to get the explanations for the first two outputs printed...

Here is the C program ..

#include<stdio.h>

main()

{
int x=1, y=1;

for(; y; printf("%d %d\n",x,y))
{
y = x++<=5;

}
printf("\n");
return 0;

}

O/p :

2 1

3 1

4 1

5 1

6 1

7 0

// i justt want to know how is the first line of out been printed as 2 1

__________________
Frank - Catch my statement if you can.
Register to Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

Good Answers:

These comments received enough positive votes to make them "good answers".
2
Guru
Popular Science - Weaponology - New Member United Kingdom - Member - New Member

Join Date: May 2007
Location: Harlow England
Posts: 16512
Good Answers: 670
#1

Re: How is this Output Coming?

08/22/2013 4:12 PM

maybe 'cos x starts as 1 and is immediately incremented by the ++ ?

Get a pencil and paper and write the numbers alongside each and every opperation.

I'm not really sure what x++<=5; does or is... that's the wonder of 'C' instant incomprehensibility with a veneer of sophistication.

IMO The world took a huge step backwards when Quickbasic was removed from Windows, by a bunch of snobs who didn't like line numbers.
And as for 'visual' basic... or visual anything for that matter ... a triumph of presentation over content.

Maybe if you explain what you would like to acheive we could code it in a simpler cleaner more readable manner?

Del

(baaaaad cynical kitty... I shall go sit on the naughty step)

__________________
health warning: These posts may contain traces of nut.
Register to Reply Good Answer (Score 2)
Guru

Join Date: Apr 2009
Location: USA, Florida
Posts: 1595
Good Answers: 125
#2
In reply to #1

Re: How is this Output Coming?

08/22/2013 5:59 PM

Totally agree !!

QBasic was the all time simple yet versatile program.

While I can write in VBasic, it was obviously designed for those who like "pretty", and was also designed to torment those of us who dabble in code.

__________________
An obstacle is something you see when you take your eyes off the goal.
Register to Reply Score 1 for Off Topic
2
Guru

Join Date: Feb 2011
Location: Atlanta, GA
Posts: 1053
Good Answers: 110
#3

Re: How is this Output Coming?

08/22/2013 6:24 PM

This is explained in considerable detail here. I'd guess you may be tripped up by the increment occurring before the print. Were you expecting 1 1 ?

__________________
Think big. Drive small.
Register to Reply Good Answer (Score 2)
Associate

Join Date: Jan 2008
Location: Bangalore , India
Posts: 52
#6
In reply to #3

Re: How is this Output Coming?

08/23/2013 2:22 AM

Hi

thanks .... this explains all...

__________________
Frank - Catch my statement if you can.
Register to Reply
3
Guru
Popular Science - Evolution - New Member Popular Science - Weaponology - New Member

Join Date: May 2006
Location: The 'Space Coast', USA
Posts: 11119
Good Answers: 918
#4

Re: How is this Output Coming?

08/22/2013 7:37 PM

I think that the reason it never prints 1 1 first is because the for expression is not evaluated quite the way you would expect.

The for statement is really three statements rolled into one line, but it does not execute as a single statement, rather is it dispersed amongst the braces {}.

Even though you visually see the printf("%d %d\n",x,y) at the start of the loop, that expression is not evaluated until it has actually gone through the loop once!

The goofy expression y = x++<=5; is really y = x++ <= 5;

That means that y equals either true or false (1 or 0) depending on the value of x. X is 1 at the start and the evaluation 1 <= 5 is true, so y = 1.

Next, the code increments x to the value of 2 and it reaches the for loop's closing brace, which is where the printf("%d %d\n",x,y) is executed. So it prints 2 1.

The loop repeats until x is 6 and y evaluates as false (0), x is incremented to 7 and the loop prints 7 0, then exits the loop where a final carriage return is printed and the program terminates.

A for loop executes in the following way

for (i=0; i<5; i++)

{

do something

}

i=0 only happens once at the beginning of the loop.

i<5 is evaluated and if true, the program executes the code between the braces.

i++ is the last thing the loop does before reentering the loop and reevaluating i<5.

You may also want to look up post incrementing (x++) versus preincrementing (++). The difference is when the operator is incremented; the first is after the operator is used by the program. The second occurs before the program uses x.

Register to Reply Good Answer (Score 3)
Associate

Join Date: Jan 2008
Location: Bangalore , India
Posts: 52
#7
In reply to #4

Re: How is this Output Coming?

08/23/2013 2:24 AM

thank you.

check K_fry's india bix link which has the same Q and explanations

__________________
Frank - Catch my statement if you can.
Register to Reply
Guru
Popular Science - Weaponology - New Member United Kingdom - Member - New Member

Join Date: May 2007
Location: Harlow England
Posts: 16512
Good Answers: 670
#9
In reply to #4

Re: How is this Output Coming?

08/23/2013 2:50 AM

Great explanation of the poorly writen program, and how it should have been done.
KISS principal should always be applied to writing code.
Del

__________________
health warning: These posts may contain traces of nut.
Register to Reply
Guru
Popular Science - Evolution - New Member Popular Science - Weaponology - New Member

Join Date: May 2006
Location: The 'Space Coast', USA
Posts: 11119
Good Answers: 918
#12
In reply to #9

Re: How is this Output Coming?

08/23/2013 8:33 AM

This has been one of my biggest pet peeves about software programmers. I have seen enough of them over complicate their code for no other reason than to parade their mastery of the C language syntax.

In the aviation industry we must keep it readable and maintainable. Code written by one person must be readily maintainable by anyone else.

While that particular code snippet was probably written as a software homework problem, I contend it is a lesson on what not to do. A better way to do this is as follows:

int x=1, y=1; // Initialize x & y.

while (Y)
{
if (x++ <= 5) // Test x, then increment.
{
y = TRUE; // If less than 6, y is TRUE.
}
else
{
y = FALSE; // Else, y is FALSE.
}

printf("%d %d\n",x,y); // Print value of x and y.

} // End while (y)

Sorry about the formatting...

Register to Reply
Guru
Popular Science - Evolution - New Member Popular Science - Weaponology - New Member

Join Date: May 2006
Location: The 'Space Coast', USA
Posts: 11119
Good Answers: 918
#13
In reply to #12

Re: How is this Output Coming?

08/23/2013 8:36 AM

The only other reason to do this differently might have to do with code efficiency. In those cases you may be forced to use C trickery or even embedded assembly language to meet design requirements.

If you do, you would be expected to use copious comments to explain what you are doing and why.

Register to Reply
Guru

Join Date: Feb 2011
Location: Atlanta, GA
Posts: 1053
Good Answers: 110
#17
In reply to #12

Re: How is this Output Coming?

08/23/2013 11:11 AM

Great example of nicely commented code. GA

__________________
Think big. Drive small.
Register to Reply
Power-User

Join Date: Feb 2011
Location: New Zealand
Posts: 128
Good Answers: 5
#5

Re: How is this Output Coming?

08/22/2013 11:11 PM

Switch to Delphi. It is not stupidly obscure.

Register to Reply
Guru
Popular Science - Evolution - New Member Popular Science - Weaponology - New Member

Join Date: May 2006
Location: The 'Space Coast', USA
Posts: 11119
Good Answers: 918
#14
In reply to #5

Re: How is this Output Coming?

08/23/2013 8:37 AM

Nor is it as powerful.

Register to Reply
Power-User

Join Date: Feb 2011
Location: New Zealand
Posts: 128
Good Answers: 5
#19
In reply to #14

Re: How is this Output Coming?

08/23/2013 10:47 PM

How do you define "powerful"? Dephi works for our realtime machine vision applications. They have some complicated mathemetics and handle 15 camera frames per second very successfully. And you can read the code.

Register to Reply
Guru
Popular Science - Evolution - New Member Popular Science - Weaponology - New Member

Join Date: May 2006
Location: The 'Space Coast', USA
Posts: 11119
Good Answers: 918
#20
In reply to #19

Re: How is this Output Coming?

08/23/2013 11:32 PM

C is powerful because it can be used as an abstrated high level programming language as well as a low level language with the possibility of even embedding and/or calling assembly language when required.

It is a very mature language that is the most universally used programming language in the world. There are more programmers that know C and C++ than Delphi, therefore it is easier to source software engineers.

C is highly transportable from one hardware platform to the next.

C can be written to nearly the same efficiency as assembly code if done wisely and every processor and microprocessor sold on the planet has at least one C compiler available for it.

Particularly in the low level microcontroller world C is used universally. I don't know of any microcontroller that uses Delphi. I imagine there may be some that do, but C beats Delphi hands down in that world.

Register to Reply
Guru
Popular Science - Weaponology - New Member United Kingdom - Member - New Member

Join Date: May 2007
Location: Harlow England
Posts: 16512
Good Answers: 670
#8

Re: How is this Output Coming?

08/23/2013 2:48 AM

So I got it at trick one!

This illustrates the myth of 'readability' in C

There is a perfectly good 'while' statement which if used would have made the whole thing pretty obvious.

Or write out the condition 0< y<= 5 in full... add a sprinkling of brackets and ; for correct syntax

Trying to cram the whole program onto one line is self defeating.

If you break it down in to simple steps and code it sensibly it will be readable and easy to debug.....

How C can accept that code is beyond me, while it will throw out stuff that makes much more sense.

Del

__________________
health warning: These posts may contain traces of nut.
Register to Reply
Associate

Join Date: Jan 2008
Location: Bangalore , India
Posts: 52
#10
In reply to #8

Re: How is this Output Coming?

08/23/2013 3:07 AM

LOL...

these are Questions used for testing once C skills but not an object for company source codes...

i was very much confused on how did x = 1 and y =1 be printed...

could understand by the time C echos the output , x has already become 2

Frank

__________________
Frank - Catch my statement if you can.
Register to Reply
Guru
Popular Science - Weaponology - New Member United Kingdom - Member - New Member

Join Date: May 2007
Location: Harlow England
Posts: 16512
Good Answers: 670
#11
In reply to #10

Re: How is this Output Coming?

08/23/2013 3:34 AM

If you want a specific answer you need to ask a specific question.

Even your last post makes no sense.

x=1 and y=1 were NOT printed.
The printout starts with 2

printf is the function that actually prints, but it only prints what you tell it to.

You have wasted the time of a lot of helpful people.

I'm out of this one.
Del

__________________
health warning: These posts may contain traces of nut.
Register to Reply
Guru
Popular Science - Evolution - New Member Popular Science - Weaponology - New Member

Join Date: May 2006
Location: The 'Space Coast', USA
Posts: 11119
Good Answers: 918
#15
In reply to #10

Re: How is this Output Coming?

08/23/2013 8:41 AM

I think that kind of code skill is a bad thing. It may be good to at least untangle or understand that kind of syntax...

However, if you worked for me and wrote code like that I would fire you!

And if you didn't understand why I fired you, I fire you again!!!

Register to Reply
Power-User
United States - Member - NE, USA Hobbies - Target Shooting - New Member Engineering Fields - Aerospace Engineering - Engineering Fields - Electromechanical Engineering - New Member

Join Date: Jul 2007
Location: USA
Posts: 199
Good Answers: 6
#16
In reply to #15

Re: How is this Output Coming?

08/23/2013 10:38 AM

The OPs original question looks identical to a homework that was in a book back when I was in school.

Register to Reply
Guru

Join Date: Feb 2011
Location: Atlanta, GA
Posts: 1053
Good Answers: 110
#18
In reply to #15

Re: How is this Output Coming?

08/23/2013 11:33 AM

This is a purely instructive (textbook) example, and I would guess that it is intended to challenge the student to untangle difficult-to-read code. I've been in the situation of having to read and modify someone else's undocumented code, and having the ability to do so can be useful, or even essential. Among other things, it enables you to rewrite the original code so others can understand it.

I imagine we have all commented out a section of code because we can't tell wtf it was intended to do. We've all no doubt found code with sections commented out and wondered why the code was there in the first place -- did it ever do anything useful? Did the original programmer try something and then forget to delete it all together?

I often think that Microsoft's code must be full of worthless garbage. Open Office is about 1/10th the size, last I checked.

__________________
Think big. Drive small.
Register to Reply
Anonymous Poster #1
#21

Re: How is This Output Coming?

08/26/2013 4:40 PM

I could be wrong, but I'd guess original intent was to increment x until it is greater than 5, reporting values and the result of the comparison. This could be cleanly and conventionally served by:

for (int x=1; x <= 5; ++x) printf("%d %d\n",x,x<=5);

(Though professionally, I almost never leave the statement controlled by the "for" statement unbraced, similarly with do, if, or while)

Register to Reply
Register to Reply 21 comments

Good Answers:

These comments received enough positive votes to make them "good answers".
Copy to Clipboard

Users who posted comments:

Anonymous Hero (6); frankcorners (3); JCase (1); K_Fry (3); Oraka (2); user-deleted-1105 (4); WJMFIRE (1)

Previous in Forum: Needed: Earthlink Technical Help   Next in Forum: Need EarthLink Help - Part 2

Advertisement