LinuxCommandLibrary

cdecl

Translate C declarations to English (and vice versa)

TLDR

Compose English phrase into C declaration, and create [c]ompilable output (include ; and {})

$ cdecl -c [phrase]
copy

Explain C declaration in English
$ cdecl explain [C_declaration]
copy

Cast a variable to another type
$ cdecl cast [variable_name] to [type]
copy

Run in [i]nteractive mode
$ cdecl -i
copy

SYNOPSIS

cdecl [-a] [-d] [-e] [-i] [-p] [-u] [--] [english declaration]

PARAMETERS

-a, --all
    Enable both English-to-C and C-to-English modes

-d, --declare
    C declaration to English (default for declare)

-e, --cdecl
    English to C declaration (default for cdecl)

-i, --interactive
    Interactive mode; read multiple lines until EOF

-p, --posix
    Strict POSIX parsing (no GNU extensions)

-u, --undef-ok
    Allow undefined type names

--help
    Display usage summary

--version
    Print version information

--
    End of options; treat rest as input

DESCRIPTION

Cdecl is a utility that deciphers complex C language declarations by translating between cryptic C syntax and readable English prose. It aids programmers in understanding intricate prototypes involving pointers to functions, arrays of pointers, multi-level indirections, and qualifiers like const and volatile.

For instance, entering 'declare x as array 10 of pointer to function returning pointer to foo' produces: foo (*x[10])();. Conversely, inputting a C declaration like int (*foo)(char *, int) yields: 'declare foo as pointer to function (char *, int) returning int'.

The tool supports interactive mode for repeated queries and POSIX-compliant parsing. Invoked as cdecl (English-to-C by default) or declare (C-to-English), it's essential for mastering C's declaration grammar, as defined in K&R or ANSI C standards. Install via package managers like apt install cdecl on Debian-based systems.

CAVEATS

Not installed by default; supports ANSI/POSIX C but may fail on very obscure or non-standard declarations. Limited to function prototypes, not full type expressions.

EXAMPLES

cdecl declare foo as pointer to function(int) returning int
int (*foo)(int);

declare -i int (*bar[5])(void);
Interactive: declare bar as array 5 of pointer to function(void) returning int

HISTORY

Created by Geoff Kuenning in 1985 as a Unix tool for C programmers. Evolved through GNU ports and modern maintenance; widely used in education and development since 1990s.

SEE ALSO

declare(1)

Copied to clipboard