chlimous / 42-ft_printf

Fully buffered implementation of printf / dprintf / vprintf / vdprintf / sprintf / snprintf / vsprintf / vsnprintf / asprintf / vasprintf

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

42 ft_printf

ft_printf

Fully buffered implementation of printf / dprintf / vprintf / vdprintf / sprintf / snprintf / vsprintf / vsnprintf

SYNOPSIS

int ft_printf(const char *format, ...);
int ft_dprintf(int fd, const char *format, ...);
int ft_vprintf(const char *format, va_list args);
int ft_vdprintf(int fd, const char *format, va_list args);
int ft_sprintf(char *str, const char *format, ...);
int ft_snprintf(char *str, size_t size, const char *format, ...);
int ft_vsprintf(char *str, const char *format, va_list args);
int ft_vsnprintf(char *str, size_t size, const char *format, va_list args);

DESCRIPTION

The functions ft_printf() writes output to stdout.
'd' functions write output to the given file descriptor.
'v' functions are called with a va_list instead of a variable number of arguments.
's' functions write to the character string str.
's' functions write at most size bytes to str.

Format of the format string

The format string is a character string, beginning and ending in its initial shift state, if any. The format string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments. Each conversion specification is introduced by the character %, and ends with a conversion specifier. In between there may be (in this order) zero or more flags, an optional minimum field width, an optional precision and an optional length modifier.

The overall syntax of a conversion specification is:

%[flags][width][.precision][length]specifier

Flag characters

The character % is followed by zero or more of the following flags:

Flag Description
# Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.
0 Left-pads the number with zeroes (0) instead of spaces when width is specified.
- Left-justify within the given field width.
(space) If no sign is going to be written, a blank space is inserted before the value.
+ Forces to preceed the result with a plus or minus sign.

Field width

An optional decimal digit string specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left.
Instead of a decimal digit string one may write "*" to specify that the field width is given in the next argument, which must be of type int.

Precision

An optional precision, in the form of a period ('.') followed by an optional decimal digit string. Instead of a decimal digit string one may write "*", to specify that the precision is given in the next argument, which must be of type int. If the precision is given as just '.', the precision is taken to be zero.
This gives the minimum number of digits to appear for d, i, u, o, x, X and k conversions, or the maximum number of characters to be printed from a string for s conversion.

Length modifier

The length modifier modifies the length of the data type.

Length / Specifier d i u o x X k c s p n
(none) int unsigned int int char * void * int *
hh signed char unsigned char
h short int unsigned short int
l long int unsigned long int wint_t
ll long long int unsigned long long int
j intmax_t uintmax_t
z ssize_t size_t
t ptrdiff_t ptrdiff_t

Conversion specifier

Specifier Output Applicable flags / precision
d, i Signed decimal integer - 0 (space) + .
u Unsigned decimal integer - 0 .
o Unsigned octal integer - 0 # .
x Unsigned hexadecimal integer (lowercase) - 0 # .
X Unsigned hexadecimal integer (uppercase) - 0 # .
k Unsigned integer in base specified as second argument
(This is a custom specifier. Not POSIX-compliant.)
- 0 .
c Character -
s String - .
p Pointer address -
% Prints '%' character
n The number of characters written so far is stored into the integer pointed to by the corresponding argument

RETURN VALUE

The number of printed characters or -1 in case of error:

  • format parameter is NULL
  • Undefined specifier
  • File descriptor error
  • Flag/Precision/Length not applicable
  • Duplicate flag
  • Write error
  • Memory allocation error

About

Fully buffered implementation of printf / dprintf / vprintf / vdprintf / sprintf / snprintf / vsprintf / vsnprintf / asprintf / vasprintf

License:GNU General Public License v3.0


Languages

Language:C 97.4%Language:Makefile 2.6%