473,395 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

i donno the problem

#include<stdio.h>
#include<conio.h>
main()
{
int n;
while(1<=n<=100)
{
printf("%d ",n);
}
}

Now this code prints something like 783 infinitely.....if the number
is between 1 to 100 then it should be printed infinetely else it
should not be printed at all///why is it so happening???
Sep 14 '08 #1
15 1064
"ashwin700" <mi**************@gmail.comwrote in message
news:ac**********************************@a3g2000p rm.googlegroups.com...
#include<stdio.h>
#include<conio.h>
main()
{
int n;
while(1<=n<=100)
{
printf("%d ",n);
}
}

Now this code prints something like 783 infinitely.....if the number
is between 1 to 100 then it should be printed infinetely else it
should not be printed at all///why is it so happening???
n is not initialized (so it could be anything, including 783)
"1<=n<=100" does Not mean "n is between 1 and 100" but rather something like
"(1 is smaller or equal to n) is small or equal to 100" which is allways
True because any outcome of the expression "(1 is smaller or equal to n)"
will be smaller or equal to 100 (it will be either 1 or 0)
Therefore it will keep printing n, forever, n is functionally random and
will not change.

you may want to try

#include<stdio.h>
#include<conio.h>
main()
{
int n;
while(n<=100 && n>0)
{
printf("%d ",n);
}
}

it will still not make sense but atleast the condition will do what you mean

Sep 14 '08 #2
"Harold Aptroot" <ha************@gmail.comwrote in message
news:63***************************@cache100.multik abel.net...
"ashwin700" <mi**************@gmail.comwrote in message
news:ac**********************************@a3g2000p rm.googlegroups.com...
>#include<stdio.h>
#include<conio.h>
main()
{
int n;
while(1<=n<=100)
{
printf("%d ",n);
}
}

Now this code prints something like 783 infinitely.....if the number
is between 1 to 100 then it should be printed infinetely else it
should not be printed at all///why is it so happening???

n is not initialized (so it could be anything, including 783)
"1<=n<=100" does Not mean "n is between 1 and 100" but rather something
like "(1 is smaller or equal to n) is small or equal to 100" which is
allways True because any outcome of the expression "(1 is smaller or equal
to n)" will be smaller or equal to 100 (it will be either 1 or 0)
actually, for True any value other than 0 would be allowed, but at least
with your compiler True ends up being smaller of equal to 100 (it could be
42 or -1 or -666 however)
Therefore it will keep printing n, forever, n is functionally random and
will not change.

you may want to try

#include<stdio.h>
#include<conio.h>
main()
{
int n;
while(n<=100 && n>0)
{
printf("%d ",n);
}
}

it will still not make sense but atleast the condition will do what you
mean
Sep 14 '08 #3
On Sep 14, 5:14*pm, "Harold Aptroot" <harold.aptr...@gmail.comwrote:
"ashwin700" <mishra.ashwin...@gmail.comwrote in message

news:ac**********************************@a3g2000p rm.googlegroups.com...
#include<stdio.h>
#include<conio.h>
main()
{
* *int n;
* *while(1<=n<=100)
* *{
* * * printf("%d ",n);
* *}
}
Now this code prints something like 783 infinitely.....if the number
is between 1 to 100 then it should be printed infinetely else it
should not be printed at all///why is it so happening???

n is not initialized (so it could be anything, including 783)
"1<=n<=100" does Not mean "n is between 1 and 100" but rather something like
"(1 is smaller or equal to n) is small or equal to 100" which is allways
True because any outcome of the expression "(1 is smaller or equal to n)"
will be smaller or equal to 100 (it will be either 1 or 0)
Therefore it will keep printing n, forever, n is functionally random and
will not change.

you may want to try

#include<stdio.h>
#include<conio.h>
main()
{
* *int n;
* *while(n<=100 && n>0)
* *{
* * * printf("%d ",n);
* *}

}

Here also, it will print either infinitely or will not print
anything.
see, n is not being changed at all.
so really need to check the purpose of program

--
vIpIn
Sep 14 '08 #4
On Sep 14, 3:19 pm, "Harold Aptroot" <harold.aptr...@gmail.comwrote:
"Harold Aptroot" <harold.aptr...@gmail.comwrote in message
n is not initialized (so it could be anything, including 783)
"1<=n<=100" does Not mean "n is between 1 and 100" but rather something
like "(1 is smaller or equal to n) is small or equal to 100" which is
allways True because any outcome of the expression "(1 is smaller or equal
to n)" will be smaller or equal to 100 (it will be either 1 or 0)

