Plzz Explain !!

D

dev_cool

hello everybody,
i'm a new member of this group, this is my first ever post. i've found
a c program that prints prime numbers through an infinite loop. But I
really don't understand the code. Anybody plzz explain how the code
works?

/******here is the code*****/
#include<stdio.h>
#include<conio.h>
int l;
int main(O)
{
for(;2-l?O:printf("%8d",O);l=O%--l?l:++O);
return 0;
}
/*******program ends*******/

Plzz help me, I cannot figure out anything abt this code. thx in
advance.
 
P

pemo

dev_cool said:
hello everybody,
i'm a new member of this group, this is my first ever post. i've found
a c program that prints prime numbers through an infinite loop. But I
really don't understand the code. Anybody plzz explain how the code
works?

/******here is the code*****/
#include<stdio.h>
#include<conio.h>
int l;
int main(O)
{
for(;2-l?O:printf("%8d",O);l=O%--l?l:++O);
return 0;
}
/*******program ends*******/

Plzz help me, I cannot figure out anything abt this code. thx in
advance.

Laid it out a little clearer? Don't know - did my head in working through
it!

I'm sure that someone will soon come along with a better re-write PLUS an
explanation of the algorithm being used here!

Of course, as the values grow this will get into undefined behaviour -
integer overflow etc.

#include<stdio.h>

int main(void)
{
int l = 0;
int n = 1;

for(;;)
{
int i = 2 - l;

if(i)
{
i = n;
}
else
{
printf("%8d", n);

i = 1;
}

if(i)
{
l = l - 1;

if(!(n % l))
{
n = n + 1;

l = n;
}

}
}

return 0;
}
 
S

spibou

dev_cool said:
hello everybody,
i'm a new member of this group, this is my first ever post. i've found
a c program that prints prime numbers through an infinite loop. But I
really don't understand the code. Anybody plzz explain how the code
works?

/******here is the code*****/
#include<stdio.h>
#include<conio.h>
int l;
int main(O)
{
for(;2-l?O:printf("%8d",O);l=O%--l?l:++O);
return 0;
}
/*******program ends*******/

Plzz help me, I cannot figure out anything abt this code. thx in
advance.

conio.h is not a standard C header plus the programme
doesn't need it. The main function should be either
main(void) or with the usual 2 arguments ; main(O) is
illegal.

Now that we have dispensed with the technicalities here's
a non-obfuscated rewrite of the programme with initialization
added. I have also replaced the variable O in the original by a
to aid readability.

#include <stdio.h>

int main(void) {
int a = 1 , l=52 ;
while (1) {
/* printf("a is %d l is %d\n",a,l) ; */
if (l != 2) {
if (a == 0) return 0 ;
} else printf("%8d",a) ;
l-- ;
if ( a % l == 0 ) {
a++ ;
l = a ;
}
}
}

The algorithm is straightforward. Just remove the
comments from the printf line , run it and everything
will become clear.

Note that the initial value of l cannot be 1. If l is negative
then -l must be smaller than a for the programme to work
correctly. Otherwise the initial value of l does not matter
which causes surprise in the initial obfuscated version
of the programme. I imagine it was an intended effect.

Spiros Bousbouras
 
A

Andrew Poelstra

dev_cool said:
hello everybody,
i'm a new member of this group, this is my first ever post. i've found
a c program that prints prime numbers through an infinite loop. But I
really don't understand the code. Anybody plzz explain how the code
works?

Hint: It doesn't.
/******here is the code*****/
#include<stdio.h>
#include<conio.h>
Non-standard, unused header.
Global variable - bad style.
int main(O)
Syntax error.
{
for(;2-l?O:printf("%8d",O);l=O%--l?l:++O);
Poor spacing, and many more syntax errors.
return 0;
}
/*******program ends*******/

Plzz help me, I cannot figure out anything abt this code. thx in
advance.

Use proper grammer. It will make people believe you have an IQ over
20, something that as of now most people don't. "Plzz" is not a word,
nor is "i", "abt", "thx", or "dev". Also, you forgot to capitalize
all too frequently.
 
S

spibou

