17598

__printflike__ modifier

Question:

what is "__printflike__ modifier" exactly? what does this term mean?

Answer1:

At a guess it tells the compiler you're using that a function takes arguments in the form [anything, ] format, ... where the format, ... part look like the arguments to printf. The __printflike__ attribute lets the compiler test the types in the argument list against the string format. This comes up when you write a function like log(format, ...) and use vsprintf to subordinate the formatting work to the usual standard library functions before sending the string to some special log interface.

If you are using GCC then it is probably a #define in your project something like:

#define __printflike__ __attribute__((format(printf, 1, 2)))

Where 1, 2 means that format, ... appear in positions 1 and 2.

Answer2:

I have a function in my error reporting library with the declaration in the header like:

extern void err_logmsg(FILE *fp, int flags, int estat, const char *format, ...) PRINTFLIKE(4,5);

The PRINTFLIKE is in upper-case so that I can define it as nothing when I'm not using GCC. This use says that the first three arguments are nothing special, but the fourth argument is a format string like the ones used by printf() (indeed, internally, it gets passed to vfprintf()), and the arguments corresponding to it (formatted using the format string) start with the fifth argument.

This means that if I type:

err_logmsg(stdout, ERR_ABORT, 1, "%s: %d\n", errno, strerror(errno));

I will get a compilation error because errno is an int and strerror(errno) returns a pointer to a string. I can fix the error by changing the format string or the fifth and sixth arguments. (ERR_ABORT is a set of flags defined in the same header that declares err_logmsg().)

There are two numbers in the PRINTFLIKE macro because there could be other arguments between the format string and the first of the arguments used by the format string. For example, an alternative function could be:

extern void err_writer(FILE *fp, const char *format, int flags, int estat, ...) PRINTFLIKE(2,5);

This tells the compiler that the format string is the second argument, but that the corresponding arguments that get formatted still appear starting at the fifth argument.

The header file for this code contains the lines:

#ifdef __GNUC__ #define PRINTFLIKE(n,m) __attribute__((format(printf,n,m))) #define NORETURN() __attribute__((noreturn)) #else #define PRINTFLIKE(n,m) /* If only */ #define NORETURN() /* If only */ #endif /* __GNUC__ */

Answer3:

Probably tells the compiler that the corresponding function has printf-like semantics.

This can enable the compiler to issue warnings at compile-time when the modifiers in the format string do not correspond to the type or the count of the passed arguments.

There is no other way that the compiler can have the knowledge to tell you that %u isn't the right formatting for an int when calling printf, sprintf, fprintf, etc.

I asked the reverse question a few months ago: <a href="https://stackoverflow.com/questions/3709620/are-printf-sprintf-compiler-warnings-a-conceptual-break" rel="nofollow">Are printf/sprintf compiler warnings a conceptual break?</a>

Recommend

  • AngularJS: Name a variable using another's variable value
  • How to download a PDF that is in a hyperlink using VB in Outlook 2016 [closed]
  • Assign a number of occurrence to each record (sort of Count if)
  • Why do I get IPC delays on 20% busy machine
  • How to test Rate this app functionality before submitting to app store
  • Changing UITableViewCell's width constraint's constant value for each instance
  • mod rewrite clear home url for tomcat + apache
  • TeamCity: Scripting elements jsp:declaration, jsp:expression, jsp:scriptlet are disallowed here
  • Java Regex Finding digits in a String
  • Get all the values from excel file by using linqtoexcel
  • Eclipse ADT Plugin crashed after updating to version 22.0
  • Fixing corrupt encoding (with Python)
  • no endpoints available for service \\“kubernetes-dashboard\\”
  • Azure NodeJS Error: ENOENT, open 'D:\\home\\site\\wwwroot\\bin\\views\\'
  • Google Compute instance receiving email
  • Python Equivalent of Java's 'Keystore'?
  • javax.net.ssl.SSLException: SSL handshake aborted Connection reset by peer while calling webservice
  • VB.NET and LINQ - Group by in a DataTable
  • Passing HTTP Post from AWS API GW to Lambda
  • Combine two jagged lists into one
  • How to implement Google Drive and Google Plus sdk in ios project
  • How to move to lines with the same indentation in Visual Studio Code
  • Query regarding com.jcraft.jsch.JSchException: UnknownHostKey: x.y.com. DSA key fingerprint is “ac:e
  • Windows biometric framework sample umdf driver: This device cannot start. (Code 10)
  • playing mp3 from nsbundle
  • Query takes almost two seconds but matches only two rows - why isn't the index helping?
  • Set SelectedIndex of ListView in FlipView_SelectionChanged event
  • How to join two tables from different databases
  • Swift: UIView.animate works unexpectedly
  • Thumbnails for mxml components in Flex
  • Separating definition/instantiation of template classes without 'extern'
  • How to create subsets of a single set of elements with XSLT?
  • trigger ontouch event programmatically
  • Dynamic XML Schema Validates Subsection of Document
  • Why is ordered choice in pyparsing failing for my use case?
  • VSTS work items list through REST API
  • Angular FormGroup won't update it's value immediately after patchValue or setValue