Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
C gibberish to English (cdecl.org)
163 points by naryad on Sept 1, 2013 | hide | past | favorite | 36 comments


This seems to be the same as the cdecl program available in most Linux distributions (apt-get install cdecl).

Websites for these little utilities are definitely useful, though. :)


Indeed. It is quite old. I remember using the CLI version of this that I had to compile on my own in the early 90's on Solaris 2.x on a Sparc.


Oh, handy, never knew that existed.


Neat site!

    // declare foo as pointer to function (void) returning pointer to array 3 of int
    int (*(*foo)(void ))[3]
That's actually a great example to throw at Go's syntax, which if you read from left-to-right, is perfectly one-to-one with the english:

    // declare foo as pointer to function (void) returning pointer to array 3 of int
    var foo *func() *[3]int
http://play.golang.org/p/1b48cjmZbx


This is a great helper for getting acquainted with the C spiral rule: http://c-faq.com/decl/spiral.anderson.html


I've said it before (http://news.ycombinator.com/item?id=5080096), I'll say it again: the "spiral rule" makes pretty pictures, but it's nonsense.


Somehow I cannot find those "spatial" (for lack of better name) rules useful.

What works for me is "type declaration mimics usage". So I find the variable name, parse the declaration as usage and then reconstruct the type. It goes like this:

  char *((*foo)[3])(int);
Ok, so if you dereference foo, then index[3], then call with int, then dereference, you get char?

Cool, looks like foo is a pointer to an array of size 3 of functions that take int and return pointer to char.


Interestingly, the "ultimate" example in that page fails to parse with the web cdecl.


Thank you very much for the website.


I also find this online utility useful when I don't have c++filt (another program available to Linux distros) available:

http://pear.warosu.org/c++filtjs/

This can be used to demangle C++ names.


Maybe it's just me but having it written in English doesn't actually help me understand complex declarations.


I'm really surprised nobody's mentioned that this is an exercise from K&R (a.k.a. "The C Programming Language"), a must-read for any C programmer!

https://en.wikipedia.org/wiki/The_C_Programming_Language


This has some odd corner cases.

"void f(void)" -> "declare f as function (void) returning void"

"void fun(void)" -> "declare fun as function (void) returning void"

"void funct(void)" -> "declare funct as function (void) returning void"

"void func(void)" -> "syntax error"

"func" is not a special name in C.

Also:

"void fn(void f(void))" -> "syntax error"

But this syntax works just fine:

    /tmp$ cat test.c
    #include <stdio.h>
    
    void fn(void f(void))
    {
        f();
        f();
    }
    
    void g(void)
    {
        puts("g");
    }
    
    int main()
    {
        fn(g);
        return 0;
    }
    /tmp$ gcc test.c
    /tmp$ ./a.out
    g
    g


Not sure what's going on with your second example, but that first one looks like an odd consequence of how a cdecl feature is implemented. cdecl doesn't just explain C declarations, it can also create them based on a more verbose pseudo-english language (the same one it spits out in response to being fed an actual C declaration, I believe). "func" is an alias for that language's reserved word "function".



Apple extension to C language for blocks/lambdas. http://en.wikipedia.org/wiki/Blocks_(C_language_extension)


Tat explains it why I haven't seen it then. I was afraid there for a second I was losing my mind.


"Blocks" seem to be an Objective C feature.

https://developer.apple.com/library/ios/documentation/cocoa/...


Technically an extension to plain C, with some Objective-C integration.


C gibberish to English gibberish.


I wonder how difficult it would be to write a C compiler (probably using LLVM would make sense) that translates an entire C program into English.


That's actually a neat idea. It could be very useful for teaching.

(For bonus points, make it output a legal COBOL programme.)


A C compiler that converts C to English is not a compiler


Pointless semantics.


For most programmers, the C notation is probably easier to read / understand than the English gibberish derived from it with this tool


Note that this is “C gibberish English”, not just “to English”. So it also converts (specially-formatted) English to C gibberish. The second and third examples at the top demonstrate this feature.


I did something like this using C++ template metaprogramming, which means you can make use of the compiler to figure out the types for you, and you just have to write class templates to produce the English description. It's described at http://blog.asymptotic.co.uk/2011/02/c-type-declaration-deco... , although somewhere along the line the markup got corrupted so the code samples don't quite render correctly.


I'm a fan of the 'c-spiral' rule/ http://c-faq.com/decl/spiral.anderson.html


Cool idea, although I expect everyone who understands the English version would also have understood the C version.


Cdecl is quite useful, but it does not work if you have declared your own types.

"Foo a" declares a of type Foo (whatever that is), but cdecl says Syntax Error.

It does not even work with C99 types. "bool a", "_Bool a", "int32_t a" all give Syntax Error.


"I guess this'll be used mainly for function pointers"... first example.


I'd like to see an equivalent for parsing common shell commands and their arguments. Just converting the short form to the long form would be a good start


It can be a useful tool to get the main "feeling" in a complex line of pointers, but I wouldn't use it to auto-generate explainer comments


too bad it doesn't accept statements, I would be glad to get its take on a Duff's device.


Should anyone take this on (to write a version of cdecl that translates statements to English) as a personal challenge, please consider making it adaptable to whatever language is currently in fad for teaching beginning programming. This could be an extremely useful tool for teachers.


I need a regex version of this.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: