What are variadic functions (va_list) in C?

Поделиться
HTML-код
  • Опубликовано: 16 май 2024
  • Source code can be found here:
    code-vault.net/lesson/cc1b934...
    ===== Support us through our store =====
    code-vault.net/shop
    ===== Check out our website =====
    code-vault.net
    ===== Check out our Discord server =====
    discord.code-vault.net

Комментарии • 35

  • @kookts8491
    @kookts8491 7 месяцев назад +3

    The last one in the playlist🎉🥳 Thank you so much!!!

  • @peyop5262
    @peyop5262 Год назад +6

    I've used it to write a generic "err_exit" function, which basically takes a return error int as first parameter, then as much strings as needed. For example 'err_exit(1, "", SDL_GetError(), NULL);'
    It makes my code cleaner (exit program in a single line and customized error message) and it's really helpful when a project gets big.
    As you've mentioned, here the NULL last arg isn't a problem since variadic args are strings. Very handy C functionality if you treat it carefully.
    Thank you for your amazing videos

  • @anilthapa6290
    @anilthapa6290 9 месяцев назад +4

    Hi very good explanation of the topic. Learned so much from your channel.

  • @mustaphaboudrik5390
    @mustaphaboudrik5390 8 месяцев назад

    Thanks a lot for the Explanation

  • @AlluAdarsh
    @AlluAdarsh Год назад +1

    Informative. Thank you!

  • @riemann4151
    @riemann4151 8 месяцев назад

    best channel on C.

  • @yrds96
    @yrds96 6 месяцев назад

    Great video
    I didn't knew about the NULL technique

  • @user-mo6vz5mg3t
    @user-mo6vz5mg3t 6 месяцев назад +2

    lol the adds started exactly at the moment when he tried to compile with 10 iterations instead of 4 at 9:24
    This was an undefined behaviour indeed

  • @akpevwepraise4208
    @akpevwepraise4208 7 месяцев назад

    the most perfect explanation sofar

  • @189Blake
    @189Blake 9 месяцев назад +2

    The C23 new syntax seems very useful, could you do a blog post or a follow up explaining that? Just a video idea

    • @CodeVault
      @CodeVault  9 месяцев назад +1

      Thanks for the suggestion. I will look into it

  • @julietagoldbaum332
    @julietagoldbaum332 11 месяцев назад

    Excelent video!!. congrats from Argentina!!. What is it argument promotion in variadic functions?

    • @CodeVault
      @CodeVault  10 месяцев назад

      That is quite a complex topic which I will cover in a video. The basic idea is that parameters for variadic functions are treated as certain data types by default

  • @dimitrioskalfakis
    @dimitrioskalfakis Год назад

    tricky. good effort.

  • @bjornnordquist1616
    @bjornnordquist1616 Год назад +2

    Hi! Interesting video. You have been asking for content ideas in the past, so maybe there is something for you in here: there appears to be almost no content about testing in C. Or maybe it is just really hard to dig up for me. I barely know where to start, especially since all content send to focus on C++ and Visual Studio appears to have a dislike for C in general. Also, maybe how far can we emulate object oriented programming or functional programming in C? And in the same direction procedural programing with C. Either way I'll keep watching these C videos until I move on. Thanks a lot!

    • @CodeVault
      @CodeVault  Год назад +2

      Thanks for the suggestions. I have dabbled in automated testing but not in C specifically (I'll have to do some research). And regarding emulating OOP in C... it was always in my mind since it's a great way of learning the basics of OOP and how it's a paradigm that can be used in almost any language

  • @nayyerabbas7174
    @nayyerabbas7174 Год назад

    Very informative. Can you make a video on opaque pointer in c.

  • @Notthetylor
    @Notthetylor Год назад +1

    I'd like to have a mentor like u!!

  • @fifaham
    @fifaham 5 месяцев назад

    Having 0 in the list while using the NULL method could be troublesome for embedded design - the hardware may not behave well or the robot arm may grab the wrong object.

  • @vilijanac
    @vilijanac Год назад +1

    Is it possible to pass an int array instead sum({1,2,3}), where you have int sum(int args[]){...int count = sizeof(args);...}
    I forgot about this.

    • @CodeVault
      @CodeVault  Год назад

      In other languages it might be possible. In C you have to first declare and initialize an array before you pass it to the function

    • @vilijanac
      @vilijanac Год назад

      @@CodeVault ok, thx, reason also for va_list implementation and passing count. It is better then testing for NULL.

  • @XAENDER196
    @XAENDER196 Год назад

    Hi! Is it possible to pass char array to parameters? i mean, can they be as variadic arguments?

    • @CodeVault
      @CodeVault  Год назад +1

      You can pass any type of variables you want to va_list

  • @ryanboudwin541
    @ryanboudwin541 6 месяцев назад

    Why not pass argc from your main into the function instead of a separate int count?

    • @CodeVault
      @CodeVault  5 месяцев назад

      The argc value is the number of parameters passed to the executable

  • @shenghongzhong
    @shenghongzhong 4 месяца назад

    Did we know which video he referred to was about not using the null terminator and the number of elements?

    • @CodeVault
      @CodeVault  4 месяца назад

      There's no video about that. I recommended this as a homework, but I can help with that if you need it

  • @zyghom
    @zyghom Год назад +3

    nice, so how come printf does not need count? ok, that is the homework ;-)

    • @CodeVault
      @CodeVault  Год назад +5

      Exactly. Hint: It might have to do with those pesky %d and %s you need to specify to printf

    • @belayoub6055
      @belayoub6055 9 месяцев назад +1

      @@CodeVault well you need to count how many % that specify the format no ?

    • @CodeVault
      @CodeVault  9 месяцев назад +1

      @@belayoub6055 Exactly

  • @rotemlv
    @rotemlv Год назад

    I wouldn't recommend using the last trick you showed, as the code will return bad results on some inputs (0s spell trouble when comparing to NULL).
    Try this main on your code:
    int main() {
    printf("A list of stuff: %d
    ", sum(1,2,3,4,5,6,0,999, NULL));
    return 0;
    }
    You will get the expected (by me) 21, and ignore the 999.
    Yeah, one might say this is an edge case, but I wouldn't.

    • @CodeVault
      @CodeVault  Год назад +2

      I fully agree. That's what I warned people about in the video. It works well if you pass strings (or other pointer types) for example