Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > is this declaration correct

Reply
Thread Tools

is this declaration correct

 
 
barry
Guest
Posts: n/a
 
      06-17-2010
Rather than
include <stdio.h> can I use int printf(const char *,...) correct at all
places if I only use printf in my program..
"You are not going to get it right first time" -> Bjarne Stroustrup
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-17-2010
barry <(E-Mail Removed)> writes:
> Rather than
> include <stdio.h> can I use int printf(const char *,...) correct at all
> places if I only use printf in my program..


Yes and no.

Yes, in C90 that's the correct declaration.

In C99, the declaration of printf is
int printf(const char * restrict format, ...);
I'm not sure what the standard says about defining a function with
"restrict" and declaring it without "restrict".

But why would you want to do this? I can't think of any good reason not
to just use

#include <stdio.h>

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      06-18-2010
Keith Thompson <(E-Mail Removed)> wrote:
> barry <(E-Mail Removed)> writes:
> > include <stdio.h> can I use int printf(const char *,...)

....
> In C99, the declaration of printf is
> * * int printf(const char * restrict format, ...);
> I'm not sure what the standard says about defining a function with
> "restrict" and declaring it without "restrict".


N1256: 6.7.5.3p15: "... (In the determination of type
compatibility ... each parameter declared with qualified type
is taken as having the unqualified version of its declared
type.)"

So, yes, it's fine in C99.

> But why would you want to do this?
>
>*I can't think of any good reason not to just use
>
> * * #include <stdio.h>


Perhaps:

1) to shorten compile time.
2) declaring printf at block scope localises the
declaration.
3) a freestanding implementation may not supply a stdio.h,
it may be possible to link one in.

--
Peter
 
Reply With Quote
 
sandeep
Guest
Posts: n/a
 
      06-18-2010
Keith Thompson writes:
> barry <(E-Mail Removed)> writes:
>> Rather than
>> include <stdio.h> can I use int printf(const char *,...) correct at all
>> places if I only use printf in my program..

>
> Yes and no.
>
> Yes, in C90 that's the correct declaration.
>
> In C99, the declaration of printf is
> int printf(const char * restrict format, ...);
> I'm not sure what the standard says about defining a function with
> "restrict" and declaring it without "restrict".


Also printf may be a macro not a function, for example if I was
implementing a compiler then I would implement fprintf then have
#define printf(s,...) (fprintf(stdout,(s),__VA_ARGS__))
in <stdio.h>.

In this situation linking will fail if you provide your own declaration
of printf!!
 
Reply With Quote
 
Ralf Damaschke
Guest
Posts: n/a
 
      06-18-2010
sandeep wrote:

> Keith Thompson writes:
>> In C99, the declaration of printf is
>> int printf(const char * restrict format, ...);
>> I'm not sure what the standard says about defining a function with
>> "restrict" and declaring it without "restrict".

>
> Also printf may be a macro not a function, for example if I was
> implementing a compiler then I would implement fprintf then have
> #define printf(s,...) (fprintf(stdout,(s),__VA_ARGS__))
> in <stdio.h>.
>
> In this situation linking will fail if you provide your own declaration
> of printf!!


In that case your implementation would not be a conforming one.
It even must allow a
#undef printf
and still accept calls to printf().

-- Ralf
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-18-2010
On 6/17/2010 10:51 PM, Peter Nilsson wrote:
> Keith Thompson<(E-Mail Removed)> wrote:
>> barry<(E-Mail Removed)> writes:
>>> include<stdio.h> can I use int printf(const char *,...)

> ...
>> In C99, the declaration of printf is
>> int printf(const char * restrict format, ...);
>> I'm not sure what the standard says about defining a function with
>> "restrict" and declaring it without "restrict".

