What are variadic functions (va_list) in C?

preview_player
Показать описание
Source code can be found here:

===== Support us through our store =====

===== Check out our website =====

===== Check out our Discord server =====
Рекомендации по теме
Комментарии
Автор

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, "<func_name>", 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

peyop
Автор

Thank you for the detailed explanation

AmitYadav-rpot
Автор

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

anilthapa
Автор

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

КириллКопнев
Автор

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

Blake
Автор

Great video
I didn't knew about the NULL technique

yrds
Автор

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

dr-Jonas-Birch
Автор

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!

bjornnordquist
Автор

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

ArdaX
Автор

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.

fifaham
Автор

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

julietagoldbaum
Автор

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

vitalis
Автор

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

shenghongzhong
Автор

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

Notthetylor
Автор

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

nayyerabbas
Автор

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

zyghom
Автор

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

XAENDER
Автор

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.

vilijanac
Автор

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

ryanboudwin
Автор

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\n", 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.

rotemlv