Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I declare multiple functions with comma?

Tags:

c++

Can we use , operator to declare multiple functions once ? This code compiles on gcc:

void f(), g();
void f() {}
void g() {}

int main() {
    f();
}

Is it standard or a particularity of the compiler ?

like image 696
rafoo Avatar asked Apr 20 '21 15:04

rafoo


People also ask

Can we declare a function multiple times?

declaration can be done any time.

Can a variable have multiple declaration?

Every declaration should be for a single variable, on its own line, with an explanatory comment about the role of the variable. Declaring multiple variables in a single declaration can cause confusion regarding the types of the variables and their initial values.

Can a function be declared multiple times in C?

A variable or function can be declared any number of times, but it can be defined only once.

What does comma operator do?

The comma operator ( , ) evaluates each of its operands (from left to right) and returns the value of the last operand. This lets you create a compound expression in which multiple expressions are evaluated, with the compound expression's final value being the value of the rightmost of its member expressions.


1 Answers

It's standard. Look at all the things you can declare:

int a, *pa, f(int), *g(int);

This is a single declaration, with a single type specifier int and four declarators. Note that the two variable declarations there have different types, despite having the same type specifier. Same deal with the two function declarations. The type specifier only covers the innermost nugget of the type; the individual declarators wrap it in things like pointers or function-ness.

The function definition int f() { ... } is unique and unusual. While it serves to both declare and define a function, and involves some of the same syntactic constructs as declarations, it is syntactically separate from a declaration. Since a declaration is the thing that gets to have a list of declarators, the function definition syntax doesn't inherit that behavior. So you can declare multiple functions in a single declaration, but a function definition is not a declaration (despite the fact that it declares a function!) so you can only do one at a time.

Editorializing a little: This facet of the syntax, with type information spread between the type specifier and the declarator, is one of the core awfulnesses of C and C++. It dates back to the earliest days of C, when the focus of the language meant that most variables were ints and the type was actually the most optional part of a declaration. Most of us have experienced this in the form of int* a, b only making one pointer, but things like the parentheses that crop up when declaring function pointers, and the overall spiral-ness of complex type definitions, also spring from that essential, never-corrected mistake from the early 1970s.

like image 68
Sneftel Avatar answered Oct 01 '22 00:10

Sneftel