36 lines
1.5 KiB
C
36 lines
1.5 KiB
C
#include <stdio.h>
|
|
#include <stdint.h>
|
|
int main(void) {
|
|
char buf[256];
|
|
int count;
|
|
// Length modifiers.
|
|
snprintf(buf, sizeof(buf), "%hhd %hd %d %ld %lld %jd %zd %td",
|
|
(signed char)-5, (short)-1000, -12345, -1234567890L,
|
|
(long long)-1234567890123LL, (long long)42LL,
|
|
(unsigned long)1024UL, (int)64);
|
|
*(volatile uint16_t *)0x025000UL = (uint16_t)snprintf(buf, sizeof(buf),
|
|
"<%5d>", 42);
|
|
// Width via '*'.
|
|
*(volatile uint16_t *)0x025002UL = (uint16_t)snprintf(buf, sizeof(buf),
|
|
"<%*d>", 5, 7);
|
|
// Precision via '*'.
|
|
*(volatile uint16_t *)0x025004UL = (uint16_t)snprintf(buf, sizeof(buf),
|
|
"<%.*s>", 3, "abcdefg");
|
|
// Zero pad + sign.
|
|
*(volatile uint16_t *)0x025006UL = (uint16_t)snprintf(buf, sizeof(buf),
|
|
"%+08d", 42);
|
|
// %n: write count so far through a pointer arg.
|
|
snprintf(buf, sizeof(buf), "abc%n", &count);
|
|
*(volatile uint16_t *)0x025008UL = (uint16_t)count;
|
|
// %lld (long long).
|
|
int len = snprintf(buf, sizeof(buf), "%lld", (long long)0x123456789LL);
|
|
*(volatile uint16_t *)0x02500AUL = (uint16_t)len;
|
|
// %zu (size_t).
|
|
snprintf(buf, sizeof(buf), "%zu", (unsigned long)4096UL);
|
|
// Hash-based check: read back the first uint16 of the formatted buf.
|
|
*(volatile uint16_t *)0x02500CUL = *(uint16_t *)buf; // "40"
|
|
// Octal.
|
|
snprintf(buf, sizeof(buf), "%o", 0644);
|
|
*(volatile uint16_t *)0x02500EUL = *(uint16_t *)buf; // "64"
|
|
return 0;
|
|
}
|