actually, for True any value other than 0 would be allowed, but at least
with your compiler True ends up being smaller of equal to 100 (it could be
42 or -1 or -666 however)
Nonsense. Comparison operators yield 0 or 1.
Sep 14 '08 #5
On Sep 14, 3:07 pm, ashwin700 <mishra.ashwin...@gmail.comwrote:
#include<stdio.h>
#include<conio.h>
<conio.his not standard. Do not include it, or post in a more
appropriate group. (presumably some microsoft group)
main()
Valid only in C89. Change it to int main(void).
("implicit int" was removed in C99)
{
int n;
while(1<=n<=100)
n is not initialized. You invoke undefined behavior.
Assuming n was initialized, this expression is always true.
See question 3.13 of the C-FAQ. <http://c-faq.com/>

<snip rest code>

Sep 14 '08 #6


<sh******@gmail.comwrote in message
news:ee**********************************@r15g2000 prd.googlegroups.com...
On Sep 14, 5:14 pm, "Harold Aptroot" <harold.aptr...@gmail.comwrote:
>"ashwin700" <mishra.ashwin...@gmail.comwrote in message

news:ac**********************************@a3g2000 prm.googlegroups.com...
#include<stdio.h>
#include<conio.h>
main()
{
int n;
while(1<=n<=100)
{
printf("%d ",n);
}
}
Now this code prints something like 783 infinitely.....if the number
is between 1 to 100 then it should be printed infinetely else it
should not be printed at all///why is it so happening???

n is not initialized (so it could be anything, including 783)
"1<=n<=100" does Not mean "n is between 1 and 100" but rather something
like
"(1 is smaller or equal to n) is small or equal to 100" which is allways
True because any outcome of the expression "(1 is smaller or equal to n)"
will be smaller or equal to 100 (it will be either 1 or 0)
Therefore it will keep printing n, forever, n is functionally random and
will not change.

you may want to try

#include<stdio.h>
#include<conio.h>
main()
{
int n;
while(n<=100 && n>0)
{
printf("%d ",n);
}

}


Here also, it will print either infinitely or will not print
anything.
see, n is not being changed at all.
so really need to check the purpose of program
Ah yes it will still not do anything useful - but atleast it will not
"ignore" the condition

Sep 14 '08 #7

"ashwin700" <mi**************@gmail.comwrote in message
news:ac**********************************@a3g2000p rm.googlegroups.com...
#include<stdio.h>
#include<conio.h>
main()
{
int n;
while(1<=n<=100)
{
printf("%d ",n);
}
}

Now this code prints something like 783 infinitely.....if the number
is between 1 to 100 then it should be printed infinetely else it
should not be printed at all///why is it so happening???
Not sure what you're trying to do but look at these alternatives:

n=24; /* must be initialised */

if (1<=n && n<=100)
printf("%d ",n);

n=24;

while (1<=n && n<=100) {
printf("%d ",n);
++n;
}

--
Bartc

Sep 14 '08 #8
On Sep 14, 5:07*pm, ashwin700 <mishra.ashwin...@gmail.comwrote:
#include<stdio.h>
#include<conio.h>
main()
{
* * int n;
* * while(1<=n<=100)
* * {
* * * *printf("%d ",n);
* * }

}

Now this code prints something like 783 infinitely.....if the number
is between 1 to 100 then it should be printed infinetely else it
should not be printed at all///why is it so happening???
it will always be in infinite loop
evaluation 1<=n<=100 is evaluated according to left to right
associativity

1<=n will always result in either 0 or 1
this resulting value (0 or 1) will always be <= 100

you can check the same by setting n =0; before
while(1<=n<=100)

--
vIpIn

Sep 14 '08 #9
vi******@gmail.com writes:
On Sep 14, 3:19 pm, "Harold Aptroot" <harold.aptr...@gmail.comwrote:
>"Harold Aptroot" <harold.aptr...@gmail.comwrote in message
n is not initialized (so it could be anything, including 783)
"1<=n<=100" does Not mean "n is between 1 and 100" but rather something
like "(1 is smaller or equal to n) is small or equal to 100" which is
allways True because any outcome of the expression "(1 is smaller or equal
to n)" will be smaller or equal to 100 (it will be either 1 or 0)

actually, for True any value other than 0 would be allowed, but at least
with your compiler True ends up being smaller of equal to 100 (it could be
42 or -1 or -666 however)

Nonsense. Comparison operators yield 0 or 1.
You are being purposely dense.

You know full well what he means

if(func(t))...

So long as the return is non zero ...

Context is another issue.
Sep 14 '08 #10
Richard<rg****@gmail.comwrites:
vi******@gmail.com writes:
>On Sep 14, 3:19 pm, "Harold Aptroot" <harold.aptr...@gmail.comwrote:
>>"Harold Aptroot" <harold.aptr...@gmail.comwrote in message
n is not initialized (so it could be anything, including 783)
"1<=n<=100" does Not mean "n is between 1 and 100" but rather something
like "(1 is smaller or equal to n) is small or equal to 100" which is
allways True because any outcome of the expression "(1 is smaller or equal
to n)" will be smaller or equal to 100 (it will be either 1 or 0)

actually, for True any value other than 0 would be allowed, but at least
with your compiler True ends up being smaller of equal to 100 (it could be
42 or -1 or -666 however)

Nonsense. Comparison operators yield 0 or 1.

You are being purposely dense.

You know full well what he means

if(func(t))...

So long as the return is non zero ...

Context is another issue.
That does not seem to what he meant at all. "at least with your
compiler" suggest to me he was correcting what he thought was an error
in his original answer.

