Copy and paste examples of the printf syntax, and how to use va_arg in a self-defined printf function.
example 01: printing basic datatypes
#include // for printf
int main(int argc, char *argv[])
{
// print "the date is: 05.01.2006",
// i.e. 2- or 4-digit with leading zeros
// using 32-bit 'long' datatype
long lday = 5;
long lmonth = 1;
long lyear = 2006;
printf("the date is: %02ld.%02ld.%04ld\n",
lday,lmonth,lyear);
// - print 8-digit hex value
// - print a pointer value
unsigned long ulID = 0x12345678;
unsigned long *pID = &ulID;
printf("hex value: 0x%02lX at address: %p\n",
ulID, pID);
// - print 4 bytes of a 32-bit ulong value
// as separate hex values
unsigned char uc1 = (unsigned char)(ulID >> 24);
unsigned char uc2 = (unsigned char)(ulID >> 16);
unsigned char uc3 = (unsigned char)(ulID >> 8);
unsigned char uc4 = (unsigned char)(ulID >> 0);
printf("hex bytes: %02X %02X %02X %02X\n",
uc1,uc2,uc3,uc4);
// - print double value like "70.35000"
double dTemp = 70.35;
printf("temperature: %5.5f\n", dTemp);
}
output:
the date is: 05.01.2006
hex value: 0x12345678 at address: 0012FEDC
hex bytes: 12 34 56 78
temperature: 70.35000
example 02: string formatting
#include // for printf
#include // for strchr
int main(int argc, char *argv[])
{
// - print three strings,
// - the first one with 10 chars,
// left-justified, and if there are
// more than 10 chars, truncate rest.
// - the second one with 10 chars, right,
// prefixed with blanks
// - the third one with 8 chars, right,
// prefixed with zeros
char *psz1 = "FooBar Inc. formerly known as "
"Foo Bar Systems Corp.";
char *psz2 = "NY";
char *psz3 = "105000";
// this prints one line, strings joined by compiler
printf("company: %-10.10s | "
"location: %10.10s | "
"turnover: %08s$\n",
psz1,psz2,psz3
);
// - find phrases in a string surrounded by < >
// - print only the parts between < >
char *pszFull = "This is a hello "
"world example.\n";
for (char *pszCur=pszFull; *pszCur;) // cursor
{
char *pszBra = strchr(pszCur, '<');
if (!pszBra) break;
pszBra++; // skip '<'
char *pszKet = strchr(pszBra, '>');
if (!pszKet) break;
// print n characters of a string,
// suppling length as int
printf("phrase found: %.*s\n",
(int)(pszKet-pszBra), pszBra);
pszCur = pszKet; // continue past phrase
}
}
output:
company: FooBar Inc | location: NY | turnover: 00105000$
phrase found: highlight
phrase found: /highlight
example 03:
- writing an own printf-like function for error output
- printing a progress indicator
#include // for printf
#include // for strchr
#include // for va_arg
#ifdef _WIN32
#define vsnprintf _vsnprintf
#endif
int main(int argc, char *argv[])
{
// create and use an own printf-like function "perr",
// which behaves exactly like printf, but
// - prefixes the output with "error: "
// - prints to stderr stream, not stdout
void perr(const char *pszFormat, ...);
char *pszFileName = "thetestfile.txt";
long lStatusCode = 100;
perr("cannot open file: %s [rc %ld]\n",
pszFileName, lStatusCode);
// print a progress indicator:
// "processing file 000 of 050 (00%) ..."
// "processing file 001 of 050 (02%) ..."
// "processing file 002 of 050 (04%) ..."
// ...
// "processing file 049 of 050 (98%) ..."
// but always within the same line.
long iMaxFiles=50;
for (long iFile=0; iFile
output: (after program completion)
error: cannot open file: thetestfile.txt [rc 100]
processing file 049 of 050 (98%) ...
processing done.
|