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`] [`-p`] [`expression`]
When `expression` is omitted, `cdecl` enters interactive mode.

PARAMETERS

`-a`
    Produce Arabic numerals for array sizes instead of English words (e.g., `[10]` instead of `ten`).

`-p`
    Enable pedantic mode, insisting on `int` for integer types, as required by Kernighan and Ritchie's C standard.

`expression`
    A C declaration or an English description to be translated. If provided, `cdecl` operates non-interactively; otherwise, it enters interactive mode.

DESCRIPTION

`cdecl` is a command-line utility designed to assist C programmers in understanding and constructing complex C declarations. It acts as a bidirectional translator: it can take a C declaration and render it in plain English, or conversely, take an English description of a type and generate the corresponding C declaration.

This functionality is particularly useful for deciphering intricate pointer, array, and function pointer declarations, which are often sources of confusion for C developers. When invoked without arguments, `cdecl` enters an interactive mode, allowing users to repeatedly input declarations or descriptions. It supports commands like `declare` (C to English), `cast` (English to C), and `explain` (C expression to English). Its ability to clarify ambiguous or convoluted syntax makes it an invaluable tool for both learning C and debugging declaration errors.

CAVEATS

`cdecl` may sometimes parse overly complex or ambiguous C declarations incorrectly due to inherent ambiguities in C's syntax. It focuses primarily on declarations and does not fully understand all C syntax constructs, preprocessor directives, or compiler-specific extensions. Its utility is primarily for understanding core C declaration syntax.

INTERACTIVE MODE COMMANDS

When run without an `expression`, `cdecl` enters an interactive shell. Within this mode, users can issue specific commands:

`declare `: Translates a C declaration into English.
`cast `: Translates an English description into a C declaration.
`explain `: Provides an English explanation for a C expression.
`help`: Displays information about interactive commands.
`quit`: Exits the interactive mode.

HISTORY

The `cdecl` utility has roots in the early days of C programming, designed to address the inherent complexity of C's declaration syntax. The original version was developed by David Anderson, with significant enhancements later contributed by Tony L. Hansen. It emerged as a practical tool for programmers grappling with pointers, arrays, and function pointers, becoming a classic utility for C development environments on Unix-like systems. Its design reflects a focus on clarity and education, aiming to demystify one of C's more challenging aspects.

SEE ALSO

gcc(1): The GNU C Compiler, for compiling C code that includes declarations., cpp(1): The C Preprocessor, which handles directives before compilation., indent(1): A utility for formatting C source code, which can help in readability., splint(1): A static analysis tool for C programs, useful for identifying potential errors and questionable code constructs.

Copied to clipboard