C programming: Why is this not flagged as a error by the compiler? - Page 1
Forum section: electronics / general

author: AndersJ
author_rank: Frequent Contributor
timestamp: August 14, 2016, 03:42:23 pm
content: I have a function: void func(void)It is called like this: func();It works fine.But,When i mistakenly called it like this: void func(); // note the "void" that should not be there!It compiles without errors, but the line is ignored, as if it was commented out.Is this correct compiler behaviour?If so, how/where is this usage of "void" defined?AndersJ

author: Kalvin
author_rank: Super Contributor
timestamp: August 14, 2016, 03:48:13 pm
content: It is correct. It is just a declaration for the function prototype.

author: Signal32
author_rank: Frequent Contributor
timestamp: August 14, 2016, 03:48:21 pm
content: void func(){ } // Function definitionvoid func(); //Function declarationfunc(); // Function callSo when you're putting the void in front you're declaring a function.If you try this:void func2();func2();You'll get a linker error that func2 cannot be found.Look up function declarations if you want to see where the "void func();" behavior is defined.

author: rstofer
author_rank: Super Contributor
timestamp: August 14, 2016, 03:50:01 pm
content: So you just included a function prototype. Without the surrounding context, it's hard to say why it was or was not accepted but it is a perfectly legal statement.Microsoft C accepts the following gibberish:Code:void f(void);void f(void){}int main(){void f();f();return 0;}

author: magetoo
author_rank: Frequent Contributor
timestamp: August 14, 2016, 03:55:00 pm
content: func(); is a function call.void func(); is a function declaration, which just says "there exists a function that takes this input and gives this output".void func() { ... code ... } is a function definition.What you've done is declare the function twice, which in this case is a no-operation. If you instead had declared it differently the second time, for instance writing "int func();" instead of "void func();" then you'd get an error.

author: AndersJ
author_rank: Frequent Contributor
timestamp: August 14, 2016, 04:14:24 pm
content: Is a function declaration allowed INSIDE a function?I find that confusing.When might that be useful?

author: ataradov
author_rank: Super Contributor
timestamp: August 14, 2016, 04:56:51 pm
content: Quote from: AndersJ on August 14, 2016, 04:14:24 pmYes, it is allowed. This is a side effect of function declaration and variable declarations being the same thing from a syntax point of view.Quote from: AndersJ on August 14, 2016, 04:14:24 pmThen C is not for youQuote from: AndersJ on August 14, 2016, 04:14:24 pmProbably never in a real world, but that's no reason to create artificial exceptions.Edit: And a typical example of a very confusing behavior in C: "array" is the same as "5". And I actually seen this used in real programs. Not in this kind of simplistic case, of course.

author: helius
author_rank: Super Contributor
timestamp: August 14, 2016, 05:05:47 pm
content: All declaration types are allowed inside functions (as well as inside other blocks, likeiforswitchstatements). There is nothing special about function declarations. The declaration of an identifier inside a block shadows the declaration it has outside that block. So this is very far from being a no-op! The declarationvoid func(void);overrides the meaning offuncwithin that block. If it were declared at top level to beint *(*func) (struct timespec, char*) = {};, then only the inner declaration would be used within that block: the binding offuncto an array would be shadowed and not used.The compiler will issue an error when you write two declarations for the same identifier in thesameblock. There is no rule for which one should be used, so this is a programming error.C inherits this feature, called "block structure", from ALGOL 68. It is a feature of most widely-used languages.

author: MarkF
author_rank: Super Contributor
timestamp: August 14, 2016, 08:13:00 pm
content: Quote from: AndersJ on August 14, 2016, 04:14:24 pmIt's useful when you declare the function prototype at the beginning of the file. And then use that function before actually writing the code for it.i.e. You use the function in main() but the code for the function doesn't appear until after the main() function. Without the prototype before main(), you will get a compile error.Code:void fun(void);main(void){. . .fun();. . .}void fun(void){. . .}

author: rstofer
author_rank: Super Contributor
timestamp: August 14, 2016, 09:10:11 pm
content: Quote from: AndersJ on August 14, 2016, 04:14:24 pmIn C, a declaration is allowed inside a function but a nested function itself is not. In languages like Algol, Pascal, Modula, Oberon ... functions may be nested inside other functions and their scope is only within the surrounding function. In these other languages, nested functions are hidden from the outside world even within the same file whereas in C, the function must be declared 'static' to hide it from other translation units (files) but it is still global within the file which contains it.

author: SL4P
author_rank: Super Contributor
timestamp: August 14, 2016, 10:11:52 pm
content: I prefer the term function 'prototype' rather than declaration, as it differentiates from variable declarations.Not sure which is the proper terminology...

author: NexusKoolaid
author_rank: Contributor
timestamp: August 15, 2016, 04:33:13 pm
content: Quote from: SL4P on August 14, 2016, 10:11:52 pmFunction declarations and definitions both have prototypes (signature, label and return type), but one has no code associated with it. That being the case I believe declaration and definition are the proper terms, with prototype being an implied passenger. In reality though, a lot of people tend to use "declaration", "prototype" and "forward declaration" interchangeably.Hey, look at that... first post!