ngn
ngn

Reputation: 7902

C function type without a typedef

Suppose we have many C functions with the same signature and return type:

R f(X x,Y y){...}
R g(X x,Y y){...}
R h(X x,Y y){...}

where X and Y are the types of the arguments and R is the type of the result.

We can declare those functions, for instance in a header file or as forward declarations, like this:

R f(X,Y);R g(X,Y);R h(X,Y);

or more concisely:

R f(X,Y),g(X,Y),h(X,Y);

or, avoiding most repetition:

typedef R F(X,Y);
F f,g,h;

Can that be done in a single statement, without a separate typedef?

Upvotes: 1

Views: 138

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

I have not checked yet but maybe in C23 there is possible to write

typeof( R( X, Y ) ) f, g, h;

Upvotes: 1

Wildptr
Wildptr

Reputation: 39

head.h

#pragma once

int f(int, int), g(int, int), h(int, int);

head.c

#include "head.h"

int f(int x, int y) { return 0; }
int g(int x, int y) { return 0; }
int h(int x, int y) { return 0; }

main.c

#include <iostream>
#include "head.h"

int main()
{
    f(1, 2);
    return 0;
}

Upvotes: -1

Related Questions