How to create functions with a variable number of arguments using stdarg.h | C Programming Tutorial

  Рет қаралды 46,626

Portfolio Courses

Portfolio Courses

2 жыл бұрын

An overview of how to create functions with a variable number or arguments with stdarg.h in C. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 141
@benjminrichie
@benjminrichie 9 ай бұрын
i havent even watched up to 5minutes of the video yet but i am already get the concepts. You are really amazing in your teaching method
@PortfolioCourses
@PortfolioCourses 9 ай бұрын
I’m so glad you enjoy the teaching method! :-)
@A.E.B.
@A.E.B. Жыл бұрын
Just want to say thanks for all your very clear and informative tutorials. I stumbled upon your channel for a specific C question and now am binge watching all your C videos, they're awesome!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Wow thank you so much for the super thanks Arne, and I’m so glad to hear you’ve found the channel and are enjoying the content! :-) If you come across a topic that the channel is not covering that you think it should, please feel free to let me know with a comment.
@mohammedosman6735
@mohammedosman6735 Жыл бұрын
Thank you very much, very smooth and clear explanation... Definitely one of the best C tutorials on KZfaq.🥇🏆
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're very welcome Mohammed! :-) I'm glad to hear that you found the explanation smooth and clear.
@nkurikiyimanaaimable9004
@nkurikiyimanaaimable9004 Жыл бұрын
i agree
@atarixle
@atarixle 5 ай бұрын
Hell this opens my eyes. I never noticed that printf's placeholders not only give the output-format, but also give the number of arguments to the functions.
@bilaledu9206
@bilaledu9206 2 жыл бұрын
Beautiful and clear video! Step by step and understandable. Your videos help me make up for what's missing and build on what I've learned in class. Thanks a lot!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome! :-D Glad to hear you enjoy the videos.
@isaac-victorprovidence9047
@isaac-victorprovidence9047 Жыл бұрын
I have a team project tomorrow, where we have to create our own print function to take care of the different function specifiers. This video just helped🥺. My go-to channel for all things coding 😊
@PortfolioCourses
@PortfolioCourses 11 ай бұрын
That's awesome, I'm glad to hear this video helped! :-)
@ሰውመሆን
@ሰውመሆን Жыл бұрын
thank you so much. we are all grateful that you are providing all these great lectures for free🙏🙏🙏🙏
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're very welcome Robel! :-)
@ijeomachimdimba2757
@ijeomachimdimba2757 2 жыл бұрын
very easy to flow with your teaching, love this thank you
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you very much Ijeoma, I'm glad to hear you enjoyed the video! :-D
@akosyt4540
@akosyt4540 Жыл бұрын
thank youu so much for this video, very clear explanation and straight to the point . I'm using c++ and learn alot from this tutorial!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That’s awesome that you learned a lot from this tutorial, and you’re welcome! :-)
@gammyhorse
@gammyhorse Жыл бұрын
One of my favorite videos, this one!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you! :-D
@a.v7998
@a.v7998 4 ай бұрын
Fantastic Video!
@Harrun
@Harrun 5 ай бұрын
Thank you, this was very helpful.
@robssonpeter
@robssonpeter Жыл бұрын
My guy I cant thank you enough, this has technically saved my life
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re very welcome Peter, I’m so glad to hear this video was helpful for you! :-)
@anitamatthew4409
@anitamatthew4409 2 жыл бұрын
Found this very helpful, thank you👍🏾
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I'm very glad to hear that Anita, and you're welcome! :-D
@user-fe8qf4vk5i
@user-fe8qf4vk5i 11 ай бұрын
these videos are really helpful for a beginner like me, thanks❤
@PortfolioCourses
@PortfolioCourses 11 ай бұрын
You're welcome, I'm glad to hear these videos are helping you out! :-)
@safeyamohamed3854
@safeyamohamed3854 Жыл бұрын
thank you so much I searched on it for a long time and didn't understand it but now I understand it I grateful for you
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're very welcome Safeya, I'm glad the video was helpful for you! :-)
@safeyamohamed3854
@safeyamohamed3854 Жыл бұрын
@@PortfolioCourses 😍
@johneric2720
@johneric2720 Жыл бұрын
Your videos are great, love them ❤
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Awesome, I'm so glad to hear you're enjoying the videos! :-)
@geraldinkaba5329
@geraldinkaba5329 11 ай бұрын
Thank you very much. I was lost about it. You helped me a lot. I like and subscribe!!!
@PortfolioCourses
@PortfolioCourses 11 ай бұрын
You're welcome, and thank you for subscribing, welcome aboard! :-)
@kitsunevuon
@kitsunevuon 9 ай бұрын
Boi, you just saved my cpool's first project.
@PortfolioCourses
@PortfolioCourses 9 ай бұрын
That’s great to hear! :-)
@Shivottmam
@Shivottmam 4 ай бұрын
Wow 🤩 this is awesome man. Nobody has ever taught me in my academics about creating a fun in C with var num of args. In fact came to know about the H file only through this video and now I because of you know the game. Much Thanks 🙏
@peterolubowale4185
@peterolubowale4185 11 ай бұрын
Wow nice video. Now we can even attempt to use our own made format specifier, and can now determine the number of variable arguments passed using the count of appearances of '%' instead of using strlen().
@PortfolioCourses
@PortfolioCourses 11 ай бұрын
I'm glad you enjoyed the video! And yes, you could use it to implement functions like printf() with format specifiers and a variable number of arguments. :-)
@michaelmusa6489
@michaelmusa6489 9 ай бұрын
Thank you so much you are my go to guy whenever I don't understand, your videos are very helpful. I created my own printf function but im struggling to print strings and characters. Please may you assist me if possible 🙏
@AmirMovafagh-zn9lc
@AmirMovafagh-zn9lc Жыл бұрын
thank you, extraordinary explanation.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome Amir, I’m glad you enjoyed it! :-)
@hangareducation
@hangareducation 2 жыл бұрын
What an excellent explanation
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you! :-)
@nikhilchitroda9886
@nikhilchitroda9886 2 жыл бұрын
To the point, thanks bro 👍
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome Nikhil! 😀
@khumbizechimsale
@khumbizechimsale Жыл бұрын
I love your videos
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm really glad to hear you enjoy the videos Khumbize, and thank you very much for the positive feedback! :-)
@auntyemman
@auntyemman 2 жыл бұрын
This is without ambiguity, so smooth
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you very much Adepoju! :-D
@Chigo-nr8jg
@Chigo-nr8jg Жыл бұрын
Think i'll need some time to get this to sink in. Thanks!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! :-) And yes it's definitely one of those things that takes time to sink in.
@jess-inspired
@jess-inspired 2 жыл бұрын
Totally Awesome..
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you! :-)
@geo-mj4gb
@geo-mj4gb 8 ай бұрын
Fantastic video but I was almost screaming on the typo you made on stdarg.h. But again, great video.
@nwabuezefranklin7676
@nwabuezefranklin7676 2 жыл бұрын
still the best tutorial on this concept in KZfaq as whole; KUDOS 💪 Also, i have a question, While practicing this concept I discovered I could not use more than 1 "else if" statement in writing the function. what could cause this?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you Nwabueze! :-)
@cryptotechcoder
@cryptotechcoder Жыл бұрын
Just 2 minutes and I'm into it...
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I’m glad you enjoyed it! :-)
@Banikan
@Banikan 9 ай бұрын
is it possible to not specify the number of args?
@ahmetkarakartal9563
@ahmetkarakartal9563 Жыл бұрын
thank you so much
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're very welcome Ahmet! :-)
@saklinsk4618
@saklinsk4618 Жыл бұрын
Thank you so much.😊 . .. ...
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re very welcome Saklin! :-)
@dimitrioskalfakis
@dimitrioskalfakis Жыл бұрын
nice presentation.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you Dimitrios, I'm glad you enjoyed it! :-)
@mastermax7777
@mastermax7777 9 ай бұрын
5:40 what happens if you dont put va_end(args)... ? Does it not compile? or leak memory
@PortfolioCourses
@PortfolioCourses 8 ай бұрын
I didn’t know the answer to be honest but this stack overflow answer looks correct: stackoverflow.com/a/587139
@sircomplex8629
@sircomplex8629 Жыл бұрын
I really like your voice and step by step teaching.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you! I'm glad to hear that you enjoy the teaching style. :-)
@massimorusso3894
@massimorusso3894 Жыл бұрын
Sono video eccezionali sul Linguaggio C. Un saluto dal'Italia.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Grazie, Massimo! :-)
@evans8245
@evans8245 Жыл бұрын
I salute you from the 42 School
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you Evans, I salute you back from Canada! :-)
@kaigoprod3238
@kaigoprod3238 2 жыл бұрын
let's recode printf using the original printf function to print the result lmao
@allnew301
@allnew301 2 жыл бұрын
here ive recoded printf but without flags lol hope you find it intersting.
@zettai8087
@zettai8087 Жыл бұрын
@@allnew301 did you make your own printf function? Send a link please
@PHXKHEEDGAMING
@PHXKHEEDGAMING Жыл бұрын
Lmaoooo. This guy has completely missed the point of the entire video. Trynna be funny.
@amerabuduhair2237
@amerabuduhair2237 Жыл бұрын
What is the purpose of for loop when i change from 0 to 3, you didn’t use i, simultaneously va_arg takes the next number while it’s independent of (i)
@CheCheDaWaff
@CheCheDaWaff Жыл бұрын
The point is to repeat the action the number of times specified by num_args. Not sure how you could do this without a for loop?
@2XS
@2XS 5 ай бұрын
What happened to that good old "int main(int argc, char* argv[])"? Did "..." replace "char* argv[]" or are both still valid?
@SlideRSB
@SlideRSB 5 ай бұрын
argc and argv are different from the stdarg library. You only get to use argc and argv with main(), not with other functions you define.
@YoussefMorad1
@YoussefMorad1 Жыл бұрын
Great !
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I’m glad you enjoyed it Youssef! :-)
@sadelax
@sadelax Жыл бұрын
el mejor vídeo que explica las funciones variádicas. gracias!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Gracias! 🙂
@oscardruke5106
@oscardruke5106 Жыл бұрын
Dot g, translated to Spanish, it's "punto g" which happens to be how we named the g spot, so as a bilingual my mind went "that's what she said" automatically after "that's not a dot g". Thx for the hard laugh. And also for the tutorial btw
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Hahaha oh that's funny Oscar, and you're welcome! :-)
@jackalturk6491
@jackalturk6491 Ай бұрын
u used in first example an int value as second argument of start function innsevond example a pointer and both have been worked, i couldnt got it even with four repeats :(
@danieltimileyin3773
@danieltimileyin3773 Жыл бұрын
I have been following most of your videos and i am a proud student. So far this has been the best tutorial for me on youtube.Then about creating a printf from scratch, how do i go about this?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you're enjoying the videos Daniel! :-) I don't have a video covering how to make printf() yet. Making a full printf() implementation would actually be a lot of work and get pretty complicated. Online there are tutorials that go over 'smaller' less functional versions of printf() implementations, like this one: www.firmcodes.com/write-printf-function-c/. One day I'd like to do a "build your own printf()" video but it may need to be limited in scope otherwise it may be too long.
@danieltimileyin3773
@danieltimileyin3773 Жыл бұрын
@@PortfolioCourses thanks for the swift reply
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! :-)
@0xRAND0M
@0xRAND0M Жыл бұрын
@danieltimileyin3773 Alx maybe?
@danieltimileyin3773
@danieltimileyin3773 Жыл бұрын
@@0xRAND0M yeah
@giuseppefontana5471
@giuseppefontana5471 9 ай бұрын
how to handle struct type with va_args? For example: struct mytype{ char *str; int length; }; and a function like this: //should compare all struct mytype input and return true, if all inputs have the same size and attribute value, and false if not bool equals_mytype(struct mytype arg_i, ...);
@Yougottacryforthis
@Yougottacryforthis 2 ай бұрын
Did you figure it out?
@fifaham
@fifaham 11 ай бұрын
This is an idea only: (placeholders, A1, A2, A3, A4...........A1000) I thought this could be one of implementations for voice synthesis, or music ! Where A1, A2 and so on could also take bit wise variables such as amplitude, frequency, filtering, modulation and duration from placeholders: A1 = amplitutde, frequency, filtering, modulation and duration (5 bytes for example) > so A1 could be convoluted with 0x7 + 0xA + 0x3 + 0x01 + 0xC (5 HEX compressed values) and with a rapidly progressing stream of variables and placeholders a decomposed speech can be re-constructed. It is only an idea that could work, or could not work. If there is any designer is in this channel then what do you think of this idea? Could it work? Of course we must have a very high processing speed processor and a very high memory desnsity for this to work properly. Any idea from this audiance?
@the_mellobean
@the_mellobean Жыл бұрын
Can I type cast the variable arguments?
@PortfolioCourses
@PortfolioCourses 11 ай бұрын
I don't see why not. :-)
@eduardzemlyanski1719
@eduardzemlyanski1719 8 ай бұрын
can't process char this way for some reason
@nathanassefa8787
@nathanassefa8787 Жыл бұрын
Can you please make a video that designs a complete printf function that have its own formatting methond, and it does not use the existed printf function to print datas?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I've added this to my list of video ideas Nathan. :-) This would not be a very easy function to build though I don't think, printf() has many options.
@nathanassefa8787
@nathanassefa8787 Жыл бұрын
@@PortfolioCourses you are right sir that what you already mentioned here is extraordinary. I just asked you to implement printf only using stdiarg since I facing a problem with that. I think many people are struggling too.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@nathanassefa8787 Interesting, I've heard of teachers assigning this problem to students before. It's something I'm going to look into one day, I agree it could make a good video. 🙂
@nathanassefa8787
@nathanassefa8787 Жыл бұрын
@@PortfolioCourses your way of teaching is really helpful even for beginners. I am looking forward to seeing your complete printf function soon. Thank you sir.
@user-zq7sx9hh6b
@user-zq7sx9hh6b Ай бұрын
So cool, we are try into education project with implementation sprintf function, this video really open my eyes, thanks a lot!
@ghadyyehya9367
@ghadyyehya9367 8 ай бұрын
thx
@PortfolioCourses
@PortfolioCourses 8 ай бұрын
You’re welcome! :-)
@amrgaber4400
@amrgaber4400 11 ай бұрын
perfecto
@PortfolioCourses
@PortfolioCourses 11 ай бұрын
I'm glad you enjoyed it! :-)
@PHXKHEEDGAMING
@PHXKHEEDGAMING Жыл бұрын
Currently trying to get va_arg to collect strings parameters. How do we collect string parameters and print? This video is all about collecting numbers and no strings. c gets tricky when it has to deal with strings, I’m tired 😪😪
@PHXKHEEDGAMING
@PHXKHEEDGAMING Жыл бұрын
Is there also a tutorial you mad in pioneers and use cases with many examples? Punters had been messing with me. I’m so frustrated
@PHXKHEEDGAMING
@PHXKHEEDGAMING Жыл бұрын
Most of the course I’m taking rn uses pointers alll the time 😪.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
So instead of this: int x = va_arg(args, int); You would want this: char *x = va_args(args, char *) if you were passing in some number of strings. :-) When we pass strings to functions what we are really passing is a pointer to a char, the first char in the string.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Yes I do have a tutorial on pointers: kzfaq.info/get/bejne/aK10nLt4mrjKfnk.html. :-) You might find these interesting too: kzfaq.info/get/bejne/pctilb18z8-7ops.html. kzfaq.info/get/bejne/iJahfLyI1J7Nl6c.html
@fass.576
@fass.576 Жыл бұрын
just wondering why u didn't call the video title Variadic function or even mention the term Variadic , did i get it confused or ......? i loved the video
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I tried naming the video based on what people search for. I think I should add the term "variadic function" to the description though to help people find it. :-)
@its_code
@its_code Жыл бұрын
❤❤❤❤❤
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you! ❤️
@jonforhan9196
@jonforhan9196 Жыл бұрын
Can you use dark mode i wanna watch this at night without burning my retinas
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm hoping one day that KZfaq adds a feature that can somehow flip videos from a light mode to a dark mode. :-) The research that I did into this came up that it largely comes down to user preference, which has a lot to do with lighting conditions. In low light conditions dark mode can be preferable for many people, where as light mode is often preferred in higher light conditions and is often higher contrast which can be good for users watching a smaller KZfaq video in the corner of a screen while coding along themselves. Unfortunately for now its one of those things where I can't please everybody, but it's something I gave a lot of thought about. :-)
@Antagon666
@Antagon666 2 жыл бұрын
That unnecessary if statement in for loop. Why wouldn't you just put max=first arg before the for loop ?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You could do it that way too! There would really need to be a first argument though if you do it that way.
@warplanner8852
@warplanner8852 3 ай бұрын
Known or referred to as "variadic arguments".
@activelow9297
@activelow9297 6 ай бұрын
Kind of a crummy way of doing it... thank the Lord for variadic templates and variadic functions!
@PHXKHEEDGAMING
@PHXKHEEDGAMING Жыл бұрын
Can you just make another characters and strings instead of numbers 😪
@PortfolioCourses
@PortfolioCourses Жыл бұрын
It should look like this: void print_all(int num_args, ...) { char *next_string; va_list ap; int i; va_start(ap, num_args); for(i = 0; i < num_args; i++) { next_string = va_arg(ap, char *); printf("%s ", next_string); } va_end(ap); } and then: print_all(4, "test 1", "test 2", "test 3", "test 4");
@PHXKHEEDGAMING
@PHXKHEEDGAMING Жыл бұрын
This works for strings but not for single char. Error was “illegal program” or something illegal, wasn’t even the usual segmentation fault. And besides, I have to use putchar() and not printf() since I’m kind of like rebuilding/cloning the printf function
@PortfolioCourses
@PortfolioCourses Жыл бұрын
For a single char you would have "char" instead of "char *" as the argument to va_arg(). And you would store the value that's returned into a char variable, and then output it with putchar().
@PHXKHEEDGAMING
@PHXKHEEDGAMING Жыл бұрын
@@PortfolioCourses thanks man
@pound9799
@pound9799 10 ай бұрын
it doesn't make any sense 😭
@sossesam
@sossesam Жыл бұрын
😂😂alx people full this comment section kai.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I don't know what this means. :-)
@sossesam
@sossesam Жыл бұрын
@@PortfolioCourses Sorry, i'm nigerian. came across your video while trying to solve a task given to me in a school am enrolled in. A friend recommended your channel. I noticed a familliar name from the program. i was trying to speak pigdin english
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@sossesam Oh I understand now haha, I'm glad that your friend recommended the channel. 🙂 Hello from Canada!
@PHXKHEEDGAMING
@PHXKHEEDGAMING Жыл бұрын
Lmao this is surprisingly very precise and clear just a little to fast for my brain 🥲. I guess I’ll just keep implementing the rewind button.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thanks! :-)
@PHXKHEEDGAMING
@PHXKHEEDGAMING Жыл бұрын
@@PortfolioCourses I just tried your logic to apply in a function of summing up the parameters passed. To return 0 if number of parameters is 0 Else go ahead and perform the getting and summing to the parameters. It works fine for when the situation of 0 and the situation of correct number of parameters and inputs. n = number of parameters But now it’s overdoing I just tested the code by inputing n = 8 while passing only 4 other parameters. It’s still worked and started producing very weird numbers. How do I tell it to stop or not run if the n is not equal or to the number of parameters passed. I hope you understand me 🥺
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Hmm, that's weird. argc should be set to the number of parameters, however many. You could "loop through" argv and output each parameter as a string value to help better understand what parameters you are getting. Maybe you're not getting the parameters you think you are, that may help you to debug what's going on. :-)
pow() function | C Programming Tutorial
2:37
Portfolio Courses
Рет қаралды 14 М.
Function Pointers | C Programming Tutorial
18:31
Portfolio Courses
Рет қаралды 57 М.
Heartwarming Unity at School Event #shorts
00:19
Fabiosa Stories
Рет қаралды 18 МЛН
ОСКАР vs БАДАБУМЧИК БОЙ!  УВЕЗЛИ на СКОРОЙ!
13:45
Бадабумчик
Рет қаралды 6 МЛН
Nastya and SeanDoesMagic
00:16
Nastya
Рет қаралды 13 МЛН
What are variadic functions (va_list) in C?
13:49
CodeVault
Рет қаралды 20 М.
Dynamic Memory Allocation | C Programming Tutorial
31:51
Portfolio Courses
Рет қаралды 79 М.
struct Basics | C Programming Tutorial
24:44
Portfolio Courses
Рет қаралды 132 М.
why do void* pointers even exist?
8:17
Low Level Learning
Рет қаралды 338 М.
How to write C functions with variable argument lists.
4:12
Jacob Sorber
Рет қаралды 64 М.
File Access Basics | C Programming Tutorial
24:05
Portfolio Courses
Рет қаралды 88 М.
Gym belt !! 😂😂  @kauermtt
0:10
Tibo InShape
Рет қаралды 10 МЛН
joga água e pula #funny #funnyvideo #shorts
0:17
Mundo de Alícia e Ana Clara
Рет қаралды 14 МЛН
Funny Escalator #katebrush #shorts
0:11
Kate Brush
Рет қаралды 2,9 МЛН
What did I eat? 🤪😂 LeoNata Best #shorts
0:19
LeoNata Best
Рет қаралды 9 МЛН