Doubt

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

    Doubt

    int main(a,b,c,d,e, f,g)
    {
    printf("Size of a = %d\nSize of b =%d",sizeof(a), sizeof(b));
    return 0;
    }

    The above program perfectly compiles in cygwin using gcc

    and my questions are

    1.How does the compiler know what are the types of variables a, b, c,
    d, e, f and g?? Where is its declaration?
    2.I haven't included stdio.h yet the printf compiles.The compiler
    doesn't complain about the requirement of function prototype, why?
  • Ian Collins

    #2
    Re: Doubt

    Anarki wrote:
    int main(a,b,c,d,e, f,g)
    {
    printf("Size of a = %d\nSize of b =%d",sizeof(a), sizeof(b));
    return 0;
    }
    >
    The above program perfectly compiles in cygwin using gcc
    >
    and my questions are
    >
    1.How does the compiler know what are the types of variables a, b, c,
    d, e, f and g?? Where is its declaration?
    It's assuming they are int.
    2.I haven't included stdio.h yet the printf compiles.The compiler
    doesn't complain about the requirement of function prototype, why?
    It does.

    gcc /tmp/x.c
    /tmp/x.c: In function 'main':
    /tmp/x.c:3: warning: incompatible implicit declaration of built-in
    function 'printf'

    --
    Ian Collins.

    Comment

    • Richard Heathfield

      #3
      Re: Doubt

      Anarki said:
      int main(a,b,c,d,e, f,g)
      {
      printf("Size of a = %d\nSize of b =%d",sizeof(a), sizeof(b));
      return 0;
      }
      >
      The above program perfectly compiles in cygwin using gcc
      >
      and my questions are
      >
      1.How does the compiler know what are the types of variables a, b, c,
      d, e, f and g?? Where is its declaration?
      In C90 (which is what you're using), the code is perfectly legal (albeit in
      an obsolete style), and the compiler knows that a, b, etc are ints because
      that's the default type where a type is not specified within a
      declaration.

      In C99, which you are *not* using but which might become popular any
      century now, the code is illegal, and a diagnostic message is required.
      2.I haven't included stdio.h yet the printf compiles.The compiler
      doesn't complain about the requirement of function prototype, why?
      In C90, no diagnostic message is required when a function is called without
      a prototype being in scope (or at least, one might be, but it will be for
      a reason other than the mere absence of the prototype) - the compiler will
      assume that the args are fine and that the function returns int. (So if,
      for example, you don't bother to include <stdlib.hwhen calling malloc,
      the compiler will assume it returns int, but you're assigning that value
      to a pointer object, and that's why you get a diagnostic message along the
      lines of "cannot convert integer to pointer without a cast" - the right
      solution is *not* to add a cast, but to add a prototype!)

      If the function really does return int and if you get the parameters right,
      you can get away with this most of the time. In the case of variadic
      functions like printf, however, the behaviour is undefined unless you
      provide a prototype.

      In C99, you... well, you don't necessarily need a function prototype before
      calling a function (although it really really really is a good idea), but
      you do at least need a function declaration before the call. Otherwise, a
      diagnostic message is required.

      --
      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

      • Anarki

        #4
        Re: Doubt

        On Jul 31, 3:44 pm, Ian Collins <ian-n...@hotmail.co mwrote:
        Anarki wrote:
        int main(a,b,c,d,e, f,g)
        {
           printf("Size of a = %d\nSize of b =%d",sizeof(a), sizeof(b));
           return 0;
        }
        >
        The above program perfectly compiles in cygwin using gcc
        >
        and my questions are
        >
        1.How does the compiler know what are the types of variables a, b, c,
        d, e, f and g?? Where is its declaration?
        >
        It's assuming they are int.
        >
        2.I haven't included stdio.h yet the printf compiles.The compiler
        doesn't complain about the requirement of function prototype, why?
        >
        It does.
        >
        gcc /tmp/x.c
        /tmp/x.c: In function 'main':
        /tmp/x.c:3: warning: incompatible implicit declaration of built-in
        function 'printf'
        >
        --
        Ian Collins.
        y no warning for me! ok if at all it complains how does this program
        link with the printf? Does it look in default include files/libs ??

        By the way I use Cygwin(windows Xp) is there a way to redirect the
        warning/errors to a text file. If so please tell me how

        Comment

        • santosh

          #5
          Re: Doubt

          Anarki wrote:
          int main(a,b,c,d,e, f,g)
          You are using the so-called "old style" function definition without a
          declaration list specifying the types of the parameters. In such a case
          they default to type int.

          Further note that unless your implementation specifically defines this
          form of main, (i.e., int main(int, int, int, int, int, int, int)), you
          are invoking undefined behaviour.
          {
          printf("Size of a = %d\nSize of b =%d",sizeof(a), sizeof(b));
          The sizeof operator yields a value of type size_t. The 'd' type
          specifier is for int values. Use 'z' if available, or 'lu' and cast to
          unsigned long.
          return 0;
          }
          >
          The above program perfectly compiles in cygwin using gcc
          >
          and my questions are
          >
          1.How does the compiler know what are the types of variables a, b, c,
          d, e, f and g?? Where is its declaration?
          As above.
          2.I haven't included stdio.h yet the printf compiles.The compiler
          doesn't complain about the requirement of function prototype, why?
          If you had invoked your compiler in conforming mode then it would have.
          For gcc use the -ansi/-std=c99 and -pedantic flags for C90 or C99
          conformance. C99 conformance is incomplete.

          Comment

          • santosh

            #6
            Re: Doubt

            Anarki wrote:
            On Jul 31, 3:44 pm, Ian Collins <ian-n...@hotmail.co mwrote:
            >Anarki wrote:
            int main(a,b,c,d,e, f,g)
            {
            printf("Size of a = %d\nSize of b =%d",sizeof(a), sizeof(b));
            return 0;
            }
            >>
            The above program perfectly compiles in cygwin using gcc
            >>
            and my questions are
            >>
            1.How does the compiler know what are the types of variables a, b,
            c, d, e, f and g?? Where is its declaration?
            >>
            >It's assuming they are int.
            >>
            2.I haven't included stdio.h yet the printf compiles.The compiler
            doesn't complain about the requirement of function prototype, why?
            >>
            >It does.
            >>
            >gcc /tmp/x.c
            >/tmp/x.c: In function 'main':
            >/tmp/x.c:3: warning: incompatible implicit declaration of built-in
            >function 'printf'
            >>
            >--
            >Ian Collins.
            Please snip signatures unless you are commenting on them.
            y no warning for me! ok if at all it complains how does this program
            link with the printf? Does it look in default include files/libs ??
            A compiler is allowed to do whatever it wants after emitting the
            required diagnostics, including making guesses about what you meant and
            trying to generate the best possible code.
            By the way I use Cygwin(windows Xp) is there a way to redirect the
            warning/errors to a text file. If so please tell me how
            Use:

            gcc -whatever_flags_ you_want file_list_etc 2errors.txt

            Now all diagnostic messages will be redirected to a file named
            errors.txt.

            Using 2>instead of 2will append the output of the command to the
            existing contents of errors.txt.

            For more help ask in a Unix group like comp.unix.progr ammer or on the
            mailing lists for Cygwin.

            Comment

            • Richard Heathfield

              #7
              Re: Doubt

              santosh said:
              Anarki wrote:
              >
              <snip>
              >
              >2.I haven't included stdio.h yet the printf compiles.The compiler
              >doesn't complain about the requirement of function prototype, why?
              >
              If you had invoked your compiler in conforming mode then it would have.
              Chapter and verse for C90, please. (I accept that your claim is correct for
              C99.)

              <snip>

              --
              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

              • santosh

                #8
                Re: Doubt

                Richard Heathfield wrote:
                santosh said:
                >
                >Anarki wrote:
                >>
                <snip>
                >>
                >>2.I haven't included stdio.h yet the printf compiles.The compiler
                >>doesn't complain about the requirement of function prototype, why?
                >>
                >If you had invoked your compiler in conforming mode then it would
                >have.
                >
                Chapter and verse for C90, please. (I accept that your claim is
                correct for C99.)
                >
                <snip>
                Um, does 0.0 p0 qualify? :-)

                You're right. The rules of C90 do not require a diagnostic. This is what
                comes of relying more on gcc than on the Standard document itself.

                Comment

                Working...