Let's build a mini printf function

Поділитися
Вставка
  • Опубліковано 30 жов 2024

КОМЕНТАРІ • 54

  • @minimalSwift
    @minimalSwift 8 місяців тому +12

    my friend, I love your videos! But this miniprintf has a problem. I also put it here so that other can see it. I mean I don't pretend to let you do all the work and criticize. :)
    It is a small thing, but you pass (va_list) )ap to the next function by value and this causes indefinite behavior:
    (Quote as per C99)
    >The type declared is va_list which is an object type suitable for holding information needed by the macros va_start, va_arg, va_end, and va_copy. If access to the varying arguments is desired, the called function shall declare an object (generally referred to as ap in this subclause) having type va_list. The object ap may be passed as an argument to another function; if that function invokes the va_arg macro with parameter ap, the value of ap in the calling function is indeterminate and shall be passed to the va_end macro prior to any further reference to ap
    So. It might happen to work on one architecture, but not another, due to differences in how va_list is implemented between architectures. (it did not work on my Mac M1 for instance, I was getting rubbish printed out! Then I double checked the code).
    > It is permitted to create a pointer to a va_list and pass that pointer to another function, in which case the original function may make further use of the original list after the other function returns.
    Solution is easy. Use a va_list pointer.
    int print_format(char specifier, va_list *ap)
    Thank you for the video. I loved the recursive implementation of the digits... :)

    • @onaecO
      @onaecO  8 місяців тому +5

      Ty very much for the kind words, and for adding value for other. I'll pin this! ( I just didn't know lol )

    • @minimalSwift
      @minimalSwift 8 місяців тому

      Thank you for your work ! :) @@onaecO

    • @FilipBalakovski
      @FilipBalakovski 4 місяці тому

      I have the same problem with printing garbage values on my architecture (MINGW64). Online debugger (GDB) works just fine. Your fix just gives me a warning and the problem persits :/ Any other suggestions?

  • @CodingHk
    @CodingHk Рік тому +8

    Thanks for this awesome video, C seems to hard to understand and I give it enough time yet still complicated (10hrs+ daily) but this video has helped clear and bring back the hope that I can still learn it. Thank you again

    • @onaecO
      @onaecO  Рік тому +4

      10h daily it s massive, congrats!!
      Agree on everything, C is very very hard. We have to really have patience and study 😁

    • @adampassaretti9149
      @adampassaretti9149 9 місяців тому

      Bro i really need a bit of your work ethics, i'm slacking off too much

    • @CodingHk
      @CodingHk 9 місяців тому

      @@onaecO Yea 10hrs a day, a strict coding bootcamp 😀

    • @CodingHk
      @CodingHk 9 місяців тому

      @@adampassaretti9149 you mean me or onaecO?

  • @renans.5135
    @renans.5135 11 місяців тому +1

    Awesome video! Kudos from 42 Rio de Janeiro; you've been a great help since my piscine and you continue to be a a spectacular source of help, man. Keep up the good work!

    • @Yasspums
      @Yasspums 9 місяців тому +1

      does your printf got valid ? im gonna do like him probably

    • @renans.5135
      @renans.5135 9 місяців тому

      It sure did! You can base your work off of him for sure :)

    • @JenniferBerthe-w9l
      @JenniferBerthe-w9l 4 місяці тому +1

      Hi from 42 Paris !!

  • @noskov5
    @noskov5 Рік тому +1

    mate I love your videos. Keep it going

    • @onaecO
      @onaecO  Рік тому +1

      Ty very much bro 👊🏻

  • @vitalis3285
    @vitalis3285 3 місяці тому

    I am glad to be your friend, my friend! And thx, for the video, my friend!

    • @onaecO
      @onaecO  3 місяці тому

      Yeah, rately I happen to say "my friend" lol

  • @vikassharma-lp9bp
    @vikassharma-lp9bp Рік тому +1

    best tutorial on printf, please make video on complete printf :)

    • @onaecO
      @onaecO  Рік тому

      Ty very much 👊🏻

  • @pedroalmeida8453
    @pedroalmeida8453 Рік тому +20

    42 school, anyone?

  • @giovannivolante7302
    @giovannivolante7302 8 місяців тому

    Great work, thanks!
    A side question: are you using vim? In these case, how do you obtain suggestions and autocompletion?

    • @onaecO
      @onaecO  8 місяців тому

      " Basic Vim settings
      set number
      set tabstop=4
      set shiftwidth=4
      set backspace=indent,eol,start
      filetype plugin on
      call plug#begin('~/.vim/plugged')
      " Essential plugins
      Plug 'neoclide/coc.nvim', {'branch': 'release'}
      Plug 'dense-analysis/ale'
      Plug 'preservim/nerdtree'
      Plug 'vim-airline/vim-airline'
      Plug 'vim-airline/vim-airline-themes'
      Plug 'octol/vim-cpp-enhanced-highlight'
      "Plug 'ryanoasis/vim-devicons'
      call plug#end()
      " Set the Airline theme
      let g:airline_theme='onedark'
      " Automatic nerdtree toggle
      map :NERDTreeToggle
      " COC configuration for real-time diagnostics with clangd
      let g:coc_global_extensions = ['coc-clangd']
      " Enable diagnostics (errors and warnings) and update them on cursor hold
      autocmd CursorHold,CursorHoldI * silent call CocActionAsync('diagnosticRefresh')
      " Enter to select suggestion from completion menu
      inoremap pumvisible() ? "\" : "\"
      " Configuration for coc-clangd
      let g:coc_clangd_binary = 'clangd'
      let g:coc_clangd_args = ['-header-insertion=never']
      " Set the color of diagnostic error messages
      highlight CocErrorSign ctermfg=white ctermbg=red guifg=#ffffff guibg=#ff0000
      " Highlight DEBUG statements
      highlight DEBUG ctermfg=black ctermbg=yellow guifg=black guibg=yellow
      " Param suggestion
      highlight CocInlayHint guifg=yellow guibg=NONE ctermfg=yellow ctermbg=NONE
      highlight link CocInlayHintParameter CocInlayHint
      highlight link CocInlayHintType CocInlayHint
      highlight CocInlayHint guifg=yellow guibg=NONE ctermfg=white ctermbg=NONE gui=bold cterm=bold
      highlight link CocInlayHintParameter CocInlayHint
      highlight link CocInlayHintType CocInlayHint
      " Configure ALE
      let g:ale_sign_error = '✘'
      let g:ale_sign_warning = '⚠'
      " Define custom colors for warning text
      highlight link ALEWarningMsg WarningMsg
      "highlight ALEWarning ctermfg=red ctermbg=NONE
      "AUTOSAVE
      "autocmd TextChanged,TextChangedI * silent write
      autocmd TextChanged,TextChangedI *
      \ if &buftype ==# '' || &buftype == 'acwrite' |
      \ silent write |
      \ endif
      " Enable Coc
      let g:coc_global_extensions = [
      \ 'coc-clangd'
      \ ]
      " Use Coc for diagnostics (errors and warnings)
      autocmd CursorHold,CursorHoldI * silent call CocActionAsync('diagnosticRefresh')
      " Enable inline error and warning display
      let g:coc_sign_error = '✘'
      let g:coc_sign_warning = '⚠'
      let g:coc_status_float_enable = 1 " Enable floating status information
      let g:coc_status_colors = {
      \ "Warning": "white",
      \ }
      " Set the colorscheme for floating windows
      au ColorScheme * hi CocFloating ctermfg=white guifg=white ctermbg=black guibg=black

    • @giovannivolante7302
      @giovannivolante7302 8 місяців тому

      @@onaecO Many many thanks

  • @giovannitroiano
    @giovannitroiano Рік тому +2

    Thanks

    • @onaecO
      @onaecO  Рік тому +3

      👀♥️♥️

  • @ynelniino
    @ynelniino Рік тому +1

    thank you 🙏🙏

    • @onaecO
      @onaecO  Рік тому +1

      Thank you for the comment 🔥

  • @erikseferi2241
    @erikseferi2241 Рік тому +1

    Hello, thanks from 42 Wolfsburg for the video and I also took some inspiration from your Github and made the printf work with bonuses. Do you have slack or discord to reach you out ?

    • @onaecO
      @onaecO  Рік тому +4

      Dear friend, u are crazy! printf bonus was a major pain 😂
      You should do a tutorial, there is not in yt 🔥
      You got me the idea and i just created a telegram group
      ~t.me/suspectedoceanO
      We can create a 42 gang ;)

  • @kwokhocheng-zf3fo
    @kwokhocheng-zf3fo 2 місяці тому

    good

  • @oneloveafrica8860
    @oneloveafrica8860 9 місяців тому

    tanks

  • @zoenagy9458
    @zoenagy9458 9 місяців тому

    i++ is more readable when it stands alone

  • @kellvynrocha3583
    @kellvynrocha3583 Рік тому +4

    I L O V E Y O U

    • @onaecO
      @onaecO  Рік тому +1

      😂😂😂

  • @oscarms6067
    @oscarms6067 Рік тому +2

    what if print_str receves NULL?

    • @onaecO
      @onaecO  Рік тому +2

      You mean smtg like my_printf(NULL);?
      Well this is a seg fault, indeed it's good practice always to check for NULL when dealing with pointers, in my simple implementation I just assumed no corner cases, like passing a NULL to a printf ^^
      Anyway good question 👊🏻🔥

    • @oscarms6067
      @oscarms6067 Рік тому

      Also if you pass a NULL as one of the variadic arguments to be processed as a string with print_string(). This function doesn't check it the pointer is NULL, before de dereferencing . Usually, in this case the constant string "(NULL)" is printed instead.

    • @onaecO
      @onaecO  Рік тому +3

      @@oscarms6067Yep! 100%, this is my mistake not properly handling NULLs and other corner cases, not pointing this out. I just wanted to show a boilerplate for a fully printf. I'll pin your comment for other ^^, ty for feedback!

  • @a3y3g1
    @a3y3g1 11 місяців тому +1

    As long as it norminettes

    • @onaecO
      @onaecO  11 місяців тому

      My friend, this is clearly not the real thing. is just a sample code

    • @a3y3g1
      @a3y3g1 11 місяців тому

      Haha just messing with you absolutely amazing video. Your understanding is immaculate. Looking forward to watch the full printf if you drop the video. Not sure if you did already. Keep up the good work mate@@onaecO

  • @swedishpsychopath8795
    @swedishpsychopath8795 10 місяців тому

    I'm sorry - but your printf function won't work.

    • @onaecO
      @onaecO  10 місяців тому +1

      I agree

    • @swedishpsychopath8795
      @swedishpsychopath8795 10 місяців тому

      @@onaecO I'm just trolling. I'm sure it will work just fine! But you didn't get mad and didn't start to scream so I must find someone else to troll :)

    • @onaecO
      @onaecO  10 місяців тому +2

      The "I don't give a f***k mode" must be always on when on internet 😂
      Btw Happy new year my dear troll ♥️

  • @JohnSmith-g7e9x
    @JohnSmith-g7e9x 10 місяців тому

    If you can't even using C write printf, it means you are a loser in programing language.

  • @taleuback6799
    @taleuback6799 8 місяців тому

    coucou ferme ta veste bisous

  • @PauloConstantino167
    @PauloConstantino167 11 місяців тому

    hi there, my dream is to become a printf function