Doubt

Discussion in 'C Programming' started by Anarki, Jul 31, 2008.

  1. Anarki

    Anarki Guest

    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?
    Anarki, Jul 31, 2008
    #1
    1. Advertising

  2. Anarki

    Ian Collins Guest

    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.
    Ian Collins, Jul 31, 2008
    #2
    1. Advertising

  3. Anarki

    Anarki Guest

    On Jul 31, 3:44 pm, Ian Collins <> wrote:
    > 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
    Anarki, Jul 31, 2008
    #3
  4. Anarki

    santosh Guest

    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.
    santosh, Jul 31, 2008
    #4
  5. Anarki

    santosh Guest

    Anarki wrote:

    > On Jul 31, 3:44 pm, Ian Collins <> wrote:
    >> 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 2> errors.txt

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

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

    For more help ask in a Unix group like comp.unix.programmer or on the
    mailing lists for Cygwin.
    santosh, Jul 31, 2008
    #5
  6. Anarki

    santosh Guest

    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.
    santosh, Jul 31, 2008
    #6
    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. Igbal

    Doubt to go .Net technology

    Igbal, Nov 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    488
    Ed Kaim [MSFT]
    Nov 17, 2003
  2. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    596
  3. Replies:
    0
    Views:
    539
  4. Peter Otten
    Replies:
    2
    Views:
    103
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    107
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page