--
Ben.
Sep 14 '08 #11
On 2008-09-14, ashwin700 <mi**************@gmail.comwrote:
#include<stdio.h>
#include<conio.h>
main()
{
int n;
while(1<=n<=100)
{
printf("%d ",n);
}
}

Now this code prints something like 783 infinitely.....if the number
is between 1 to 100 then it should be printed infinetely else it
should not be printed at all///why is it so happening???
Does your compiler not give you some warnings about
this code? It is not required to, but I think you
need a better compiler (or better compilation flags)
if this is the case.

Mine says:

usenet.c:2:18: error: conio.h: No such file or directory
usenet.c:4: warning: return type defaults to ‘int’
usenet.c: In function ‘main’:
usenet.c:6: warning: comparisons like X<=Y<=Z do not have
their mathematical meaning

(Though with no command-line arguments, it says nothing.)

--
Andrew Poelstra ap*******@wpsoftware.com
To email me, use the above email addresss with .com set to .net
Sep 14 '08 #12
Andrew Poelstra wrote:
....
Does your compiler not give you some warnings about
this code? It is not required to, but I think you
need a better compiler (or better compilation flags)
if this is the case.

Mine says:
....
(Though with no command-line arguments, it says nothing.)
That's odd - mine says

gcc - no input files

;-)
Sep 14 '08 #13
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
Richard<rg****@gmail.comwrites:
>vi******@gmail.com writes:
>>On Sep 14, 3:19 pm, "Harold Aptroot" <harold.aptr...@gmail.comwrote:
"Harold Aptroot" <harold.aptr...@gmail.comwrote in message
n is not initialized (so it could be anything, including 783)
"1<=n<=100" does Not mean "n is between 1 and 100" but rather
something
like "(1 is smaller or equal to n) is small or equal to 100" which is
allways True because any outcome of the expression "(1 is smaller or
equal
to n)" will be smaller or equal to 100 (it will be either 1 or 0)

actually, for True any value other than 0 would be allowed, but at
least
with your compiler True ends up being smaller of equal to 100 (it could
be
42 or -1 or -666 however)

Nonsense. Comparison operators yield 0 or 1.

You are being purposely dense.

You know full well what he means

if(func(t))...

So long as the return is non zero ...

Context is another issue.

That does not seem to what he meant at all. "at least with your
compiler" suggest to me he was correcting what he thought was an error
in his original answer.
Ah I am sorry that my bad english has caused an argument..
I just meant that if there had been an other "thing meant as bool" there it
could easily have been 42 and the same effect would happen
if that makes any sense to you..

Sep 14 '08 #14
On Sun, 14 Sep 2008 14:14:35 +0200, "Harold Aptroot"
<ha************@gmail.comwrote:

>you may want to try

#include<stdio.h>
#include<conio.h>
Why are you including a non-standard header that you don't use?
>main()
Implicit return values have been removed from the language.
>{
int n;
while(n<=100 && n>0)
This invokes undefined behavior.
{
printf("%d ",n);
}
main needs to return a value of the promised type.
>}

it will still not make sense but atleast the condition will do what you mean
--
Remove del for email
Sep 14 '08 #15
On 14 Sep, 18:41, "Harold Aptroot" <harold.aptr...@gmail.comwrote:
"Ben Bacarisse" <ben.use...@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
Richard<rgr...@gmail.comwrites:
vipps...@gmail.com writes:
On Sep 14, 3:19 pm, "Harold Aptroot" <harold.aptr...@gmail.comwrote:
"Harold Aptroot" <harold.aptr...@gmail.comwrote in messag
>n is not initialized (so it could be anything, including 783)
"1<=n<=100" does Not mean "n is between 1 and 100" but rather
something like "(1 is smaller or equal to n) is small or equal
to 100" which is allways True because any outcome of the
expression "(1 is smaller or equal to n)" will be smaller or
equal to 100 (it will be either 1 or 0)
>>actually, for True any value other than 0 would be allowed,
not in a compliant C compiler!
>>but
at least with your compiler True ends up being smaller of equal
to 100 (it could be 42 or -1 or -666 however)
>Nonsense. Comparison operators yield 0 or 1.
You are being purposely dense.
I don't think so

You know full well what he means
it confused me. And a very common (but incorrect
belief) is that boolean operators in C can yeild "non-zero"
for true, which seems to mean "any non-zero value".
I correct this misapprehension when I encounter it.

Re-reading Mr Aptroot, I'm still not convinced
he doesn't suffer from misapplication-of-true-is-non-zero
syndrome.

if(func(t))...
So long as the return is non zero ...
Context is another issue.
and the context is (1 <= n <= 100)

That does not seem to what he meant at all. *"at least with your
compiler" suggest to me he was correcting what he thought was an error
in his original answer.

Ah I am sorry that my bad english has caused an argument..
I just meant that if there had been an other "thing meant as bool"
there it
could easily have been 42 and the same effect would happen
if that makes any sense to you

--
Nick Keighley

Sep 15 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
117
by: Peter Olcott | last post by:
www.halting-problem.com
17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.