Andrew said:
Poor spacing, and many more syntax errors.

Actually no , this part works just fine. And the code
is obviously intentionally obfuscated so it seems a
bit silly to complain about poor spacing.
 
S

spibou

Actually no , this part works just fine.

To be more precise it works if O has been declared
as an int variable. I assume that in whatever compiler
the opening poster used to compile the code , writing
int main(O) defines O as an int.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Andrew said:
Syntax error.

It's an old-style definition of main(), with a single parameter named O
(uppercase o). Its type is not specified, which is a constraint violation
in C99, and in C89 it defaults to int, so the behaviour is undefined
because main() should take either zero or two parameters. It is not a
syntax error either way, though.
 
F

Flash Gordon

To be more precise it works if O has been declared
as an int variable. I assume that in whatever compiler
the opening poster used to compile the code , writing
int main(O) defines O as an int.

Actually it is an old style function definition where the types of the
parameters were declared outside the parenthesis and the implicit int
allowing you to not bother specifying a type at all if it was an int
parameter (implicit int has been removed in C99). The problem is that
the standard defined main as taking either no parameters or two
parameters, it does not define it as taking one parameter.
 
M

Malcolm

dev_cool said:
hello everybody,
i'm a new member of this group, this is my first ever post. i've found
a c program that prints prime numbers through an infinite loop. But I
really don't understand the code. Anybody plzz explain how the code
works?

/******here is the code*****/
#include<stdio.h>
#include<conio.h>
int l;
int main(O)
{
for(;2-l?O:printf("%8d",O);l=O%--l?l:++O);
return 0;
}
/*******program ends*******/

Plzz help me, I cannot figure out anything abt this code. thx in
advance.
I am not surprised.

A readable for() loop is always in the form

for(i=0;i<N;i++)
{
/* loop body executed N times, with i ranging from 0 to N-1 */
}

However C will allow you to put absolutely anything within the statements.
The loop terminates when the middle bit evaluates to zero. So you can do
stupid things like putting printf()s in the condition.
Occasionally you will see for loops used sensibly with compex conditions,
but only very occasionally. My own view is that it is never the correct
thing to put operations with non- control side-effects in a for loop, but
other regulars may disagree for a few exceptional situations.
 
A

Andrew Poelstra

Malcolm said:
However C will allow you to put absolutely anything within the statements.
The loop terminates when the middle bit evaluates to zero. So you can do
stupid things like putting printf()s in the condition.
Occasionally you will see for loops used sensibly with compex conditions,
but only very occasionally. My own view is that it is never the correct
thing to put operations with non- control side-effects in a for loop, but
other regulars may disagree for a few exceptional situations.

How do you feel about

for (ch; ch != EOF; ch = getc (fh))

Or variations on the theme? This extends nicely if you want to add a
variable to keep track of how many bytes are read.
 
F

Flash Gordon

Malcolm said:
I am not surprised.

A readable for() loop is always in the form

for(i=0;i<N;i++)
{
/* loop body executed N times, with i ranging from 0 to N-1 */
}

That is a matter of opinion. I find loops of the form
for (node=start; node!=NULL; node = node->next) {
/* Loop body executed for all nodes */
}

to be readable as well.
However C will allow you to put absolutely anything within the statements.
The loop terminates when the middle bit evaluates to zero. So you can do
stupid things like putting printf()s in the condition.

I agree that this is a stupid thing to do.
Occasionally you will see for loops used sensibly with compex conditions,
but only very occasionally. My own view is that it is never the correct
thing to put operations with non- control side-effects in a for loop, but
other regulars may disagree for a few exceptional situations.

In general I would agree with you.
 
K

Keith Thompson

Andrew Poelstra said:
How do you feel about

for (ch; ch != EOF; ch = getc (fh))

Or variations on the theme? This extends nicely if you want to add a
variable to keep track of how many bytes are read.

The first clause, ch, just evaluates ch and throws away the result.

The second clause, ch != EOF, is evaluated at the top of the loop,
before ch has been assigned a value, because the assignment
"ch = getc(fh)" is in the third clause.

The usual idiom is

