Skip to content

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

License

Notifications You must be signed in to change notification settings

chlimous/42-ft_printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

42 ft_printf

ft_printf

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

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);
int ft_asprintf(char **str, const char *format, ...);
int ft_vasprintf(char **str, const char *format, va_list args);

DESCRIPTION

The function 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.
'n' functions write at most size bytes to str.
'a' functions allocate memory 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
# For o, x, X specifiers, the text 0, 0x, 0X, respectively, is prepended to non-zero numbers.
For f, F, e, E specifiers, the output always contains a decimal point.
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, the number of digits to appear after the radix character for f, F, e, and E 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 f F e E c s p n
(none) int unsigned int double int char * void * int *
hh signed char unsigned char signed char *
h short int unsigned short int short int *
l long int unsigned long int wint_t long int *
ll long long int unsigned long long int long long int *
j intmax_t uintmax_t intmax_t *
z ssize_t size_t size_t *
t ptrdiff_t ptrdiff_t ptrdiff_t *
L long double

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 .
f Floating point number (lowercase) - 0 (space) # + .
F Floating point number (uppercase) - 0 (space) # + .
e Floating point number, scientific notation (lowercase) - 0 (space) # + .
E Floating point number, scientific notation (uppercase) - 0 (space) # + .
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