Difference between revisions of "printf"
m |
m |
||
Line 58: | Line 58: | ||
| <code>c</code> |
| <code>c</code> |
||
|<code>char</code> (character). |
|<code>char</code> (character). |
||
− | |- |
||
− | | <code>a</code>, <code>A</code> |
||
− | |<code>double</code> in hexadecimal notation, starting with <code>0x</code> or <code>0X</code>. <code>a</code> uses lower-case letters, <code>A</code> uses upper-case letters.<ref>{{cite web|url=https://www.gnu.org/software/libc/manual/html_node/Table-of-Output-Conversions.html#Table-of-Output-Conversions |title="The GNU C Library Reference Manual", "12.12.3 Table of Output Conversions" |publisher=Gnu.org |date= |accessdate=2014-03-17}}</ref><ref> |
||
− | [http://www.cplusplus.com/reference/cstdio/printf/ "printf"] |
||
− | (<code>%a</code> added in C99) |
||
− | </ref> (C++11 iostreams have a <code>hexfloat</code> that works the same). |
||
|} |
|} |
Revision as of 12:23, 6 July 2019
"printf" is a function in the C-standard library that outputs text to standard output. Standard output is typically the terminal or debug console, depending on the system the program is running on. The f stands for formatted, allowing the function to output not just fixed strings, but also text with variable data in it.
Contents
Hello world
The simplest of all C-programs is known as "Hello world" program. It outputs "Hello world", using printf.
#include <stdio.h>
int main(void) {
printf("Hello world!\n");
return 0;
}
Using parameters
Parameters need to be specified in the format string. A parameter definition starts with a % character. A simple example with one numerical parameter looks as below:
printf("Total sum is: %d\n", Sum);
Parameter specification
Type specification
Type field
The Type field can be any of:
Character Description %
Prints a literal %
character (this type doesn't accept any flags, width, precision, length fields).d
,i
int
as a signed decimal number.%d
and%i
are synonymous.u
Print decimal unsigned int
.f
,F
double
in normal (fixed-point) notation.f
andF
only differs in how the strings for an infinite number or NaN are printed (inf
,infinity
andnan
forf
;INF
,INFINITY
andNAN
forF
).e
,E
double
value in standard form ([-
]d.ddde
[+
/-
]ddd). AnE
conversion uses the letterE
(rather thane
) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is00
. In Windows, the exponent contains three digits by default, e.g.1.5e002
, but this can be altered by Microsoft-specific_set_output_format
function.g
,G
double
in either normal or exponential notation, whichever is more appropriate for its magnitude.g
uses lower-case letters,G
uses upper-case letters. This type differs slightly from fixed-point notation in that insignificant zeroes to the right of the decimal point are not included. Also, the decimal point is not included on whole numbers.x
,X
unsigned int
as a hexadecimal number.x
uses lower-case letters andX
uses upper-case.s
null-terminated string. c
char
(character).