while ((ch = getc(fh)) != EOF)

There's only one clause (though a moderately complex one), so a while
loop is just the thing.
 
A

Andrew Poelstra

Keith Thompson said:
The first clause, ch, just evaluates ch and throws away the result.

The second clause, ch != EOF, is evaluated at the top of the loop,
before ch has been assigned a value, because the assignment
"ch = getc(fh)" is in the third clause.

The usual idiom is

while ((ch = getc(fh)) != EOF)

There's only one clause (though a moderately complex one), so a while
loop is just the thing.

ch = getc (fh);
for (ctr = 0; ch != EOF; ch = getc (fh), ctr++)
{
/* ctr is a "elegantly" coded counter here. */
}

I wouldn't do that, but OTOH I wouldn't mind reading it in other people's
code.
 
S

spibou

Andrew said:
ch = getc (fh);
for (ctr = 0; ch != EOF; ch = getc (fh), ctr++)
{
/* ctr is a "elegantly" coded counter here. */
}

If you're going to do that you may as well right it as
for (ch = getc(fh) , ctr = 0 ; ch != EOF ; ch = getc(fh), ctr++)

I find displeasing the fact that ch = getc(fh) gets repeated
twice which you avoid with the while version but pleasing
the fact that ctr and ch get initialized and updated next to
each other , something you again don't get with the while
version.
 
D

dev_cool

Dear Andrew,
Thankz for replying. First of all, I didn't remember if I ever said
that I've an iq over 20 nor that I think its a group of learning
english....and I simply don't care what ppl like u who have a greater
iq what think abt me. Its enough 4 u to know that I come here 4 solving
my c probs...u helped me thx a lot....don't expect anything more.
 
D

dev_cool

Thankz harald, your post actually explains a lot.
It's an old-style definition of main(), with a single parameter named O
(uppercase o). Its type is not specified, which is a constraint violation
in C99, and in C89 it defaults to int, so the behaviour is undefined
because main() should take either zero or two parameters. It is not a
syntax error either way, though.
 
P

pete

If you're going to do that you may as well right it as
for (ch = getc(fh) , ctr = 0 ; ch != EOF ; ch = getc(fh), ctr++)

I find displeasing the fact that ch = getc(fh) gets repeated
twice which you avoid with the while version but pleasing
the fact that ctr and ch get initialized and updated next to
each other , something you again don't get with the while
version.

for (ctr = 0 ; (ch = getc(fh)) != EOF ; ctr++)
 
C

CBFalconer

dev_cool said:
Thankz for replying. First of all, I didn't remember if I ever said
that I've an iq over 20 nor that I think its a group of learning
english....and I simply don't care what ppl like u who have a greater
iq what think abt me. Its enough 4 u to know that I come here 4 solving
my c probs...u helped me thx a lot....don't expect anything more.

You need to learn to post properly, which means not top-posting,
and snipping quotations not germane to your reply. Also, this
cool-dude speak does not cut it here. Use proper English and
spelling, up to your abilities (people will make allowance for
language barriers). These silly abbreviations, u, ppl, 4, etc.
just make your article hard to read.

Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
A

Andrew Poelstra

dev_cool said:
Dear Andrew,
Thankz for replying. First of all, I didn't remember if I ever said
that I've an iq over 20 nor that I think its a group of learning
english....and I simply don't care what ppl like u who have a greater
iq what think abt me. Its enough 4 u to know that I come here 4 solving
my c probs...u helped me thx a lot....don't expect anything more.

Watch this:

------------------
Dear Andrew,
Thanks for replying. First of all, I don't remember if I've said
that I have an IQ greater than 20, nor do I think that this is a
group for learning English. I simply don't care what people like you
who have a greater IQ think of me: It's enough for you to know that
I come here to solve my C problems. You've helped me a lot, so thanks,
and I don't expect anything more.
------------------

Note that had you used proper grammar, only the first and last
sentences would be necessary. Also, my version can be read in
about 5 seconds, whereas yours took me over twice as long. Your
message is written once, but read many times.

Chuck Falconer has pointed out many other problems with this post
and provided links to help you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top