Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC: why a -Wformat warning for this fprintf?

We cleaned up a large number of warnings. In several cases, we replaced %s with %d. Everything seemed OK until we realized that the generated code wouldn't compile - strings had been replaced by numbers.

NOTE: I had the wrong revision checked out when I did the copy & paste. The fprintf string has been edited!

GCC output

fedex_plus/classes.c:2425:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]

lines 2424 and 2425:

fprintf(file, "\n//\t%s_ptr create_TIE();\n\tIDL_Application_instance_ptr create_TIE();\n", 
    ENTITYget_CORBAname(entity));

function ENTITYget_CORBAname:

const char *
ENTITYget_CORBAname (Entity ent)
{
    static char newname [BUFSIZ];
    strcpy( newname, ENTITYget_name(ent) );
    newname[0] = ToUpper (newname [0]);
    return newname;
}
like image 604
Mark Avatar asked Jan 26 '26 06:01

Mark


1 Answers

My bet is that no declaration of ENTITYget_CORBAname is available at the fprintf spot. Should this be the case, it defaults to the implicit signature:

int ENTITYget_CORBAname(...);

hence the warning about the result type being an int.

Check your code with -Wimplicit (implied by -Wall), or try to put

const char *ENTITYget_CORBAname (Entity ent);

before the fprintf line and check if the warning disappears. If it does, then you're likely missing a header.

like image 154
Alexandre C. Avatar answered Jan 27 '26 23:01

Alexandre C.