Prinftf in C Print Txt Varialbe and Text Again
Printf()
Input/Output
William J. Buchanan BSc, CEng, PhD , in Software Development for Engineers, 1997
2.3.1 C standard output (printf(), puts() and putchar())
There are three bones output functions in C, these are:
The printf() function sends a formatted cord to the standard output (the display). This cord tin brandish formatted variables and special control characters, such equally new lines ('\n'), backspaces ('\b') and tabspaces ('\t'); these are listed in Tabular array 2.ane.
Table 2.i. Special command (or escape sequence) characters
| Characters | Role | Characters | Function |
|---|---|---|---|
| \″ | Double quotes (″) | \b | Backspace (movement back one space) |
| \ ' | Single quote (') | \f | Form-feed |
| \\ | Backslash (\) | \n | New line (line-feed) |
| \nnn | ASCII grapheme in octal lawmaking, such as \ 0 41 gives 'excl' | \r | Carriage return |
| \0xnn | ASCII character in hexa-decimal code, such as \0×41 gives an 'A' | \t | Horizontal tab spacing |
| \a | Audible bell |
The puts() function writes a cord of text to the standard output and no formatted variables can be used. At the end of the text, a new line is automatically appended.
The parameters passed into printf() are known as arguments; these are separated commas. C Plan two.1 contains a printf() argument with only one argument, that is, a text string. This string is referred to as the message cord and is always the starting time statement of printf(). It tin comprise special control characters and/or parameter conversion control characters.
Conversion command characters depict the format of how the message string uses the other arguments. If printf() contains more than i argument then the format of the output is defined using a percent (%) character followed by a format description character. A signed integer uses the %d conversion control characters, an unsigned integer %u. A floating indicate value uses the %f conversion command characters, while scientific notation uses %east. Tabular array ii.2 lists the main conversion control characters.
Table 2.two. Conversion control characters
| Operator | Format | Operator | Format |
|---|---|---|---|
| %c | single character | %due south | string of characters |
| %d | signed decimal integer | %o | unsigned octal integer |
| %e | scientific floating point | %% | prints % character |
| %f | floating point | %x | unsigned hexadecimal integer |
| %u | unsigned decimal integer | %g | either floating point or scientific notation |
Figure 2.2 shows an example of the printf() statement with 4 arguments. The first argument is the message string followed by the parameters to be printed in the message string. In this case the parameters are val1, val2 and ch; val1 is formatted in the message string as a floating point (%f), val2 as an integer (%d) and ch as a character (%c). Finally, a new line character ('\northward') is used to strength a new line on the output.
Effigy ii.2. An example printf() statement
A numerical value is output to a given specification using a precision specifier. This specifies the number of characters used to display the value and the number of places after the decimal bespeak. The general format of a floating point value is:
where yard is the width of the value (the number of digits including the decimal signal), north is the number of digits post-obit the decimal point, and X is the format type (f for float). The full general format of a string or integer is:
where X is the format type (c for graphic symbol, s for string or d for integer) and k is the width of the output. Tabular array ii.three gives a few examples.
Table 2.iii. Case of conversion command modifiers
| Format | Function |
|---|---|
| %.3f | format floating signal value with 3 decimal places and a default width |
| %8.3f | format floating signal with viii reserved spaces and three places after the decimal point such every bit 32.4 53 |
| %10d | format integer for 10 reserved spaces such every bit 2 three |
| %3o | format octal integer number for 3 hexadecimal characters |
| %10.6e | format exponent format with half-dozen decimal places |
Read total chapter
URL:
https://www.sciencedirect.com/science/commodity/pii/B9780340700143500475
Common Issues, Causes, and Solutions
Shane Cook , in CUDA Programming, 2013
Debug level and press
Besides as having a single release and debug version, it's oft useful to have a debug level that is easily changeable, for instance, by setting the value of a global variable, #define, or other constant. You may besides wish to allow for setting such a parameter via the command line, for example -debug=v to ready debug level five, etc.
During evolution, y'all can add useful information messages to the lawmaking, for example:
#ifdef DEBUG
#ifndef DEBUG_MSG
// Set to 0..four to print errors
// 0 = Critical (program abort)
// i = Serious
// 2 = Problem
// 3 = Warning
// 4 = Information
#define DEBUG_ERR_LVL_CRITICAL (0u)
#define DEBUG_ERR_LVL_SERIOUS (1u)
#ascertain DEBUG_ERR_LVL_PROBLEM (2u)
#define DEBUG_ERR_LVL_WARNING (3u)
#ascertain DEBUG_ERR_LVL_INFO (4u)
// Define the global used to set the error indication level
extern unsigned int GLOBAL_ERROR_LEVEL;
void debug_msg(char ∗ str, const unsigned int error_level)
{
if (error_level <= GLOBAL_ERROR_LEVEL)
{
if (error_level == 0)
printf("\due north∗∗∗∗∗∗∗∗∗∗∗%s%s", str, "∗∗∗∗∗∗∗∗∗∗∗∗∗∗\n");
else
printf("\north%s", str);
fflush(stdout);
if (error_level == 0)
exit(0);
}
}
#define DEBUG_MSG(x, level) debug_msg(x, level)
#else
#ascertain DEBUG_MSG(x, level)
0 Response to "Prinftf in C Print Txt Varialbe and Text Again"
Post a Comment