Re: (part 21) Han from China answers your C questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lew Pitcher

    Re: (part 21) Han from China answers your C questions

    On November 14, 2008 15:00, in comp.lang.c, Nomen Nescio (nobody@dizum.c om)
    wrote:
    its urgent.pls help me.
    >
    mona said:
    >hi,i urgently need a project including graphics,pointe rs.
    [snip]
    In the meantime, here's a program that meets the specifications:
    >
    #include <stdio.h>
    >
    int
    main(void)
    {
    /*
    * What we have here is a simple graphics output.
    * We satisfy the requirement for pointers in two ways:
    * (1) we have a graphics pointer (or arrow)
    * (2) the argument to printf() decays to a pointer
    */
    printf(" === \n");
    Overkill
    return 0;
    }
    Try

    #include <stdio.h>

    int main(void)
    {
    puts(" --- ")
    return 0;
    }

    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------


  • Andrey Tarasevich

    #2
    Re: (part 21) Han from China answers your C questions

    Lew Pitcher wrote:
    >
    #include <stdio.h>
    >
    int main(void)
    {
    puts(" --- ")
    return 0;
    }
    >
    Overkill

    #include <stdio.h>

    int main(void) {
    puts(" --")
    }

    --
    Best regards,
    Andrey Tarasevich

    Comment

    • Lew Pitcher

      #3
      Re: (part 21) Han from China answers your C questions

      On November 14, 2008 15:11, in comp.lang.c, Lew Pitcher
      (lpitcher@teksa vvy.com) wrote:

      [snip]
      >
      #include <stdio.h>
      >
      int main(void)
      {
      puts(" --- ")
      gaak! I missed a semicolon here.
      return 0;
      }
      >
      I promise to be more carefull next time

      --
      Lew Pitcher

      Master Codewright & JOAT-in-training | Registered Linux User #112576
      http://pitcher.digitalfreehold.ca/ | GPG public key available by request
      ---------- Slackware - Because I know what I'm doing. ------


      Comment

      • s0suk3@gmail.com

        #4
        Re: (part 21) Han from China answers your C questions

        On Nov 14, 3:28 pm, Andrey Tarasevich <andreytarasevi ch@hotmail.com>
        wrote:
        Lew Pitcher wrote:
        >
          #include <stdio.h>
        >
          int main(void)
          {
            puts("  --- ")
            return 0;
          }
        >
        Overkill
        >
           #include <stdio.h>
        >
           int main(void) {
             puts(" --")
           }
        Overkill. The #include line will bring in a bunch of stuff :-)

        int main(void) { int puts(const char *); puts(" --"); }

        Sebastian

        Comment

        • Ralf Damaschke

          #5
          Re: (part 21) Han from China answers your C questions

          s0suk3 wrote:
          On Nov 14, 3:28 pm, Andrey Tarasevich <andreytarasevi ch@hotmail.com>
          wrote:
          >Lew Pitcher wrote:
          >>
            #include <stdio.h>
          >>
            int main(void)
            {
              puts("  --- ")
              return 0;
            }
          >>
          >Overkill
          >>
          >   #include <stdio.h>
          >>
          >   int main(void) {
          >     puts(" --")
          >   }
          >
          Overkill. The #include line will bring in a bunch of stuff :-)
          >
          int main(void) { int puts(const char *); puts(" --"); }
          Overkill. No need for a prototype (even in C99).

          int main(void) { int puts(); puts(" --"); }

          -- Ralf

          Comment

          • Richard Heathfield

            #6
            Re: (part 21) Han from China answers your C questions

            Ralf Damaschke said:
            s0suk3 wrote:
            >
            >On Nov 14, 3:28 pm, Andrey Tarasevich <andreytarasevi ch@hotmail.com>
            >wrote:
            >>Lew Pitcher wrote:
            >>>
            >#include <stdio.h>
            >>>
            >int main(void)
            >{
            >puts(" --- ")
            >return 0;
            >}
            >>>
            >>Overkill
            >>>
            >>#include <stdio.h>
            >>>
            >>int main(void) {
            >>puts(" --")
            >>}
            >>
            >Overkill. The #include line will bring in a bunch of stuff :-)
            >>
            >int main(void) { int puts(const char *); puts(" --"); }
            >
            Overkill. No need for a prototype (even in C99).
            >
            int main(void) { int puts(); puts(" --"); }
            Overkill. No need for a prototype (even in C99).

            int main() { int puts(); puts(" --"; }

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • Harald van =?UTF-8?b?RMSzaw==?=

              #7
              Re: (part 21) Han from China answers your C questions

              On Sat, 15 Nov 2008 07:59:35 +0000, Ralf Damaschke wrote:
              s0suk3 wrote:
              >Overkill. The #include line will bring in a bunch of stuff :-)
              >>
              >int main(void) { int puts(const char *); puts(" --"); }
              >
              Overkill. No need for a prototype (even in C99).
              >
              int main(void) { int puts(); puts(" --"); }
              This doesn't convert the pointer to the string from char * to const char
              *. I doubt any implementation will even notice, but you need either the
              prototype, or you need to change the call to puts((const char *) " --");
              for it to be valid.

              Comment

              • Flash Gordon

                #8
                Re: (part 21) Han from China answers your C questions

                Harald van Dijk wrote, On 15/11/08 10:04:
                On Sat, 15 Nov 2008 07:59:35 +0000, Ralf Damaschke wrote:
                >s0suk3 wrote:
                >>Overkill. The #include line will bring in a bunch of stuff :-)
                >>>
                >>int main(void) { int puts(const char *); puts(" --"); }
                >Overkill. No need for a prototype (even in C99).
                >>
                >int main(void) { int puts(); puts(" --"); }
                >
                This doesn't convert the pointer to the string from char * to const char
                *. I doubt any implementation will even notice, but you need either the
                prototype, or you need to change the call to puts((const char *) " --");
                for it to be valid.
                There are other ways, such as using a variable, and the spaces are
                overkill...

                int main(void){int puts();const char*s=" --";puts(s);}
                int main(void){int puts();puts((co nst char*)" --");}
                --
                Flash Gordon
                If spamming me sent it to smap@spam.cause way.com
                If emailing me use my reply-to address
                See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

                Comment

                • pete

                  #9
                  Re: (part 21) Han from China answers your C questions

                  Richard Heathfield wrote:
                  Ralf Damaschke said:
                  >
                  >s0suk3 wrote:
                  >>
                  >>On Nov 14, 3:28 pm, Andrey Tarasevich <andreytarasevi ch@hotmail.com>
                  >>wrote:
                  >>>Lew Pitcher wrote:
                  >>>>
                  >>>>#include <stdio.h>
                  >>>>int main(void)
                  >>>>{
                  >>>>puts(" --- ")
                  >>>>return 0;
                  >>>>}
                  >>>Overkill
                  >>>>
                  >>>#include <stdio.h>
                  >>>>
                  >>>int main(void) {
                  >>>puts(" --")
                  >>>}
                  >>Overkill. The #include line will bring in a bunch of stuff :-)
                  >>>
                  >>int main(void) { int puts(const char *); puts(" --"); }
                  >Overkill. No need for a prototype (even in C99).
                  >>
                  >int main(void) { int puts(); puts(" --"); }
                  >
                  Overkill. No need for a prototype (even in C99).
                  >
                  int main() { int puts(); puts(" --"; }
                  What do you mean by "(even in C99)" ?

                  There is no implicit function declaration in C99.
                  That's a C89 feature.

                  --
                  pete

                  Comment

                  • Harald van =?UTF-8?b?RMSzaw==?=

                    #10
                    Re: (part 21) Han from China answers your C questions

                    On Sat, 15 Nov 2008 07:35:48 -0500, pete wrote:
                    Richard Heathfield wrote:
                    >Ralf Damaschke said:
                    >>s0suk3 wrote:
                    >>>int main(void) { int puts(const char *); puts(" --"); }
                    >>Overkill. No need for a prototype (even in C99).
                    >>>
                    >>int main(void) { int puts(); puts(" --"); }
                    >>
                    >Overkill. No need for a prototype (even in C99).
                    >>
                    >int main() { int puts(); puts(" --"; }
                    >
                    What do you mean by "(even in C99)" ?
                    >
                    There is no implicit function declaration in C99. That's a C89 feature.
                    Yes, but unprototyped functions are allowed even in C99. Ralf Damaschke
                    removed "const char *" from the declaration of puts. Richard Heathfield
                    removed "void" from the definition of main. Both kept the one called
                    function explicitly declared. There is no implicit function declaration
                    here.

                    Comment

                    • Keith Thompson

                      #11
                      Re: (part 21) Han from China answers your C questions

                      Richard Heathfield <rjh@see.sig.in validwrites:
                      Ralf Damaschke said:
                      >s0suk3 wrote:
                      >>On Nov 14, 3:28 pm, Andrey Tarasevich <andreytarasevi ch@hotmail.com>
                      >>wrote:
                      >>>Lew Pitcher wrote:
                      >>>>
                      >>#include <stdio.h>
                      >>>>
                      >>int main(void)
                      >>{
                      >>puts(" --- ")
                      >>return 0;
                      >>}
                      >>>>
                      >>>Overkill
                      >>>>
                      >>>#include <stdio.h>
                      >>>>
                      >>>int main(void) {
                      >>>puts(" --")
                      >>>}
                      >>>
                      >>Overkill. The #include line will bring in a bunch of stuff :-)
                      >>>
                      >>int main(void) { int puts(const char *); puts(" --"); }
                      >>
                      >Overkill. No need for a prototype (even in C99).
                      >>
                      >int main(void) { int puts(); puts(" --"); }
                      >
                      Overkill. No need for a prototype (even in C99).
                      >
                      int main() { int puts(); puts(" --"; }
                      Actually, that's debatable -- and it's been debated here before.
                      The argument is that declaration "int main()" isn't equivalent to
                      "int main(void)", since the former permits (with undefined behavior)
                      "main(42)" while the latter makes it a constraint violation; see
                      C99 5.1.2.2.1.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      Nokia
                      "We must do something. This is something. Therefore, we must do this."
                      -- Antony Jay and Jonathan Lynn, "Yes Minister"

                      Comment

                      • Richard Heathfield

                        #12
                        Re: (part 21) Han from China answers your C questions

                        Keith Thompson said:
                        Richard Heathfield <rjh@see.sig.in validwrites:
                        >Ralf Damaschke said:
                        >>s0suk3 wrote:
                        <snip>
                        >>>int main(void) { int puts(const char *); puts(" --"); }
                        >>>
                        >>Overkill. No need for a prototype (even in C99).
                        >>>
                        >>int main(void) { int puts(); puts(" --"); }
                        >>
                        >Overkill. No need for a prototype (even in C99).
                        >>
                        >int main() { int puts(); puts(" --"; }
                        >
                        Actually, that's debatable -- and it's been debated here before.
                        The argument is that declaration "int main()" isn't equivalent to
                        "int main(void)", since the former permits (with undefined behavior)
                        "main(42)" while the latter makes it a constraint violation; see
                        C99 5.1.2.2.1.
                        Yes, but the above code doesn't *contain* main(42). If you simply meant
                        "it's better to use int main(void) than int main()", I agree. But that
                        wasn't the cake we were baking on this occasion.

                        --
                        Richard Heathfield <http://www.cpax.org.uk >
                        Email: -http://www. +rjh@
                        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                        "Usenet is a strange place" - dmr 29 July 1999

                        Comment

                        • Harald van =?UTF-8?b?RMSzaw==?=

                          #13
                          Re: (part 21) Han from China answers your C questions

                          On Sat, 15 Nov 2008 17:12:57 +0000, Richard Heathfield wrote:
                          Keith Thompson said:
                          >Richard Heathfield <rjh@see.sig.in validwrites:
                          >>Ralf Damaschke said:
                          >>>s0suk3 wrote:
                          <snip>
                          >>>>int main(void) { int puts(const char *); puts(" --"); }
                          >>>>
                          >>>Overkill. No need for a prototype (even in C99).
                          >>>>
                          >>>int main(void) { int puts(); puts(" --"); }
                          >>>
                          >>Overkill. No need for a prototype (even in C99).
                          >>>
                          >>int main() { int puts(); puts(" --"; }
                          >>
                          >Actually, that's debatable -- and it's been debated here before. The
                          >argument is that declaration "int main()" isn't equivalent to "int
                          >main(void)", since the former permits (with undefined behavior)
                          >"main(42)" while the latter makes it a constraint violation; see C99
                          >5.1.2.2.1.
                          >
                          Yes, but the above code doesn't *contain* main(42).
                          But if main() isn't equivalent to main(void), and main must be defined as
                          int main(void), int main(int argc, char *argv[]), or equivalent, then
                          defining it as main() is invalid, even if you don't call main(42). That's
                          the point I believe Keith Thompson was making.

                          (Personally, I believe int main() is valid.)

                          Comment

                          • Richard Heathfield

                            #14
                            Re: (part 21) Han from China answers your C questions

                            Harald van D?k said:

                            <snip>
                            But if main() isn't equivalent to main(void), and main must be defined as
                            int main(void), int main(int argc, char *argv[]), or equivalent, then
                            defining it as main() is invalid, even if you don't call main(42). That's
                            the point I believe Keith Thompson was making.
                            Oh, I see.
                            (Personally, I believe int main() is valid.)
                            I agree. Note that int main() - in a function *definition* - defines main
                            as taking no parameters. The relevant quote is in 3.5.4.3: "An empty list
                            in a function declarator that is part of a function definition specifies
                            that the function has no parameters. The empty list in a function
                            declarator that is not part of a function definition specifies that no
                            information about the number or types of the parameters is supplied."

                            Almost identical wording appears in 6.7.5.3(14) of C99. Incidentally, the
                            wording is slightly tighter in C99 - which is actually quite interesting,
                            because it reveals a slight flaw in the wording of the above, but the
                            differences aren't relevant to the current case.

                            --
                            Richard Heathfield <http://www.cpax.org.uk >
                            Email: -http://www. +rjh@
                            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                            "Usenet is a strange place" - dmr 29 July 1999

                            Comment

                            • Keith Thompson

                              #15
                              Re: (part 21) Han from China answers your C questions

                              Richard Heathfield <rjh@see.sig.in validwrites:
                              Harald van D?k said:
                              >
                              <snip>
                              >
                              >But if main() isn't equivalent to main(void), and main must be defined as
                              >int main(void), int main(int argc, char *argv[]), or equivalent, then
                              >defining it as main() is invalid, even if you don't call main(42). That's
                              >the point I believe Keith Thompson was making.
                              >
                              Oh, I see.
                              >
                              >(Personally, I believe int main() is valid.)
                              >
                              I agree. Note that int main() - in a function *definition* - defines main
                              as taking no parameters. The relevant quote is in 3.5.4.3: "An empty list
                              in a function declarator that is part of a function definition specifies
                              that the function has no parameters. The empty list in a function
                              declarator that is not part of a function definition specifies that no
                              information about the number or types of the parameters is supplied."
                              >
                              Almost identical wording appears in 6.7.5.3(14) of C99. Incidentally, the
                              wording is slightly tighter in C99 - which is actually quite interesting,
                              because it reveals a slight flaw in the wording of the above, but the
                              differences aren't relevant to the current case.
                              Yes, either "int main(void) { ... }" or "int main() { ... }" defines
                              main as taking no parameters; they're similar in that way. But one
                              similarity doesn't make them equivalent.

                              Here's what the standard says (C99 5.1.2.2.1):

                              The function called at program startup is named main. The
                              implementation declares no prototype for this function. It shall
                              be defined with a return type of int and with no parameters:

                              int main(void) { /* ... */ }

                              or with two parameters (referred to here as argc and argv, though
                              any names may be used, as they are local to the function in which
                              they are declared):

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

                              or equivalent; or in some other implementation-defined manner.

                              The following two declarations:

                              int x;
                              double x;

                              are similar in that they both declare objects named "x". They're not
                              equivalent, though, because they declare objects of different types.
                              Even putting them in a program where the difference has no visible
                              effect doesn't make them equivalent; the programs might be effectively
                              equivalent, but the declarations aren't. (My argument here is just a
                              reasonable use of the word "equivalent ".)

                              The following two function definitions:

                              int main() { return 0; }
                              int main(void) { return 0; }

                              are similar in that they both declare and define a function called
                              "main". They differ in that, for example, one makes the call
                              "main(42)", appearing in another function in the same translation unit
                              following the above definition, invoke undefined behavior, while the
                              other makes it a constraint violation.

                              The standard doesn't say that the definition of main must be very
                              similar to "int main(void) { /* ... */ }", differing only in ways that
                              have no effect on the particular program being considered. It says
                              that it must be *equivalent* (or it must follow one of the other two
                              options).

                              In practice, most or all C compilers do support "int main()" with the
                              obvious semantics. If my interpretation is correct (and no, I'm not
                              100% certain of that), then "int main()", in an implementation that
                              doesn't document it, invokes undefined behavior; accept it is
                              perfectly legal.

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              Nokia
                              "We must do something. This is something. Therefore, we must do this."
                              -- Antony Jay and Jonathan Lynn, "Yes Minister"

                              Comment

                              Working...