Difference between revisions of "printf"

From SEGGER Wiki
Jump to: navigation, search
m
m
Line 43: Line 43:
 
| <code>f</code>, <code>F</code>
 
| <code>f</code>, <code>F</code>
 
|<code>double</code> in normal ([[Fixed-point arithmetic|fixed-point]]) notation. <code>f</code> and <code>F</code> only differs in how the strings for an infinite number or NaN are printed (<code>inf</code>, <code>infinity</code> and <code>nan</code> for <code>f</code>; <code>INF</code>, <code>INFINITY</code> and <code>NAN</code> for <code>F</code>).
 
|<code>double</code> in normal ([[Fixed-point arithmetic|fixed-point]]) notation. <code>f</code> and <code>F</code> only differs in how the strings for an infinite number or NaN are printed (<code>inf</code>, <code>infinity</code> and <code>nan</code> for <code>f</code>; <code>INF</code>, <code>INFINITY</code> and <code>NAN</code> for <code>F</code>).
|-
 
| <code>e</code>, <code>E</code>
 
|<code>double</code> value in standard form ([<code>-</code>]d.ddd <code>e</code>[<code>+</code>/<code>-</code>]ddd). An <code>E</code> conversion uses the letter <code>E</code> (rather than <code>e</code>) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is <code>00</code>. In Windows, the exponent contains three digits by default, e.g. <code>1.5e002</code>, but this can be altered by Microsoft-specific <code>_set_output_format</code> function.
 
|-
 
| <code>g</code>, <code>G</code>
 
|<code>double</code> in either normal or exponential notation, whichever is more appropriate for its magnitude. <code>g</code> uses lower-case letters, <code>G</code> 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.
 
 
|-
 
|-
 
| <code>x</code>, <code>X</code>
 
| <code>x</code>, <code>X</code>

Revision as of 19:49, 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 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 for output, but are different when used with scanf() for input (where using %i will interpret a number as hexadecimal if it's preceded by 0x, and octal if it's preceded by 0.)
u Print decimal unsigned int.
f, F double in normal (fixed-point) notation. f and F only differs in how the strings for an infinite number or NaN are printed (inf, infinity and nan for f; INF, INFINITY and NAN for F).
x, X unsigned int as a hexadecimal number. x uses lower-case letters and X uses upper-case.
s null-terminated string.
c char (character).