mirror of
https://github.com/tailix/libkernaux.git
synced 2025-04-07 17:32:45 -04:00
Add "examples/snprintf[_va].c"
This commit is contained in:
parent
b3aed09ce9
commit
d1105f1cc6
5 changed files with 61 additions and 3 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -92,6 +92,8 @@
|
|||
/examples/pfa
|
||||
/examples/printf
|
||||
/examples/printf_va
|
||||
/examples/snprintf
|
||||
/examples/snprintf_va
|
||||
/examples/units_human
|
||||
|
||||
/tests/multiboot2_header_print1
|
||||
|
|
10
Makefile.am
10
Makefile.am
|
@ -113,6 +113,8 @@ if ENABLE_TESTS
|
|||
TESTS += \
|
||||
examples/printf \
|
||||
examples/printf_va \
|
||||
examples/snprintf \
|
||||
examples/snprintf_va \
|
||||
tests/test_printf
|
||||
endif
|
||||
endif
|
||||
|
@ -150,6 +152,14 @@ examples_printf_va_SOURCES = \
|
|||
$(libkernaux_a_SOURCES) \
|
||||
examples/printf_va.c
|
||||
|
||||
examples_snprintf_SOURCES = \
|
||||
$(libkernaux_a_SOURCES) \
|
||||
examples/snprintf.c
|
||||
|
||||
examples_snprintf_va_SOURCES = \
|
||||
$(libkernaux_a_SOURCES) \
|
||||
examples/snprintf_va.c
|
||||
|
||||
examples_units_human_SOURCES = \
|
||||
$(libkernaux_a_SOURCES) \
|
||||
examples/units_human.c
|
||||
|
|
|
@ -60,9 +60,8 @@ API
|
|||
* Code from [https://github.com/mpaland/printf](https://github.com/mpaland/printf). Thank you!
|
||||
* [printf](/examples/printf.c)
|
||||
* [vprintf](/examples/printf_va.c)
|
||||
* `snprintf`
|
||||
* `vsnprintf`
|
||||
* `sprintf`
|
||||
* [snprintf](/examples/snprintf.c)
|
||||
* [vsnprintf](/examples/snprintf_va.c)
|
||||
|
||||
|
||||
|
||||
|
|
22
examples/snprintf.c
Normal file
22
examples/snprintf.c
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include <kernaux/printf.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
static char buffer[BUFFER_SIZE];
|
||||
|
||||
int main()
|
||||
{
|
||||
const int result = kernaux_snprintf(
|
||||
buffer,
|
||||
sizeof(buffer),
|
||||
"Hello, %s! Session ID: %u.",
|
||||
"Alex",
|
||||
123
|
||||
);
|
||||
assert((size_t)result == strlen(buffer));
|
||||
assert(strcmp(buffer, "Hello, Alex! Session ID: 123.") == 0);
|
||||
return 0;
|
||||
}
|
25
examples/snprintf_va.c
Normal file
25
examples/snprintf_va.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
#include <kernaux/printf.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
static char buffer[BUFFER_SIZE];
|
||||
|
||||
static int my_snprintf(const char *const format, ...)
|
||||
{
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
const int result = kernaux_vsnprintf(buffer, sizeof(buffer), format, va);
|
||||
va_end(va);
|
||||
return result;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
const int result = my_snprintf("Hello, %s! Session ID: %u.", "Alex", 123);
|
||||
assert((size_t)result == strlen(buffer));
|
||||
assert(strcmp(buffer, "Hello, Alex! Session ID: 123.") == 0);
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue