Printf

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

🖨

Printf
🖨 Acelera — Printf

What is ft_printf?
This project is about recoding the famous printf C function to learn variadic functions and improve algorithmic methodology.

int ft_printf(const char * (restrict) format, ...);

ft_printf can print different contents depending on conversions and flags. You can print using the following syntax:

%[flag][min-width].[precision][length modifier][conversion specifier]

min-width depending on cases, will add empty spaces. Precision, depending on cases, will add '0'.

See below what are flags, length modifier and conversions.

Features
Conversions:

c char
s char *
p void * (to print pointer's adress)
f floats
u unsigned int
o unsigned int (octal)
x/X: unsigned int (hexadecimal)
d/i: int

flags:

- Left align.
+ Sign of number always O/P.
space Positive values begin with a space.
0 Field is padded with 0's instead of space.
# has different uses:
%#o 0 prefix inserted.
%#x 0x prefix added to non-zero values.
%#X 0X prefix added to non-zero values.

length modifiers:

ft_printf
include "stdarg.h" When you want to have function which can take variable number of arguments. The last
argument of a function (...) will be variable arguments. Need va_list type pointer to read arguments stored in
stack. Initializes ap, which can be passed to va_arg() for each argument to be passed.
https://velog.io/@ljiwoo59/ftprintf

h short
l long
L long double

C library function - printf() Linguagem C - Funções Variádicas

Printf 1
The C library function int printf(const Antes de começar, vamos conversar
char *format, ...) sends formatted um pouco sobre abstrações, sistemas,
output to stdout. Following is the funções e definir o que todos esses
https://www.tutorialspoint.com/c_st https://www.vivaolinux.com.br/artig
andard_library/c_function_printf.htm o/Linguagem-C-Funcoes-Variadicas

how can i write a function that takes a variable number


Write
of arguments
your own printf() function in c Sintaxe de especificação de formato: `printf`
This is the question asked to me in my HCL interview "How Writecan I write
your own
a printf() function in c I As várias funções printf e wprintf usam uma cadeia
function that takes a variable number of arguments?" after the wasanswering
wondering this
where I can find the C formato e argumentos opcionais e produzem uma s
they start cross questioning on this like "What are the limitations
codewith
that's
this?"
used,"so that when I write de caracteres de saída. A cadeia de caracteres de
http://www.firmcodes.com/can-write-function-takes-variable-number-argumen
http://www.firmcodes.com/write-printf-f https://docs.microsoft.com/pt-br/cpp/c-runtime-l
ts-limitations-vprintf/ unction-c/ cation-syntax-printf-and-wprintf-functions?view=ms

C Variadic Function Implementation Explained with Example


printf Code What is the difference between "long", "long l
In C programming, variadic function will contribute to the flexibility
int printf
of (the
const char * format, ... ); Print long and long int are identical. So are long long and
program that you are developing. To understand this flexibility,
formatted
let us start
data to stdout Writes the C string As to the difference between the two sets, the C++
with a basic example. pointed by format to the standard output and that long long is at least as wide as long.
https://www.thegeekstuff.com/2017/05/c-variadic-functions/ https://www.cplusplus.com/reference/cst https://stackoverflow.com/questions/18971732/w
dio/printf/ ng-int-and-long-long-i

C library function - printf() Variadic function - Wikipedia printf.c


The C library function int In mathematics and in include #include #include #include #include
printf(const char *format, ...) computer programming, a #include #include #include #include #include
sends formatted output to variadic function is a function of #include #ifdef MACH_BSD #include #endif
https://www.tutorialspoin https://en.wikipedia.org/wiki/ https://opensource.apple.com/source/xnu/x
t.com/c_standard_library/c_f Variadic_function#:~:text=In%2 nu-4570.41.2/osfmk/kern/printf.c.auto.html
unction_printf.htm 0mathematics%20and%20in%2
0computer,dating%20back%20t
o%201936%E2%80%931937
Variadic Functions in C
This is a tutorial on Variadic Functions in C.

https://www.youtube.com/watch?v=FgvrnYScdH8

Secrets of printf(): C Programming


Using " instead of less than/greater than brackets because they are not allowed in youtube descriptions. This
program is for the video. */#include "stdio....

https://www.youtube.com/watch?v=Y9kUWsyyChk

Printf 2
Operador Elipse (...) e o Uso da Biblioteca stdarg.h - Linguagem C Formatted Printing Function
Já imaginou como funciona o printf? Se projetarmos uma função que leva stdio.h contains the function prototype and any other definitions that are
dois argumentos e passamos três parâmetros, vamos receber um erro, ou needed for the printf function
seja, suponhamos que temos a seguinte função: Int fun (int a, int b); E https://user-web.icecube.wisc.edu/~dglo/c_class/printf.html
http://qiinformatica.blogspot.com/2016/12/operador-elipse-e-o-uso-da-b
iblioteca.html

ft_printf tutorial - 42project rchallie/ft_printf Ft_printf


ft_printf is a project that Permalink Failed to This project is about
mimics the real printf load latest commit recoding the famous
function.Although wide in information. printf C function to
https://csnotes.medium. https://github.co https://git.42l.fr/a
com/ft-printf-tutorial-42proje m/rchallie/ft_printf mamy/Ft_printf
ct-f09b6dc1cd0e

http://wiki.icmc.usp.br/images/1/1e/SCC501_-_Introdu%C3%A7%C3%A3o_a_linguagem_C-Aulas-1-2-3-4-5.pdf

recode printf  😲
Introduction

Step by step

Mindmap

Study resources

Usage

Testing

introduction
The versatility of the printf function in C represents a great exercise in programming for us. This project is of moderate difficulty.
It will enable you to discover variadic functions in C. The key to a successful ft_printf is a well-structured and good extensible
code.

step_by_step
First of all, I wrote this little program just to understand the subject and printf's flags.
Those are the format identifier's I nedd to recode:

% type

%c character
%s string

Printf 3
% type

%p pointer
%d decimal signed integer

%i integer
%u unsigned integer
%x hex integer (lowercase)

%X hex integer (uppercase)


%% just the %

%d %i Decimal signed integer.


%o Octal integer.
%x %X Hex integer.
%u Unsigned integer.
%c Character.
%s String. See below.
%f double
%e %E double.
%g %G double.
%p pointer.
%n Number of characters written by this printf.
No argument expected.
%% %. No argument expected.

And those are the flags:

flag ?

num (number between % and the identifier) minimum field width


'-' left justify
'0' field padded with 0's instead of blanks

'.' precision
'*' indicates that the maximum or minimum field width will be passed as parameter

For %d and %i, the precision is the minimum number of digits to print.
For %s, the precision is the maximum field width.

To be aware:

flag '0' is ignored when flag '-' is present

flag '0' is ignored when flag '.' is present (%d e %i)

flag '0' results in undefined behavior with '%c', '%s' and '%p'

flag '.' results in undefined behavior with '%c' and '%p'

(due my researches and empirical tests, 'undefined behavior' means the flags will be ignored, just as the '0' with '-')

Format tags prototype is %|flags| |width| |.precision| |length| |specifier|

The width specification never causes a value to be truncated. If the number of characters in the
output value is greater than the specified width, or if width isn't provided, all characters of the value
are output.

mindmap

Printf 4
integers

decimal to hexadecimal conversion

study
printf overview

secrets of printf

variadic functions -intro

va-arg

Printf 5
layout of directories

format specification syntax

%p versus %x

decimal to hex conversion

usage
clone this repository

git clone https://github.com/paulahemsi/ft_printf.git

and

comand result
make compile ftprintf library
make test compile and run tests edit your own test here

make flags compile and run printf flags example


make clean clean .o

make fclean clean .o and .a

tests
ft_printf_test by Chacharle

PFT_EXAM by César Claude

42TESTERS-PRINTF by Mazoise

Printf 6
The Visual Workspace | Whimsical

https://whimsical.com/printf-UQJ9HhJja2gp5DAZC4JCqQ

https://whimsical.com/printf-UQJ9HhJja2gp5DAZC4JCqQ

Printf 7

You might also like