Difference between revisions of "printf"

From SEGGER Wiki
Jump to: navigation, search
m
m
Line 1: Line 1:
 
[[Category:Knowledge Base]]
 
[[Category:Knowledge Base]]
"printf" is a function in the C-standard library that outputs text to the terminal, typically debug console.
+
"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==
 
==Hello world==
Line 11: Line 14:
 
return 0;
 
return 0;
 
}
 
}
  +
</syntaxhighlight>
  +
  +
==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:
  +
<syntaxhighlight lang="c">
  +
printf("Total sum is: %d\n", Sum);
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 07:36, 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);