UTAT Space Systems Documentation
master

Getting Started

  • Getting Started
  • Useful Websites

Software and Programming

  • Software Tools
  • Troubleshooting
  • Embedded Systems
  • Communication Protocols
  • C Programming
    • C Programming
    • Variables
    • Operators
    • Bitwise Operators
    • Control Structures
    • Functions
    • Header Files
    • #define
    • typedef
    • Pointers
    • Structs
    • .c and .h Files
    • Binary (0b) and Hexadecimal (0x) Literals
    • Integer Types
    • Print Formatting
      • Integer Formatting
    • Volatile Variables
  • Style Guide
  • Testing

Electronics and Altium

  • Altium
  • Electrical Systems
  • Reading Datasheets
  • Electrical Style Guide

IT

  • Remote Access
  • Remote Programming
UTAT Space Systems Documentation
  • Docs »
  • C Programming »
  • Print Formatting
  • Edit on GitHub

Print Formatting¶

C printf() formatting

The UART print() function behaves almost identically to the standard C printf() function. This gives a powerful way to format values as strings for output.

Using `%f` or `%lf` requires a special compiler flag to be enabled. See `lib-common/src/uart/log.c` about this, which is enabled when compiling testing programs but not for main programs.

AVR’s printf() doesn’t support printing 64-bit integers.

Here are the format specifiers for common data types:

Data Type(s) Format Specifier(s)
uint8_t, uint16_t %u (Unsigned)
uint32_t %lu (Long Unsigned)
int8_t, int16_t %d (Decimal - Signed)
int32_t %ld (Long Decimal - Signed)
uint8_t, uint16_t, int8_t, int16_t %x (Hexadecimal - Lowercase), %X (Hexadecimal - Uppercase)
uint32_t, int32_t %lx (Long Hexadecimal - Lowercase), %lX (Long Hexadecimal - Uppercase)
float, double %f (Float)

Integer Formatting¶

An example of controlling integer formatting output as hex:

print("0x%02x", value);
// lowercase hex, always with 2 hex digits (zero padded if necessary)
// add "0x" at the beginning to indicate to the reader that the output is in hex
Next Previous

© Copyright 2018, University of Toronto Aerospace Team. Revision 6631d4d7.

Built with Sphinx using a theme provided by Read the Docs.