Programs without semicolon

Discussion in 'C Programming' started by subnet, May 11, 2004.

  1. subnet

    subnet Guest

    I'm not a C expert, but I've seen this topic discussed in the
    newsgroup several times. What's the deal with writing such programs?
    Why are they considered so interesting?

    Thanks
     
    subnet, May 11, 2004
    #1
    1. Advertising

  2. subnet <> scribbled the following:
    > I'm not a C expert, but I've seen this topic discussed in the
    > newsgroup several times. What's the deal with writing such programs?
    > Why are they considered so interesting?


    Beats me. Maybe some professor is handing out such assignments like
    they're going out of fashion, which I certainly hope they are.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "'It can be easily shown that' means 'I saw a proof of this once (which I didn't
    understand) which I can no longer remember'."
    - A maths teacher
     
    Joona I Palaste, May 11, 2004
    #2
    1. Advertising

  3. On Tue, 11 May 2004, subnet wrote:

    > I'm not a C expert, but I've seen this topic discussed in the
    > newsgroup several times. What's the deal with writing such programs?
    > Why are they considered so interesting?


    It is hard to do without getting creative. The idea is that you really
    have to know the language and have a bit of creativity to be able to write
    such a program.

    The other thought is that if you can write such a program you 1) know a
    lot about the language and 2) you can recall the appropriate information.

    For example, everyone with a degree in Computer Science learned about
    recursion. If I pose a challenge that requires recursion to solve it not
    everyone will be able to figure it out even though they have all had
    training on recursion. Having training is one thing. Using it effectively
    is what employers are really looking for.

    The truth of the matter is that many people are just good at using a
    search engine or memorizing questions that people like to use in
    interviews.

    One other aspect of these questions is that once I know what the answer is
    I can impress my friends by asking them the same question and watch them
    struggle with finding the answer.

    The specific challenge of writing a program that has no semicolons comes
    from the fact that everyone is taught that:

    int main(void)
    {
    return 0;
    }

    is a minimal program. You start with this and build from here. If this
    really is the simpliest program and there is already one semicolon in it,
    how do you get rid of the semicolon?

    int main(void)
    {
    return 0
    }

    This will not compile. You actually have to make the code more complex to
    be able to remove the semicolon.

    --
    Send e-mail to: darrell at cs dot toronto dot edu
    Don't send e-mail to
     
    Darrell Grainger, May 11, 2004
    #3
  4. subnet

    Dan Pop Guest

    In <> (Darrell Grainger) writes:

    >On Tue, 11 May 2004, subnet wrote:
    >
    >> I'm not a C expert, but I've seen this topic discussed in the
    >> newsgroup several times. What's the deal with writing such programs?
    >> Why are they considered so interesting?

    >
    >It is hard to do without getting creative. The idea is that you really
    >have to know the language and have a bit of creativity to be able to write
    >such a program.


    Not really. You know from start that your main function can't contain
    any statement or declaration, except the ones of its two parameters.
    There is nothing useful you can do in the declaration of argc, so your
    only remaining hope is argv. After this trivial analysis, the solution
    becomes obvious to anyone familiar with C99.

    The C89 reasoning is a bit more complex, but still no big deal:
    The only statement that doesn't require a terminating semicolon is the
    empty block statement. So you can make your function call in the
    controlling expression of an if or while statement that conditionally
    executes an empty block statement. The while statement even allows
    programming loops.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, May 11, 2004
    #4
  5. subnet

    PlasmaDragon Guest

    > int main(void)
    > {
    > return 0
    > }
    >
    > This will not compile. You actually have to make the code more complex to
    > be able to remove the semicolon.


    No, you will have to make it simpler:

    int main(){}

    compiles fine on GCC.
     
    PlasmaDragon, May 11, 2004
    #5
  6. subnet

    Richard Bos Guest

    (PlasmaDragon) wrote:

    > > int main(void)
    > > {
    > > return 0
    > > }
    > >
    > > This will not compile. You actually have to make the code more complex to
    > > be able to remove the semicolon.

    >
    > No, you will have to make it simpler:
    >
    > int main(){}
    >
    > compiles fine on GCC.


    It compiles fine, but it returns garbage.

    Richard
     
    Richard Bos, May 12, 2004
    #6
  7. subnet

    Dan Pop Guest

    In <> (Richard Bos) writes:

    > (PlasmaDragon) wrote:
    >
    >> > int main(void)
    >> > {
    >> > return 0
    >> > }
    >> >
    >> > This will not compile. You actually have to make the code more complex to
    >> > be able to remove the semicolon.

    >>
    >> No, you will have to make it simpler:
    >>
    >> int main(){}
    >>
    >> compiles fine on GCC.

    >
    >It compiles fine, but it returns garbage.


    Who says it shouldn't return garbage?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, May 12, 2004
    #7
  8. subnet

    PlasmaDragon Guest

    > It compiles fine, but it returns garbage.
    >
    > Richard


    Returns fine for me. Here's a test program:

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    int x;
    x=system("nosemicolon");
    printf("%d",x);
    printf("\n\n");

    system("PAUSE");
    }

    Where "nosemicolon" is the name of your program that doesn't have a
    semicolon. I always get the output 0.
     
    PlasmaDragon, May 12, 2004
    #8
  9. subnet

    Alan Balmer Guest

    On 12 May 2004 10:41:04 -0700, (PlasmaDragon)
    wrote:

    >> It compiles fine, but it returns garbage.
    >>
    >> Richard

    >
    >Returns fine for me. Here's a test program:
    >
    >#include <stdio.h>
    >#include <stdlib.h>
    >
    >int main()
    >{
    > int x;
    > x=system("nosemicolon");
    > printf("%d",x);
    > printf("\n\n");
    >
    > system("PAUSE");
    >}
    >
    >Where "nosemicolon" is the name of your program that doesn't have a
    >semicolon. I always get the output 0.


    What you're printing is the return value of system, which is
    implementation-defined and may or may not reflect the return value of
    nosemicolon.

    BTW, why

    printf("%d",x);
    printf("\n\n");

    rather than

    printf("%d\n\n", x);

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, May 12, 2004
    #9
  10. On Wed, 12 May 2004, PlasmaDragon wrote:
    >

    [please don't snip attributions, PlasmaDragon]
    > > It compiles fine, but it returns garbage.
    > >
    > > Richard

    >
    > Returns fine for me. Here's a test program:

    <snip>
    > I always get the output 0.


    Really? Prove it!

    Test programs prove nothing, by definition. Besides,
    you're arguing about a stupid quirk that's easily figured
    out by consulting the Standards or a relevant FAQ.

    In C89/C90/C95, falling off the end of 'main' returns
    an implementation-defined value to the system.
    In C99, falling off the end of 'main' is exactly equivalent
    to returning 0 via 'return 0;' or 'exit(0);'.

    This appears to be one of those cases where the C89 bigots
    and the C99 bigots are simply butting heads for the heck of
    it. If you guys would just say, "This answer refers to the
    standard of 1989" and "This answer refers to the standard of
    1999," threads like this one simply wouldn't happen.

    -Arthur
     
    Arthur J. O'Dwyer, May 13, 2004
    #10
  11. Alan Balmer <> wrote in message news:<>...
    > On 12 May 2004 10:41:04 -0700, (PlasmaDragon)
    > wrote:
    >
    > >> It compiles fine, but it returns garbage.
    > >>
    > >> Richard

    > >
    > >Returns fine for me. Here's a test program:
    > >
    > >#include <stdio.h>
    > >#include <stdlib.h>
    > >
    > >int main()
    > >{
    > > int x;
    > > x=system("nosemicolon");
    > > printf("%d",x);
    > > printf("\n\n");
    > >
    > > system("PAUSE");
    > >}
    > >
    > >Where "nosemicolon" is the name of your program that doesn't have a
    > >semicolon. I always get the output 0.

    >
    > What you're printing is the return value of system, which is
    > implementation-defined and may or may not reflect the return value of
    > nosemicolon.


    The behaviour is effectively undefined. Once you call system with a
    non-null string, you potentially lose the guarantee of conforming
    behaviour (assuming system even returns).

    --
    Peter
     
    Peter Nilsson, May 13, 2004
    #11
  12. subnet

    Dan Pop Guest

    In <> "Arthur J. O'Dwyer" <> writes:

    > In C89/C90/C95, falling off the end of 'main' returns
    >an implementation-defined value to the system.


    Nope, the value is *undefined*.

    If the main function executes a return that specifies no value,
    the termination status returned to the host environment is undefined.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, May 13, 2004
    #12
  13. subnet

    Dan Pop Guest

    In <> (Peter Nilsson) writes:

    >The behaviour is effectively undefined.


    The behaviour is mereley defined by another document.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, May 13, 2004
    #13
  14. "Arthur J. O'Dwyer" <> wrote ...

    > Test programs prove nothing, by definition. Besides,
    > you're arguing about a stupid quirk that's easily figured
    > out by consulting the Standards or a relevant FAQ.
    >
    > In C89/C90/C95, falling off the end of 'main' returns
    > an implementation-defined value to the system.
    > In C99, falling off the end of 'main' is exactly equivalent
    > to returning 0 via 'return 0;' or 'exit(0);'.


    A reference to a section in the standards to substantiate
    your claim(s) would be appreciated here.

    ISO Section 5.1.2.2.3#1 starts as follows:
    "If the return type of the main function is a type compatible
    with int,..." [snipped for brevity]

    Further on, ISO Section 6.8.6.4#1 says:
    "A return statement with an expression shall not appear in a
    function whose return type is void. A return statement without
    an expression shall only appear in a function whose return
    type is void."

    CMIIW, but my limited intellect says that this implies that
    'main' expects a return statement [or exit(...) for the pedants].


    --
    Devarshi Chatterjee.
     
    Devarshi Chatterjee, May 13, 2004
    #14
  15. Devarshi Chatterjee wrote:
    > "Arthur J. O'Dwyer" <> wrote ...
    >> In C99, falling off the end of 'main' is exactly equivalent
    >> to returning 0 via 'return 0;' or 'exit(0);'.

    >
    > A reference to a section in the standards to substantiate
    > your claim(s) would be appreciated here.
    >
    > ISO Section 5.1.2.2.3#1 starts as follows:
    > "If the return type of the main function is a type compatible
    > with int,..." [snipped for brevity]


    ... a return from the initial call to the main function is
    equivalent to calling the exit function with the value returned by
    the main function as its argument; reaching the } that terminates
    the main function returns a value of 0.
     
    Jeremy Yallop, May 13, 2004
    #15
  16. subnet

    Dan Pop Guest

    In <> (Devarshi Chatterjee) writes:

    >"Arthur J. O'Dwyer" <> wrote ...
    >
    >> Test programs prove nothing, by definition. Besides,
    >> you're arguing about a stupid quirk that's easily figured
    >> out by consulting the Standards or a relevant FAQ.
    >>
    >> In C89/C90/C95, falling off the end of 'main' returns
    >> an implementation-defined value to the system.
    >> In C99, falling off the end of 'main' is exactly equivalent
    >> to returning 0 via 'return 0;' or 'exit(0);'.

    >
    >A reference to a section in the standards to substantiate
    >your claim(s) would be appreciated here.
    >
    >ISO Section 5.1.2.2.3#1 starts as follows:
    >"If the return type of the main function is a type compatible
    >with int,..." [snipped for brevity]


    You should have read the text you've snipped, because this is what it
    says:

    ... a return from the initial call to the main function is
    equivalent to calling the exit function with the value returned
    by the main function as its argument;10) reaching the } that
    ^^^^^^^^^^^^^^^^^^^
    terminates the main function returns a value of 0.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    >Further on, ISO Section 6.8.6.4#1 says:
    >"A return statement with an expression shall not appear in a
    >function whose return type is void. A return statement without
    >an expression shall only appear in a function whose return
    >type is void."
    >
    >CMIIW, but my limited intellect says that this implies that
    >'main' expects a return statement [or exit(...) for the pedants].


    Your intellect is too limited: nothing in 6.8.6.4#1 forbids a function
    returning something to terminate without a return statement. And
    5.1.2.2.3#1 *explicitly* documents what happens when a main defined as
    returning int terminates without a return statement. If you're
    curious about what happens in other such cases, the answer is provided
    by 6.9.1#12:

    12 If the } that terminates a function is reached, and the value
    of the function call is used by the caller, the behavior is
    undefined.

    Therefore, the following is a strictly conforming C99 program, with a
    well defined termination status. In C89, the termination status would be
    undefined.

    #include <stdio.h>

    int foo(void) { printf("hello world\n"); }

    int main() { foo(); }

    I would expect a good compiler to emit a warning for each function
    defined in this program, but the standard requires no diagnostic.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, May 13, 2004
    #16
  17. subnet

    Ralmin Guest

    "Dan Pop" <> wrote:
    > If you're curious about what happens in other such cases, the answer
    > is provided by 6.9.1#12:
    >
    > 12 If the } that terminates a function is reached, and the
    > value of the function call is used by the caller, the
    > behavior is undefined.
    >
    > Therefore, the following is a strictly conforming C99 program, with
    > a well defined termination status. In C89, the termination status
    > would be undefined.


    My copy of C89 has equivalent wording in 3.6.6.4#4:

    If a return statement without an expression is executed,
    and the value of the function call is used by the caller,
    the behavior is undefined. Reaching the } that terminates
    a function is equivalent to executing a return statement
    without an expression.

    Following this logic, since the value of the function call to main is used
    by its caller, the behaviour is undefined. I don't think there is such a
    thing as an "undefined termination status". In this case, the program has
    "undefined behaviour".

    > #include <stdio.h>
    >
    > int foo(void) { printf("hello world\n"); }
    >
    > int main() { foo(); }
    >
    > I would expect a good compiler to emit a warning for each function
    > defined in this program, but the standard requires no diagnostic.


    Agreed.

    --
    Simon.
     
    Ralmin, May 15, 2004
    #17
  18. subnet

    Dan Pop Guest

    In <> "Ralmin" <> writes:

    >"Dan Pop" <> wrote:
    >> If you're curious about what happens in other such cases, the answer
    >> is provided by 6.9.1#12:
    >>
    >> 12 If the } that terminates a function is reached, and the
    >> value of the function call is used by the caller, the
    >> behavior is undefined.
    >>
    >> Therefore, the following is a strictly conforming C99 program, with
    >> a well defined termination status. In C89, the termination status
    >> would be undefined.

    >
    >My copy of C89 has equivalent wording in 3.6.6.4#4:
    >
    > If a return statement without an expression is executed,
    > and the value of the function call is used by the caller,
    > the behavior is undefined. Reaching the } that terminates
    > a function is equivalent to executing a return statement
    > without an expression.
    >
    >Following this logic, since the value of the function call to main is used
    >by its caller, the behaviour is undefined. I don't think there is such a
    >thing as an "undefined termination status". In this case, the program has
    >"undefined behaviour".


    Why didn't you bother checking your copy of C89? You'd have learned that
    there is such thing as "undefined termination status" and, therefore, the
    program doesn't invoke undefined behaviour:

    "Program termination"

    A return from the initial call to the main function is equivalent
    to calling the exit function with the value returned by the main
    function as its argument. If the main function executes a return that
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    specifies no value, the termination status returned to the host
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    environment is undefined.
    ^^^^^^^^^^^^^^^^^^^^^^^^^

    According to your own quote, falling off the end of main is the equivalent
    to executing a return with no value.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, May 17, 2004
    #18
  19. subnet

    avsharath

    Joined:
    Jun 1, 2007
    Messages:
    1
    Location:
    India
    Questions like these originate from fresh graduates or students in India who are preparing for interviews. Most of the interviewers here in India ask such stupid questions, they have no idea what programming is about.

    I've maintain a FAQ website for C, and it contains many such questions and answers there. It is for programming forums in India, and hence such questions had to be put up in the FAQ. You can find the website here: http://prokutfaq.byethost15.com/CFAQ
     
    avsharath, Jun 1, 2007
    #19
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    1,672
    Dave Thompson
    Jan 10, 2005
  2. ankursinha
    Replies:
    33
    Views:
    22,391
    Keith Thompson
    Feb 25, 2004
  3. Replies:
    3
    Views:
    363
    Joe Greer
    Oct 10, 2007
  4. thomas
    Replies:
    3
    Views:
    1,357
    James Kanze
    Feb 17, 2008
  5. Peter Michaux

    Cookies: semicolon vs. semicolon-space

    Peter Michaux, Dec 10, 2006, in forum: Javascript
    Replies:
    3
    Views:
    224
    Michael Winter
    Dec 11, 2006
Loading...

Share This Page