The right way to define a C function with no arguments

preview_player
Показать описание
---
The right way to define a C function with no arguments. // Most new programmers (and many old programmers — me) define functions with no arguments with just an empty argument list. There's a better way, and this video is here to talk about it.

***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:

To Support the Channel:
+ like, subscribe, spread the word

Рекомендации по теме
Комментарии
Автор

Note that C23 changes this behaviour to match C++, that is, an empty set of parentheses means exactly the same thing as (void). To be exact, C23 removed old-style/K&R function definitions/declarations of which the empty parentheses are a special case. Of course, the advice given in the video is not outdated since C23 won't be adopted for a very long time (at the time this video came out, it is several months away from being published), and (void) is more explicit anyway.

EDIT: An empty set of parentheses actually meant two different things depending on whether it appeared in a function definition or a function declaration (that isn't a definition) before C23. You can find the relevant part of the standard in a reply. The idea is that the video is technically mistaken in saying that a function defined with empty parens takes zero or more arguments. It takes exactly zero but calling it with more causes UB. Compare that with using void which causes a compile-time error instead. On the other hand, using empty parens in a declaration actually means the function could take zero or more arguments.

Nickps
Автор

I think the other, and perhaps more general take-away is don't ignore warnings from a compiler or assembler. If you don't investigate there is a good chance it is something that will bite you down the line.

ColinMill
Автор

It's worth noting that in C++ the two function signatures are equivalent and mean "function with no parameters". Just one of those small differences between C and C++.

sledgex
Автор

Fifty years ago I had to make a career choice: Study law and become a lawyer, OR study computer science and become a programmer.
Luckily, I discovered C and I've been a programmer and a lawyer ever since.
Another insightful one, Jacob.

greg
Автор

By the way, in "The C programming language" (best programming textbook) they explain why this is the way it is. Function arguments were declared inside the body before the standard used in the book was adopted, so that's another relic of backwards compatibility

postmodernist
Автор

Thank you for making these videos. I was one of the people who tought it did not matter whether I used 'void' or not.

tommarcoen
Автор

Jacob, I'm pretty sure you NEVER thought this little topic would inspire so much excitement.

greg
Автор

This video actually helped me even though I wasn't getting any compiler warnings (and I have all warnings set to on). In my code I was setting a function pointer of type (void) to a function with () arguments.

Hadn't caused any bugs for me yet but happy to have picked up the problem before it became a problem!

CheCheDaWaff
Автор

The problem with your recommendation to use (void) is that it's just visual clutter; it does not help reading the code. A better recommendation is to use option '-std=gnu2x' (with both gcc and clang); then you can simplify all the (void) clutter to () AND get decent diagnostics at the same time.

Bruno_Haible
Автор

In languages as C ALWAYS be as explicit as possible. Basically every bug can be tracked to not being explicit. -pedantic is your best friend. Leaving a (void) can lead to memory-bugs that are really, really serious - just like doing a (void).

delqyrus
Автор

great tip! great to know this nuance... thanks!

dripcode
Автор

Forty years ago, I would have given a lot to have the answers to all the questions you address in your C videos. I had to guess, try, fail, try again and again. Documentation and compilers were not as good as today, and english is not my first language. Young generations are spoilt with such good content of yours, Jacob !!! Peace & Prosper 👍❤🖖

philpeko
Автор

All companies I have been have a rule that a function can’t have an empty arguments parameter list. It shall always have a void or the actual signature of the function

LordHog
Автор

You could also have showed if generated assembly/binary is different.

test-rjvl
Автор

How would you access those arguments then, in case you define your function as "accepting zero or more", when you don't have the ’void’, without also providing parameter definitions?

JohnDoe__
Автор

So does that mean all C functions can take varargs by default unless they have explicitly stated arguments, it's just that you can't access the arguments because you have no variable name to identify them by? Could you still access them with inline assembly assuming the compiler will still pass the arguments in to the function in the registers as usual?

jlewwis
Автор

Beating video, thanks, one interesting thing that is missing is to how can we leverage "no or more arguments" in their function?

sinamobasheri
Автор

Funny that the first time I saw code use that was about 2 weeks ago and here you are making a video about it. It seems like you always address the stuff that I'm wondering about... mhm...

rn-
Автор

If there's no arguments, but passing them still compiles, does that mean you could use vargs with it and just have them optional while (...) requires at least one?

KelvinShadewing
Автор

When using GCC, why not use to force an error? I guess using void is only good when you write your library and to avoid warning generation upon including your header files by end users, in case they have -Wstrict-prototypes, you should forcibly use VOID

grenadier