Channels ▼

Matthew Wilson

Dr. Dobb's Bloggers

Forgotten Headers and Strange Warnings

October 31, 2010

In an example program I wrote for a recent instalment of Quality Matters, I had one of those oh-oh moments where you find yourself suddenly abandoned by the wisdom and experience of decades of programming. As usual the explanation was blindingly obvious after the fact, but it was harder than you might expect to get there, in part because GCC is ever so helpful, and C is often not helpful enough.Here's the program:

/* fprintf.fail.test.c */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
 if(2 != argc)
 {
  fprintf(stderr, "supply file name\n");
  return EXIT_FAILURE;
 }
 else
 {
  FILE* f = fopen(argv[1], "a+");
  if(NULL == f)
  {
   <strong>fprintf(stderr, "could not open %s: %s\n", argv[1], strerror(errno));</strong>
   return EXIT_FAILURE;
  }
  else
  {
   int n = fprintf(f, "hello, world\n");
   if(13 != n)
   {
    if(n < 0)
    {
     <strong>fprintf(stderr, "fprintf() failed: %s\n", strerror(errno));</strong>
    }
    else
    {
     <strong>fprintf(stderr, "wrote only %d/13 bytes: %s\n", n, strerror(errno));</strong>
    }
    return EXIT_FAILURE;
   }
  }
  fclose(f);
 }
 return EXIT_SUCCESS;
}

And here're GCC's compilation warnings:

fprintf.fail.test.c: In function 'main':
fprintf.fail.test.c:17: warning: format '%s' expects type 'char *', but argument 4 has type 'int'
fprintf.fail.test.c:27: warning: format '%s' expects type 'char *', but argument 3 has type 'int'
fprintf.fail.test.c:31: warning: format '%s' expects type 'char *', but argument 4 has type 'int'

Have you picked it, or is it as confusing as I found it the first time I looked at it?

Clearly it thinks that strerror() is of type int.

The answer is that we're missing the string.h inclusion, and so the declaration of strerror() is not seen. This being C, any such missing declaration is assumed to be of type int. This being GCC - as opposed to some other compilers whose libraries less rigorously separate symbols into appearing only where they should appear - you're not going to see the declaration of strerror() unless you include string.h; that's a good thing for many reasons. Perhaps it's because I don't spend the majority of my programming time in C or using GCC, that I was so easily tripped up.

Conclusion:

  • Use different compilers, regularly and frequently, to keep your code clean and fresh
  • Use different languages, regularly and frequently, to keep your brain clean and fresh
I recently had one of those oh-oh moments where you find yourself suddenly abandoned by the wisdom and experience of decades of programming. As usual the explanation was blindingly obvious after the fact, but it was harder than you might expect to get there, in part because GCC is ever so helpful, and C is often not helpful enough.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video