Plzz Explain !!

Discussion in 'C Programming' started by dev_cool, Aug 20, 2006.

  1. dev_cool

    dev_cool Guest

    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

    /******here is the code*****/
    int l;
    int main(O)
    return 0;
    /*******program ends*******/

    Plzz help me, I cannot figure out anything abt this code. thx in
    dev_cool, Aug 20, 2006
    1. Advertisements

  2. dev_cool

    pemo Guest

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

    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.


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

    int i = 2 - l;

    i = n;
    printf("%8d", n);

    i = 1;

    l = l - 1;

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

    l = n;


    return 0;
    pemo, Aug 20, 2006
    1. Advertisements

  3. dev_cool

    spibou Guest

    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

    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
    spibou, Aug 20, 2006
  4. Hint: It doesn't.
    Non-standard, unused header.
    Global variable - bad style.
    Syntax error.
    Poor spacing, and many more syntax errors.
    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.
    Andrew Poelstra, Aug 20, 2006
  5. dev_cool

    spibou Guest

    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.
    spibou, Aug 20, 2006
  6. dev_cool

    spibou Guest

    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.
    spibou, Aug 20, 2006
  7. 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.
    Harald van =?UTF-8?B?RMSzaw==?=, Aug 20, 2006
  8. dev_cool

    Flash Gordon Guest

    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.
    Flash Gordon, Aug 20, 2006
  9. dev_cool

    Malcolm Guest

    I am not surprised.

    A readable for() loop is always in the form

    /* 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.
    Malcolm, Aug 20, 2006
  10. 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.
    Andrew Poelstra, Aug 21, 2006
  11. dev_cool

    Flash Gordon Guest

    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.
    I agree that this is a stupid thing to do.
    In general I would agree with you.
    Flash Gordon, Aug 21, 2006
  12. 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.
    Keith Thompson, Aug 21, 2006
  13. 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
    Andrew Poelstra, Aug 21, 2006
  14. dev_cool

    spibou Guest

    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
    spibou, Aug 21, 2006
  15. dev_cool

    dev_cool Guest

    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.
    dev_cool, Aug 21, 2006
  16. dev_cool

    dev_cool Guest

    Thankz harald, your post actually explains a lot.
    dev_cool, Aug 21, 2006
  17. dev_cool

    pete Guest

    for (ctr = 0 ; (ch = getc(fh)) != EOF ; ctr++)
    pete, Aug 21, 2006
  18. dev_cool

    CBFalconer Guest

    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:
    CBFalconer, Aug 21, 2006
  19. dev_cool

    Default User Guest

    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or:

    Default User, Aug 21, 2006
  20. 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.
    Andrew Poelstra, Aug 21, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.