Difference between revisions of "printf"
m |
m |
||
Line 35: | Line 35: | ||
|- |
|- |
||
| <code>d</code>, <code>i</code> |
| <code>d</code>, <code>i</code> |
||
− | |<code>int</code> as a signed [[decimal]] number. <code>%d</code> and <code>%i</code> |
+ | |<code>int</code> as a signed [[decimal]] number. <code>%d</code> and <code>%i</code> produce identical output.) |
|- |
|- |
||
| <code>u</code> |
| <code>u</code> |
Revision as of 20:10, 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.
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 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
produce identical output.)u
Print decimal unsigned int
.f
,F
double
in normal (fixed-point) notation.x
,X
unsigned int
as a hexadecimal number.x
uses lower-case letters andX
uses upper-case.s
null-terminated string. c
char
(character).