Skip to content

C23 typeof examples? #28

Open
Open

Description

@dramforever

typeof is available in C23 as well as GCC and Clang as extensions (might need __typeof__ depending on flags)

See:

I'm not sure how to organize this into the existing examples though. Here's my attempt at using typeof on everything in the C section, with original as comparison. Some of these aren't really better.

Note that I haven't tested these.


As a variable:

returnType (*variableName)(parameterTypes) = function_name;
typeof(returnType (parameterTypes)) *variableName = function_name;

As a static const variable:

static returnType (* const variableName)(parameterTypes) = function_name;
static typeof(returnType (parameterTypes)) * const variableName = function_name;

As an array:

returnType (*arrayName[])(parameterTypes) = {function_name0, ...};
typeof(returnType (parameterTypes)) *arrayName[] = {function_name0, ...};

As a parameter to a function:

int my_function(returnType (*parameterName)(parameterTypes));
int my_function(typeof(returnType (parameterTypes)) *parameterName);

As a return value from a function:

returnType (*my_function(int, ...))(parameterTypes);
typeof(returnType (parameterTypes)) *my_function(int, ...);

As a cast (but try not to cast functions):

... (returnType (*)(parameterTypes))my_expression ...
... (typeof(returnType (parameterTypes)) *)my_expression ...

As a function pointer typedef:

typedef returnType (*typeName)(parameterTypes);
typedef typeof(returnType (parameterTypes)) *typeName;

As a function typedef:

typedef returnType typeName(parameterTypes);
typedef typeof(returnType (parameterTypes)) typeName;

Activity

  1. jerryryle commented on Apr 20, 2025

    @jerryryle
    Owner

    Thanks for the suggestion! The typeof syntax seems notable enough that it'd be worth including. I'll look into it when I can, though it might be a little while before I can get to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions