Thread: A C Puzzler...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    79

    A C Puzzler...

    The program shall display to stdout using printf the string "Hello C"

    The program shall not contain any or a single semi-colon.

    The program should compile on gcc without errors using switches -Wall -Pedantic -std=c99

    The switch -std=c99 is optional and c89 may display a warning,
    that control reaches end of non-void function. This is acceptable.

    Compilation on any other C compiler system that results in an executable
    that adheres to the first and second directives is also acceptable.

    First correct entry wins.
    Last edited by c99; 02-26-2004 at 04:33 AM.
    R.I.P C89

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is old hat. There's even a FAQ on it.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This would be better placed in the Contests forum.
    My best code is written with the delete key.

  4. #4
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Code:
    #include <stdio.h>
    
    int 
    main(void)
    {
         while( printf("Hello World\n"), 0)
         {
         }
    }
    There would be different versions based on the different loops. Is there an essentially different method?
    The one who says it cannot be done should never interrupt the one who is doing it.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    19
    Originally posted by pinko_liberal
    Code:
    #include <stdio.h>
    
    int 
    main(void)
    {
         while( printf("Hello World\n"), 0)
         {
         }
    }
    i don't have here any compiler - but i think this won't work! shouldn't it be

    Code:
    while( printf("Hello World\n"))
         {
         }
    ?
    i would take the following:

    Code:
    if( printf("Hello World\n") ) {}

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Plus stdio.h contains semi-colons, so I guess that shouldn't be allowed.

    If you can #include then:

    noco.c
    Code:
    #include <stdio.h>
    
    int main()
    {
      ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
      ;;     ;printf("Hello C");     ;;
      ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
      ;;;;;;;;;;;;;;;;
      ;;  return 0; ;;
      ;;;;;;;;;;;;;;;;
    }
    noco_wrap.c
    Code:
    #include "noco.c"
    gcc -ansi -pedantic -Wall -std=c99 noco_wrap.c
    Last edited by major_blagger; 02-26-2004 at 03:33 PM.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    while( printf("Hello World\n"))
         {
         }
    printf returns the number of characters outputted. As such it would be in an infinate loop.

    edit: Made the sentance clearer
    Last edited by Thantos; 02-26-2004 at 04:46 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by wudmx
    [B]Originally posted by pinko_liberal
    Code:
    #include <stdio.h>
    
    int 
    main(void)
    {
         while( printf("Hello World\n"), 0)
         {
         }
    }
    i don't have here any compiler - but i think this won't work! shouldn't it be
    It works due to the use of the comma operator. It evaluates each argument, and in doing so, the printf is executed. It also evaluates zero, which terminates the loop.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    A simpler variation:

    Code:
     int main()
    {
         while (printf("Hello World!") && 0){}
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Originally posted by quzah
    It works due to the use of the comma operator. It evaluates each argument, and in doing so, the printf is executed. It also evaluates zero, which terminates the loop.
    It's also note-worthy to add that the comma operator is evaluated
    from left to right meaning that the printf in pinko's while loop was
    evaluated first, then the expression on the right side of the comma was evaluated.

    pinko_liberal was the first to satisfy all conditions, pinko wins.
    Code:
     while (printf("Hello C"), 0) {}
    wudmx also supplied a correct entry,
    Code:
    if (printf("Hello C")) {}
    Sebastiani also supplied a correct entry,
    Code:
     while (printf("Hello C") && 0) {}
    R.I.P C89

  11. #11
    Registered User
    Join Date
    Dec 2002
    Posts
    19
    Originally posted by quzah
    It works due to the use of the comma operator. It evaluates each argument, and in doing so, the printf is executed. It also evaluates zero, which terminates the loop.
    of course,you're right! thanks for the lecture

    bye
    wudmx

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    It's also note-worthy to add that the comma operator is evaluated
    from left to right meaning that the printf in pinko's while loop was
    evaluated first, then the expression on the right side of the comma was evaluated.
    Its also note-worthy to add that EVERY expression is evaluated from left to right not just ones using the comma.

    My entry. Since you never stated that it had to be done IN C:
    Code:
            .file   "bt.c"
            .version        "01.01"
    gcc2_compiled.:
    .section        .rodata
    .LC0:
            .string "Hello World\n"
    .text
            .align 4
    .globl main
            .type    main,@function
    main:
            pushl %ebp
            movl %esp,%ebp
            subl $8,%esp
            nop
            .p2align 4,,7
    .L3:
            addl $-12,%esp
            pushl $.LC0
            call printf
            addl $16,%esp
            movl %eax,%eax
            testl %eax,%eax
            je .L5
            jmp .L4
            .p2align 4,,7
    .L5:
            jmp .L3
            .p2align 4,,7
    .L4:
    .L2:
            leave
            ret
    .Lfe1:
            .size    main,.Lfe1-main
            .ident  "GCC: (GNU) 2.95.4 20011002 (Debian prerelease)"
    command line: gcc -o bt bt.s -Wall -pedantic
    (btw next time make sure on the capitalization of your arguments)
    No warnings nor errors.

  13. #13
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Originally posted by Thantos
    Its also note-worthy to add that EVERY expression
    is evaluated from left to right not just ones using the comma.
    Now, at which point did I say that the comma expression
    evaluates from left to right?

    I did however state that the comma operator is evaluated from left to right.

    It's understandable that you are a little confused. Oh! the P
    well if you didn't get that then you're just out of the loop.
    Last edited by c99; 02-27-2004 at 04:16 AM.
    R.I.P C89

  14. #14
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Thantos.

    Worked it out yet?

    We're talking about operator associativity, not expressional evaluation.
    Last edited by c99; 02-27-2004 at 04:12 AM.
    R.I.P C89

  15. #15
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Not everything evaluates from left to right. Take this as an exampple:
    Code:
    a = b = c = d = e = 3;
    That evaluates from right to left.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed