[prog] Another elementary 'C' question

Kelly Martin kmartin at pyrzqxgl.org
Tue Dec 14 09:05:26 EST 2004


David Sumbler wrote:

> Could somebody explain the significance of 'const' in C function
> prototypes to me, please?  I'm slightly confused.
>
> fprintf, for instance, is shown as
>
> int fprintf(FILE *stream, const char *format, ...)
>
> Now, I imagine that the 'const' there means that fprintf won't change
> the contents of the string *format.
>
> But presumably it doesn't change the value pointed to by 'stream'
> either, otherwise we'd get in a right old mess with subsequent actions
> on the relevant file.  But FILE isn't shown as 'const'.
>
> So have I misunderstood something about 'const' in this sort of
> context?
>
> Or is it that the type 'FILE' itself is defined as a constant?
>
> Or what?


"const" in the context of a prototype argument list means that "this 
function will not alter the data this parameter points to".  The format 
specification is const because it will be used read-only; the file 
pointer is not const because it will be used read-write.  The contents 
of the FILE structure WILL be changed by the fprintf operation.

Kelly



More information about the Programming mailing list