|
a command line
free external tools,
java sources
cpp sources
articles
|
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 <stdio.h> // 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.35000example 02: string formatting
#include <stdio.h> // for printf
#include <string.h> // 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 the 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";
printf("company: %-10.10s | " // continue on next line,
"location: %10.10s | " // strings are joined by compiler.
"turnover: %08s$\n",
psz1,psz2,psz3
);
// - find phrases in a string surrounded by < >
// - print only the parts between < >
char *pszFull = "This is a <highlight>hello "
"world</highlight> 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: /highlightexample 03:
#include <stdio.h> // for printf
#include <string.h> // for strchr
#include <stdarg.h> // 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, ...); // see perr below
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<iMaxFiles; iFile++)
{
printf("processing file %03ld of %03ld "
"(%02ld%%) ... \r",
iFile, iMaxFiles, iFile*100/iMaxFiles
);
fflush(stdout);
}
printf("\nprocessing done.\n");
return 0;
}
void perr(const char *pszFormat, ...)
{
static char szErrBuf[200];
va_list argList;
va_start(argList, pszFormat);
::vsnprintf(szErrBuf, sizeof(szErrBuf)-10, pszFormat, argList);
szErrBuf[sizeof(szErrBuf)-10] = '\0';
fprintf(stderr, "error: %s", szErrBuf);
}
output: (after program completion) error: cannot open file: thetestfile.txt [rc 100] processing file 049 of 050 (98%) ... processing done.
|
|