"Hello World" without semicolon with a difference

Discussion in 'C Programming' started by ankursinha, Feb 19, 2004.

  1. ankursinha

    ankursinha Guest

    Hi,

    Is it possible to write a C program that prints "Hello World" on screen without
    having a single semi-colon in the entire program?
    The extra constraint here is that u r not allowed to use if,while,switch etc.

    So far,i figured this could be done by insertint the printf statement in main
    as shown:

    int main(int argc=printf("Hello world")
    {
    }
    Note:This is not allowed in ANSI C,default arguments are more a C++ concept.

    Even this doesnt work.It compiles & runs but doesnt print anything.

    Is the solution to this question possible in c(or c++ for that matter)?
     
    ankursinha, Feb 19, 2004
    #1
    1. Advertisements

  2. I believe not unless you consider this a valid option:
    #include <stdio.h>

    #define WEE puts( "Hello World!" );

    int main( int argc, char * argv[] ) {
    WEE
    }


    ...in fact to print on the screen you need an instruction.. and
    in C every instruction has to be followed by a semi-colon.

    Best regards.

    --
    Roberto Nunnari -software engineer-
    mailto:
    http://www.nunnisoft.ch
    Residenza Boschetto 12 tel/fax: +41-91-6046511
    6935 Bosco Luganese """ mobile: +41-76-3208561
    Switzerland (o o)
    ========================oOO==(_)==OOo========================
     
    Roberto Nunnari, Feb 19, 2004
    #2
    1. Advertisements

  3. ankursinha

    Eric Sosman Guest

    Answer #1: No, because the C language has no concept of
    a "screen."

    Answer #2: Yes, as follows

    ??=include <stdio.h>
    int main(void) {
    puts ("Hello, world!");;
    return 10??'012;;
    ??>

    Observe that there are no single semi-colons.
    In that case, the problem is impossible. (Assumption: "etc"
    includes the open parenthesis character '('.)
    DYOH. Or if you won't DYOH, at least RTFW.
     
    Eric Sosman, Feb 19, 2004
    #3
  4. This invokes undefined behaviour, because variable length array types
    aren't compatible with normal array types, but it's the closest I
    could get. It's C99, obviously.

    #include <stdio.h>
    int main(int argc, char *argv[static printf("Hello World\n"), 0])
    {
    }

    There's also

    #error "Hello World"

    but I don't consider that a program.

    Jeremy.
     
    Jeremy Yallop, Feb 19, 2004
    #4
  5. The endless September:
    http://groups.google.com/groups?hl=...n&btnG=Google+Search&meta=group=comp.lang.c.*

    One should always consult the FAQ and search the newsgroups
    before posting.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c++/faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Feb 19, 2004
    #5
  6. ankursinha

    Guillaume Guest

    Is the solution to this question possible in c(or c++ for that matter)?

    You could try:


    int main(int argc, char **argv)
    {
    if (printf("Hello world!\n")) {}
    }


    Fits the bill?
     
    Guillaume, Feb 19, 2004
    #6
  7. ankursinha

    Guillaume Guest

    Damn, didn't remember you couldn't use 'if'.

    I'm afraid there isn't such a beast, then...
     
    Guillaume, Feb 19, 2004
    #7
  8. hehehe.. this is CLEVER!

    Could you please explain why that works? I mean.. why that printf
    statement gets executed? Has it something to do with the fact
    that static variables get initialized at start time?

    Best regards.

    --
    Roberto Nunnari -software engineer-
    mailto:
    http://www.nunnisoft.ch
    Residenza Boschetto 12 tel/fax: +41-91-6046511
    6935 Bosco Luganese """ mobile: +41-76-3208561
    Switzerland (o o)
    ========================oOO==(_)==OOo========================
     
    Roberto Nunnari, Feb 19, 2004
    #8
  9. ankursinha

    Dan Pop Guest

    When did the following statement disappear from his post?

    The extra constraint here is that u r not allowed to use
    if,while,switch etc.

    Dan
     
    Dan Pop, Feb 19, 2004
    #9
  10. ankursinha

    Dan Pop Guest

    With this extra constraint, the solution involves cheating, one way or
    another.

    fangorn:~/tmp 470> cat test.c
    #include <stdio.h>

    int main()
    {
    puts("Hello World") _
    }
    fangorn:~/tmp 471> gcc -D_=\; test.c
    fangorn:~/tmp 472> ./a.out
    Hello World

    Dan
     
    Dan Pop, Feb 19, 2004
    #10
  11. Why would you want to do that? Are you and Aniruddha taking the same
    class? (Aniruddha has been posting to comp.lang.c.moderated asking
    how to access command line arguments without using argc and argv, and
    how to emulate sizeof without using sizeof.)
     
    Keith Thompson, Feb 19, 2004
    #11
  12. Accessing command line arguments without using argc and argv is easy.
    Just name the parameters something else. =)
     
    Joona I Palaste, Feb 19, 2004
    #12
  13. That was one of the proposed solutions over in clcm, but the Aniruddha
    said it didn't fit the specification (without explaining why using
    argc and argv wasn't acceptable).

    I suspect some instructor in India is assigning these silly problems
    without making it sufficiently clear that asking Usenet to do your
    homework for you is not acceptable. (I'm not picking on India; of the
    two blatant examples of this I've seen, both posters have
    Indian-sounding names, one posted from a *.in address, and the other
    used a hotmail address.) That's just speculation; there may be no
    connection between the two posters, but the questions have the same
    feel to them.

    There may be legitimate reasons for these questions, or they may be
    intended as fun but meaningless challenges (I have no problem with
    that) but until I know *why* using the features of the language is not
    an acceptable solution I'm not inclined to be helpful.
     
    Keith Thompson, Feb 20, 2004
    #13
  14. ankursinha

    ankursinha Guest

    This code doesnt work(atleast on my pc).it says expression syntax &
    array bounds misssing.
    im using the old TurboC++ compiler.
    also,isnt argv supposed to have only one array size value when u
    declare it
    as : char *argv[].

    How is static printf ("Heloo") supposed to work.
    It gives an error even if it is given as a normal statement.

    static printf("hello");


    true.
     
    ankursinha, Feb 20, 2004
    #14
  15. ankursinha

    James Hu Guest

    This works on my system:

    $ cat hello.c
    # /*
    echo Hello World
    exit 0
    # */
    int main(void) {}
    $ ./hello.c
    Hello World
    $ cc hello.c
    $

    -- James
     
    James Hu, Feb 20, 2004
    #15
  16. On 19 Feb 2004 20:32:21 -0800, in comp.lang.c, ankursinha wrote:
    :> ankursinha wrote:
    :> > Is it possible to write a C program that prints "Hello World" on screen without
    :> > having a single semi-colon in the entire program?
    :> > The extra constraint here is that u r not allowed to use if,while,switch etc.
    :>
    <snip>
    :> #include <stdio.h>
    :> int main(int argc, char *argv[static printf("Hello World\n"), 0])
    :> {
    :> }
    :
    :This code doesnt work(atleast on my pc).it says expression syntax &
    :array bounds misssing. im using the old TurboC++ compiler.

    Get a new compiler. If you don't want to pay for one, get a free
    compiler like gcc (part of cygwin), mingw or lcc. I don't know whether
    any of these support C99 features yet. The latest version of gcc might.
    On *nix systems, I think gcc supports many of the C99 features.

    As for Jeremy's code, I am not sure how it works, but it does (with gcc
    3.2.2). I think the whole expression within [] is treated as the size of
    the array and since it is a comma separated expression, the LHS is
    evaluated, result discarded and, the RHS is evaluated.

    Someone please tell me if this is right. Also, why is static required
    there? Confused...

    Have a nice day,
    Pradeep
     
    R Pradeep Chandran, Feb 20, 2004
    #16
  17. ankursinha

    Dan Pop Guest

    Well, emulating sizeof is an enlightening exercise for anyone who has
    taken a course on basic C programming, while the current problem only
    has a sensible solution in C99.

    Dan
     
    Dan Pop, Feb 20, 2004
    #17
  18. ankursinha

    Dan Pop Guest

    That's the point: in his example, main() was defined without arguments!

    Dan
     
    Dan Pop, Feb 20, 2004
    #18
  19. ankursinha

    Dan Pop Guest

    ^^^^^^^^^^^^^^^^
    Are you reading impaired or merely a patent idiot?

    Dan
     
    Dan Pop, Feb 20, 2004
    #19
  20. ankursinha

    Dan Pop Guest

    Yes, argv is defined as a VLA (variable length array). It is not clear
    if C99 allows the second parameter of main() to be defined this way, so
    the code is not necessarily correct.
    It's another C99 feature, but it is not needed here. Without static,
    things would be even simpler:

    int main(int argc, char *argv[printf("Hello World\n")]) {}

    because the value used in the VLA definition is completely ignored after
    being evaluated for any side effects, the effective type of argv being
    char **, as usual.

    The big point, however, is whether C99 allows the second parameter of
    main() to be defined as a VLA.

    Dan
     
    Dan Pop, Feb 20, 2004
    #20
    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.