What are variadic functions (va_list) in C?

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ม.ค. 2025

ความคิดเห็น • 45

  • @peyop5262
    @peyop5262 ปีที่แล้ว +7

    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 ปีที่แล้ว +5

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

  • @КириллКопнев
    @КириллКопнев ปีที่แล้ว +4

    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

  • @AmitYadav-rp3ot
    @AmitYadav-rp3ot 3 หลายเดือนก่อน

    Thank you for the detailed explanation

  • @akpevwepraise4208
    @akpevwepraise4208 ปีที่แล้ว +1

    the most perfect explanation sofar

  • @yrds96
    @yrds96 ปีที่แล้ว

    Great video
    I didn't knew about the NULL technique

  • @AlluAdarsh
    @AlluAdarsh ปีที่แล้ว +1

    Informative. Thank you!

  • @189Blake
    @189Blake ปีที่แล้ว +3

    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  ปีที่แล้ว +2

      Thanks for the suggestion. I will look into it

  • @mustaphaboudrik5390
    @mustaphaboudrik5390 ปีที่แล้ว

    Thanks a lot for the Explanation

  • @riemann4151
    @riemann4151 ปีที่แล้ว

    best channel on C.

  • @ArdaX95
    @ArdaX95 4 หลายเดือนก่อน +1

    Excellent video! I was wondering based on your explanation that printf() is actually variadic function. If so, why are we not passing the number of arguments or NULL character in the argument list of print()? How is it working? Thanks

    • @DatBoi_TheGudBIAS
      @DatBoi_TheGudBIAS 3 หลายเดือนก่อน +2

      Printf parses the format string to determine how many arguments it will take.
      For example,
      printf("a == %d", 42);
      printf will read the string, see the %d, and pull an argument from the list of variadic arguments (wich is just the address of the first variadic argument)

    • @CodeVault
      @CodeVault  3 หลายเดือนก่อน +3

      That is the correct response. Thanks for the answer!

  • @vitalis3285
    @vitalis3285 5 หลายเดือนก่อน

    Great Video, very helpful! Ich kuess' dein Herz.

  • @fifaham
    @fifaham ปีที่แล้ว

    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.

  • @bjornnordquist1616
    @bjornnordquist1616 ปีที่แล้ว +3

    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

  • @julietagoldbaum332
    @julietagoldbaum332 ปีที่แล้ว

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

    • @CodeVault
      @CodeVault  ปีที่แล้ว

      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

  • @nayyerabbas7174
    @nayyerabbas7174 ปีที่แล้ว

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

    • @CodeVault
      @CodeVault  ปีที่แล้ว +1

      I will look into it

  • @Notthetylor
    @Notthetylor ปีที่แล้ว +1

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

  • @shenghongzhong
    @shenghongzhong 11 หลายเดือนก่อน

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

    • @CodeVault
      @CodeVault  11 หลายเดือนก่อน +1

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

  • @dimitrioskalfakis
    @dimitrioskalfakis ปีที่แล้ว

    tricky. good effort.

  • @dr-Jonas-Birch
    @dr-Jonas-Birch 7 หลายเดือนก่อน

    As you all know, my channel also does similar C language content (although focusing on advanced C features). My question is, do all your 15 000 subscribers come for the C content, or just a tiny fraction where the rest are here for webdev stuff. Any tips on how to reach 😊them? JB

    • @CodeVault
      @CodeVault  7 หลายเดือนก่อน +2

      99% of my viewers are from the C content. I had little to no success with the webdev videos (but I might give it a shot later on).
      Here's my advice:
      - Keep things simple and short. Understand that the viewers' time is precious for the viewer (nobody wants to waste their time watching a 40 mins video).
      - Most of my views are from searches on some very niche questions. Try researching about topics like these. For example, I noticed you have a Tor-related video that did well. Maybe more videos on that topic could be a good idea. Don't focus too much on generic topics (at least not in the beginning).
      - Explain basic concepts when needed. This goes a bit against the first point but I find that many people don't understand certain concepts because they are missing some of the more basic ones related to that topic. Quickly going over some of the basics is, a lot of the times, needed.
      - Rewatch your videos and find ways to improve them. For example: always speed up large portions where you're just typing code or waiting for some process to finish. You could also speed up your voice a little if you feel that is still legible (many people already do that using the TH-cam player anyway).
      Thanks for stopping by, wish you good luck!

  • @zyghom
    @zyghom ปีที่แล้ว +4

    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 ปีที่แล้ว +1

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

    • @CodeVault
      @CodeVault  ปีที่แล้ว +1

      @@belayoub6055 Exactly

    • @DatBoi_TheGudBIAS
      @DatBoi_TheGudBIAS 3 หลายเดือนก่อน

      ​@@belayoub6055indeed. It counts all the %, except when there's 2 % together. Dats how u print a %

  • @KH-en1yr
    @KH-en1yr หลายเดือนก่อน

    what if we don't know the count?

    • @CodeVault
      @CodeVault  หลายเดือนก่อน

      You can also pass a special value (usually NULL) at the end to signify the end of the arguments

  • @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

  • @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  ปีที่แล้ว +1

      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.

    • @DatBoi_TheGudBIAS
      @DatBoi_TheGudBIAS 3 หลายเดือนก่อน

      sum((int[]){1,2 3}) is valid syntax afaik
      Unless this only works with strings

  • @ryanboudwin541
    @ryanboudwin541 ปีที่แล้ว

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

    • @CodeVault
      @CodeVault  ปีที่แล้ว

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

  • @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