C FAQs section 20.27

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

    C FAQs section 20.27

    This is the question that I need some explanation to understand it:



    Some features of C which keep it from being a strict subset of C++ (that
    is, which keep C programs from necessarily being acceptable to C++
    compilers) are that main may be called recursively, character constants
    are of type int, prototypes are not required, and void * implicitly
    converts to other pointer types.

    main may be called recursively
    anyone has an example ?


    character constants are of type int
    I learned C++ 1st. This fact was strange for me.

    prototypes are not required

    you mean decelerations (e.g of variables and arrays) ? They are also not
    required in C++.

    void * implicitly converts to other pointer types.
    This was the question already ran in a separate thread started by me and I
    got nice answers from the regular posters :)



    --

    my email ID is at the above address

  • Kenneth Brody

    #2
    Re: C FAQs section 20.27

    arnuld wrote:
    >
    This is the question that I need some explanation to understand it:
    >
    Some features of C which keep it from being a strict subset of C++ (that
    is, which keep C programs from necessarily being acceptable to C++
    compilers) are that main may be called recursively, character constants
    are of type int, prototypes are not required, and void * implicitly
    converts to other pointer types.
    >
    main may be called recursively
    >
    anyone has an example ?
    #include <stdio.h>

    int main(int argc,char **argv)
    {
    printf("%d '%s'\n",argc,(* argv)?(*argv):" <null>");
    if ( argc 0 )
    return main(argc-1,argv+1);
    return 0;
    }

    However, my C++ compiler also compiles the above, without any warnings,
    even on highest warning level.
    character constants are of type int
    >
    I learned C++ 1st. This fact was strange for me.
    Okay.
    prototypes are not required
    >
    you mean decelerations (e.g of variables and arrays) ? They are also not
    required in C++.
    No, it says "prototypes ".

    This is valid C:

    int foo()
    {
    return bar(7,8);
    }
    int bar(int a,int b)
    {
    return a+b;
    }

    My C++ compiler gives an error on the call to bar().
    void * implicitly converts to other pointer types.
    >
    This was the question already ran in a separate thread started by me and I
    got nice answers from the regular posters :)

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

    Comment

    • Keith Thompson

      #3
      Re: C FAQs section 20.27

      arnuld <NoSpam@NoPain. Romewrites:
      This is the question that I need some explanation to understand it:
      >
      Some features of C which keep it from being a strict subset of C++ (that
      is, which keep C programs from necessarily being acceptable to C++
      compilers) are that main may be called recursively, character constants
      are of type int, prototypes are not required, and void * implicitly
      converts to other pointer types.
      >
      >
      >main may be called recursively
      >
      anyone has an example ?
      Already posted in this thread. Note that calling main recursively is
      rarely, if ever, a good idea, and the prohibition in C++ is not a real
      burden; you can always write another recursive function and call it
      from main.

      [...]
      >prototypes are not required
      >
      >
      you mean decelerations (e.g of variables and arrays) ? They are also not
      required in C++.
      No, a prototype is simply a function declaration that declares the
      types of its parameters. For example, this:

      int foo(int x, int y);

      is a prototype, and this:

      int foo (int x, int y)
      {
      /* code that uses x and y */
      }

      is a function definition that includes a prototype. This:

      int foo(x, y)
      int x;
      int y;
      {
      /* code that uses x and y */
      }

      is an old-style non-prototype function definition; it's still allowed
      in C (even C99), but not in C++. (It's considered poor style even in
      C, since it doesn't allow type checking on calls.)

      [...]

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

      Comment

      • arnuld

        #4
        Re: C FAQs section 20.27

        On Mon, 21 Apr 2008 10:27:55 -0400, Kenneth Brody wrote:
        #include <stdio.h>
        >
        int main(int argc,char **argv)
        {
        printf("%d '%s'\n",argc,(* argv)?(*argv):" <null>");
        if ( argc 0 )
        return main(argc-1,argv+1);
        return 0;
        }
        However, my C++ compiler also compiles the above, without any warnings,
        even on highest warning level.

        mine does:

        [arnuld@raj C]$ g++ -ansi -pedantic -Wall -Wextra test.c
        test.c: In function `int main(int, char**)':
        test.c:18: error: ISO C++ forbids taking address of function `::main'
        [arnuld@raj C]$



        int foo()
        {
        return bar(7,8);
        }
        int bar(int a,int b)
        {
        return a+b;
        }
        >
        My C++ compiler gives an error on the call to bar().

        mine does not



        --

        my email ID is at the above address

        Comment

        • arnuld

          #5
          Re: C FAQs section 20.27

          On Mon, 21 Apr 2008 09:05:01 -0700, Keith Thompson wrote:

          No, a prototype is simply a function declaration that declares the
          types of its parameters. For example, this:
          >
          int foo(int x, int y);
          >
          is a prototype, and this:
          >
          int foo (int x, int y)
          {
          /* code that uses x and y */
          }
          >
          is a function definition that includes a prototype.

          In my other thread titled "K&R2, exercise 7.6". There is one function
          named "compare_fi les" if I remove the prototype then program does not
          compile, which directly means C requires a prototype but the example given
          by "Kenneth Brody" in the other post compiles fine without any prototype.
          here is the code for both programs:

          dpaste.com is a pastebin site for easily sharing and storing code snippets. Syntax highlighting, clean interface, markup preview, quick sharing options.

          dpaste.com is a pastebin site for easily sharing and storing code snippets. Syntax highlighting, clean interface, markup preview, quick sharing options.


          int foo(x, y)
          int x;
          int y;
          {
          /* code that uses x and y */
          }
          >
          is an old-style non-prototype function definition; it's still allowed
          in C (even C99), but not in C++. (It's considered poor style even in
          C, since it doesn't allow type checking on calls.)
          yes, I saw it for 1st time in K&R2 :)


          --

          my email ID is at the above address

          Comment

          • Keith Thompson

            #6
            Re: C FAQs section 20.27

            arnuld <NoSpam@NoPain. Romewrites:
            >On Mon, 21 Apr 2008 09:05:01 -0700, Keith Thompson wrote:
            >No, a prototype is simply a function declaration that declares the
            >types of its parameters. For example, this:
            >>
            > int foo(int x, int y);
            >>
            >is a prototype, and this:
            >>
            > int foo (int x, int y)
            > {
            > /* code that uses x and y */
            > }
            >>
            >is a function definition that includes a prototype.
            >
            In my other thread titled "K&R2, exercise 7.6". There is one function
            named "compare_fi les" if I remove the prototype then program does not
            compile, which directly means C requires a prototype but the example given
            by "Kenneth Brody" in the other post compiles fine without any prototype.
            here is the code for both programs:
            >
            dpaste.com is a pastebin site for easily sharing and storing code snippets. Syntax highlighting, clean interface, markup preview, quick sharing options.

            http://dpaste.com/46210/
            No, the fact that removing a prototype causes the program to fail to
            compile doesn't imply that C requires a prototype. In fact, when I
            remove the prototype of "compare_fi les" in your first example, it
            compiles with just a warning.

            [...]

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

            Comment

            • arnuld

              #7
              Re: C FAQs section 20.27

              On Tue, 22 Apr 2008 01:15:18 -0700, Keith Thompson wrote:
              No, the fact that removing a prototype causes the program to fail to
              compile doesn't imply that C requires a prototype. In fact, when I
              remove the prototype of "compare_fi les" in your first example, it
              compiles with just a warning.

              I thought by using "-ansi" flag with GCC I am using ANSI C


              --

              my email ID is at the above address

              Comment

              • Richard Tobin

                #8
                Re: C FAQs section 20.27

                In article <pan.2008.04.23 .06.54.16.51256 3@gmail.com>,
                arnuld <GoogleGroupsis Full0fSpam@gmai l.comwrote:
                >I thought by using "-ansi" flag with GCC I am using ANSI C
                Ansi C doesn't require prototypes, though it did introduce them.

                -- Richard
                --
                :wq

                Comment

                • santosh

                  #9
                  Re: C FAQs section 20.27

                  arnuld wrote:
                  >On Mon, 21 Apr 2008 09:05:01 -0700, Keith Thompson wrote:
                  >
                  >
                  >No, a prototype is simply a function declaration that declares the
                  >types of its parameters. For example, this:
                  >>
                  > int foo(int x, int y);
                  >>
                  >is a prototype, and this:
                  >>
                  > int foo (int x, int y)
                  > {
                  > /* code that uses x and y */
                  > }
                  >>
                  >is a function definition that includes a prototype.
                  >
                  >
                  In my other thread titled "K&R2, exercise 7.6". There is one function
                  named "compare_fi les" if I remove the prototype then program does not
                  compile, which directly means C requires a prototype [...]
                  No. C does not require a prototype and the code should compile under all
                  versions of Standard C, though diagnostics (again not absolutely
                  necessary) may be produced by your compiler. However implicit int
                  return type has been outlawed by the C99 standard.

                  <snip>

                  Comment

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

                    #10
                    Re: C FAQs section 20.27

                    On Tue, 22 Apr 2008 21:41:45 +0500, arnuld wrote:
                    In my other thread titled "K&R2, exercise 7.6". There is one function
                    named "compare_fi les" if I remove the prototype then program does not
                    compile, [...]
                    >
                    http://dpaste.com/46209/
                    Others have already replied stating that C does not require prototypes,
                    but I'd like to ask how you "removed the prototype". If you tried to
                    compile

                    int main( int argc, char* argv[] )
                    {
                    FILE *pf1, *pf2;
                    /* ... */
                    compare_files( pf1,pf2 );
                    /* ... */
                    }

                    void compare_files( FILE* pf1, FILE* pf2 )
                    {
                    /* ... */
                    }

                    you may get an error, as this implicitly declares compare_files as
                    returning int (since no declaration of compare_files is visible yet when
                    it is called), and then defines it as returning void. You don't need a
                    prototype, but you do need a declaration:

                    void compare_files() ;

                    int main(argc, argv)
                    int argc;
                    char *argv[];
                    {
                    FILE *pf1, *pf2;
                    /* ... */
                    compare_files( pf1,pf2 );
                    /* ... */
                    }

                    void compare_files(p f1, pf2)
                    FILE *pf1;
                    FILE *pf2;
                    {
                    /* ... */
                    }

                    Comment

                    Working...