Printf

From PortaWiki

Jump to: navigation, search

Use %lx instead of %p in portable code. %p is fairly portable on itself, but the output is not - sometimes it has 0x prefix, sometimes does not.

Contents

Standards

C99 has introduced some new format strings that are not supported on all platforms. To be safe avoid %a, %A and (list more here).

printf family on Tru64

The printf family in the libc's of older Tru64 systems does not understand "long long" types (eg "%lld"). This is believed to be the case for version 4.0F and below.

vsnprintf on HP-UX

On overflow, vsnprintf returns the number of bytes returns the number of bytes actually written, not the number of bytes that should have been written. (Tested on HP-UX 11.11, believed to be true on other versions too.)

Windows

Prior to VisualStudio 9 2008, vsnprintf and friends didn't exist (except as non standard _vsnprintf). Now, in VS9, it does exist. So if you have compatibility code, you'll need to fix it.

Cmake (from MySQL):

IF(CMAKE_GENERATOR MATCHES "Visual Studio 9")
  # Fool zlib on Win32 with VS9
  # It just includes config.h, which we don't have on win32.. so we have to
  # do it here alongside config-win.h.
  ADD_DEFINITIONS("-DHAVE_VSNPRINTF")
ENDIF(CMAKE_GENERATOR MATCHES "Visual Studio 9")


a config.h for win32 (from MySQL):

#if _MSC_VER >= 1500   /* VS9 (2008) has vsnprintf)
#define HAVE_VSNPRINTF
#define HAVE_STRTOUL
#endif
Personal tools