>
> N1256: 6.7.5.3p15: "... (In the determination of type
> compatibility ... each parameter declared with qualified type
> is taken as having the unqualified version of its declared
> type.)"
>
> So, yes, it's fine in C99.
>
>> But why would you want to do this?
>>
>> I can't think of any good reason not to just use
>>
>> #include<stdio.h>

>
> Perhaps:
>
> 1) to shorten compile time.


IMHO it would take a very large number of compilations to
recoup the time already spent posting the question and reading
answers.

> 2) declaring printf at block scope localises the
> declaration.


Yes, but why? This would only be advantageous if you also
wanted to use `printf' as an identifier for something else entirely,
like `static double printf[42][36];' or something -- and that's just
plain stupid.

> 3) a freestanding implementation may not supply a stdio.h,
> it may be possible to link one in.


True -- But in that case, we don't know whether the offered
declaration is correct or not, since we don't know what the
free-standing system's `printf' does.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-18-2010
Peter Nilsson <(E-Mail Removed)> writes:
> Keith Thompson <(E-Mail Removed)> wrote:

[...]
>>*I can't think of any good reason not to just use
>>
>> * * #include <stdio.h>

>
> Perhaps:
>
> 1) to shorten compile time.


If you're compiling a very large number of files on a slow system, the
speedup might even be noticeable -- but I doubt that it would exceed the
extra time spent maintaining the code.

> 2) declaring printf at block scope localises the
> declaration.


How is that useful?

> 3) a freestanding implementation may not supply a stdio.h,
> it may be possible to link one in.


Ok, that's possible (though an implementation that provides printf
but doesn't declare it in a header is not exactly friendly).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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"
 
Reply With Quote
 
pettybetty66@yahoo.com
Guest
Posts: n/a
 
      06-18-2010
On Jun 18, 11:25*am, sandeep <(E-Mail Removed)> wrote:
> Also printf may be a macro not a function, for example if I was
> implementing a compiler then I would implement fprintf then have
> #define printf(s,...) (fprintf(stdout,(s),__VA_ARGS__))
> in <stdio.h>.


That's not a valid macro implementation of printf. Consider
printf("Hello world\n");
 
Reply With Quote
 
sandeep
Guest
Posts: n/a
 
      06-18-2010
Ralf Damaschke writes:
> sandeep wrote:
>
>> Keith Thompson writes:
>>> In C99, the declaration of printf is
>>> int printf(const char * restrict format, ...);
>>> I'm not sure what the standard says about defining a function with
>>> "restrict" and declaring it without "restrict".

>>
>> Also printf may be a macro not a function, for example if I was
>> implementing a compiler then I would implement fprintf then have
>> #define printf(s,...) (fprintf(stdout,(s),__VA_ARGS__)) in <stdio.h>.
>>
>> In this situation linking will fail if you provide your own declaration
>> of printf!!

>
> In that case your implementation would not be a conforming one. It even
> must allow a
> #undef printf
> and still accept calls to printf().
>
> -- Ralf


I think standard library functions can be implemented as macros, for
example in gcc putc is a macro.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-18-2010
On 6/18/2010 5:23 PM, sandeep wrote:
>
> I think standard library functions can be implemented as macros, for
> example in gcc putc is a macro.


Yes, but no Standard library function can be implemented
*only* as a macro.

Are you the same Sandeep who's writing his Master's thesis on
the C Standard library? I guess not, because someone undertaking
such an effort would surely have read the Standard's description
of that library, and would have come across 7.1.4p1. Read it, and
you'll then know something the other Sandeep has already learned.

--
Eric Sosman
(E-Mail Removed)lid
 
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
Can a static function declaration conflict with a non-static declaration? nospam_timur@tabi.org C Programming 4 12-12-2006 10:26 PM
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM
"virtual outside class declaration" and "declaration does not declare anything" kelvSYC C++ 6 05-17-2005 08:58 AM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM
Intel C++ 8.0 : declaration hides declaration Alex Vinokur C++ 4 04-05-2004 09:49 PM



Advertisments