// How to use parameters of printf
// in the Raspberry Pi Pico with the Pico SDK.
//
// Reference:
// https://cplusplus.com/reference/cstdio/printf/
#include <stdio.h>
#include "pico/stdlib.h"
int main()
{
stdio_init_all();
while (true)
{
// First parameter is a signed long integer
long first = 1234567890L;
// Second parameter is a unsigned hex integer
unsigned int second = 0x1234ABCD;
// Third parameter is 64 bit, but the sprintf can not
// specify the number of bits of a variable.
// Solution: make a "unsigned long long" variable.
unsigned long long third = time_us_64() / 1000000ULL;
// Fourth parameter is unsigned decimal integer
unsigned int fourth = 4000000;
printf("%011ld %08X %06lld %u\n", first, second, third, fourth);
// Now with smaller and larger values
// A very common mistake is the size,
// it is the minimum size.
first = 890L;
second = 0xD;
third = time_us_64() - 1ULL;
fourth = 4;
printf("%011ld %08X %06lld %u\n", first, second, third, fourth);
// Now with calculations.
// Avoid trouble by using only the right variable
// for each parameter.
long make_a_variable_of_it = first*27/123L;
unsigned int put_it_in_a_variable = second/fourth;
unsigned long long a_variable_is_the_solution = time_us_64() / 1000ULL;
unsigned int use_variables = (unsigned int) (first / (long)fourth);
printf("%011ld %08X %06lld %u\n", make_a_variable_of_it, put_it_in_a_variable, a_variable_is_the_solution, use_variables);
printf("----------- -------- ------ -------\n");
sleep_ms(5000);
}
}