Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Doubt

Reply
Thread Tools

Doubt

 
 
Anarki
Guest
Posts: n/a
 
      07-31-2008
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?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-31-2008
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.
 
Reply With Quote
 
 
 
 
Anarki
Guest
Posts: n/a
 
      07-31-2008
On Jul 31, 3:44*pm, Ian Collins <(E-Mail Removed)> 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
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      07-31-2008
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.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      07-31-2008
Anarki wrote:

> On Jul 31, 3:44*pm, Ian Collins <(E-Mail Removed)> 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.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      07-31-2008
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
doubt Ajith Nair ASP .Net 6 03-29-2005 01:25 PM
doubt about namespace google manasa sreenivas via .NET 247 ASP .Net 1 05-15-2004 03:05 PM
Doubt to go .Net technology Igbal ASP .Net 1 11-17-2003 06:56 AM



Advertisments