r/Cprog • • Feb 15 '15

text | history Ken Thompson on why he chose and and * for pointers

Thumbnail
programmers.stackexchange.com
67 Upvotes

r/Cprog • • Apr 12 '16

Small, lesser known C compilers - a list

47 Upvotes

Every C compiler that is not gcc or clang and runs on either Linux, MacOSX or Windows is accepted. Please also give the supported versions of C. The project should at least attempt to accord to an existing standard.

  • 8cc: A small, self-hosting C11 compiler.

  • tcc: The fastest known C99 compiler, by Bellard.

  • c: Attempt at a very elegant, small C11 compiler by Andrew Chambers.

  • scc: A C99 compiler by the suckless project.

  • fcc: Fedjmike's C11 Compiler.

  • pcc: pcc, the portable C99 compiler

  • lcc: lcc, a retargetable C99 compiler

  • ack: The Amsterdam Compiler Kit, originally written for Minix. C89.

  • neatcc: Sub-C89 compiler with the goals simplicity, usability and reliability. The manual.

  • nwcc: C89 compiler written by Nils Weller. Already able to compile applications like for example bash and lynx.

  • Suggested by /u/FUZxxl: kencc, and the Plan 9 compilers (although they don't run on Linux or MacOSX or Windows). I didn't find any official link for the sun C compiler.

  • Suggested by /u/systemsock: pellesc. A small IDE (compiler, assembler, macro assembler and a lot more) for Windows.

  • Suggested /u/wang_li: Oracle Solaris Studio

  • suggested by /u/staalmannen: openwatcom, "the only proper 16-bit compilers still alive".

  • Suggested by /u/skeeto: sdcc: a retargettable, ANSI C compiler, bcc: Bruce's ANSI C compiler

  • Suggested by /u/dmitrinove: vbcc, a "highly optimizing portable and retargetable ISO C compiler"

Other C compilers I did not consider worth noting.

Thanks for the submissions!

I will put them into the wiki and post a similar thread about implementations of the C standard library.


r/Cprog • • Oct 11 '14

text | learning | language Do not trust random websites about C

Thumbnail ramblings.implicit.net
43 Upvotes

r/Cprog • • Dec 26 '15

A simpler printf

38 Upvotes

Consider the following (assume suitable definitions for M_NARGS, M_FOR_EACH and panic, which are boilerplate easily found elsewhere):

void my_fprintf(FILE * stream, const char * format, int argc, const char * argv[static argc]) {
    int next_str = 0;
    for (const char * c = format; *c; ++ c) {
        if (c[0] == '$' && c[1] == 'V') {
            if (next_str >= argc) { panic(); }
            for (const char * d = argv[next_str]; *d; ++ d) {
                putc(*d, stream);
            }
            ++ next_str;
            ++ c;
        } else {
            putc(*c, stream);
        }
    }
}

enum { MY_FPRINTF_BUF_SIZE = 32 };

#define my_fprintf(stream, format, ...) my_fprintf(stream, format, M_NARGS(__VA_ARGS__), \
    (const char *[]){ M_FOR_EACH(MY_FPRINTF_FORMAT_ARG, __VA_ARGS__) })

#define MY_FPRINTF_FORMAT_ARG(A) _Generic((0, A), \
    int: my_format_int, \
    float: my_format_float, \
    double: my_format_float, \
    char: my_format_char, \
    char*: my_format_string)(A, (char[MY_FPRINTF_BUF_SIZE]){0}),

const char * my_format_int(int a, char buf[static MY_FPRINTF_BUF_SIZE]) {
    snprintf(buf, MY_FPRINTF_BUF_SIZE, "%d", a);
    return buf;
}

const char * my_format_float(double a, char buf[static MY_FPRINTF_BUF_SIZE]) {
    snprintf(buf, MY_FPRINTF_BUF_SIZE, "%f", a);
    return buf;
}

const char * my_format_char(char a, char buf[static MY_FPRINTF_BUF_SIZE]) {
    snprintf(buf, MY_FPRINTF_BUF_SIZE, "%c", a);
    return buf;
}

const char * my_format_string(const char * a, char unused[]) {
    (void)unused;
    return a;
}

#define my_printf(...) my_fprintf(stdout, __VA_ARGS__)


int main(void) {
    my_printf("Hello $V!\n", "world");
    my_printf("There are $V arguments to this call. The remainder are $V, $V, $V, $V and $V.\n",
              6, "foo", "bar", (char)'c', 'd', 4.75);
}

(assume also a more complete/complex/correct core implementation in a real-world scenario)

In other words, between features added in C99 and C11, it's possible to design a printf-like function that doesn't need to care about type-specific format specifiers, or use va_list in the implementation:

  • C99 added __VA_ARGS__ and made it possible to implement the M_NARGS (count number of arguments) macro, which reduces the importance of the va_list because we can now pass fixed-length arrays and a generated array length (it also added checkable array length specifiers for function arguments, which are at least potentially useful for non-pointers). This is unfortunately of limited use for a printf-like function because an array demands all elements have the same type. But...

  • C11 added _Generic, which gives us a way to convert all of the arguments in the variable list to a single type outside the function's body, prior to being added to the argument array. This eliminates the need for a va_list as the function no longer needs to accept variably-typed arguments at all.

In theory, I think this should have the potential to be safer (argument array is of a known size, stack doesn't risk being inspected, error is guaranteed catchable) and slightly more convenient (e.g. you could add a ${1} style syntax to grab substitutions multiple times). Whereas printf itself requires a compiler to go outside the language to analyse its correctness, which doesn't sit so well with me.


r/Cprog • • Jan 18 '15

text | code | systems Write a shell in C

Thumbnail
stephen-brennan.com
38 Upvotes

r/Cprog • • Mar 17 '16

The ISO C WG has started working on a new standard: C2X

Thumbnail open-std.org
37 Upvotes

r/Cprog • • Nov 28 '14

text | code | systems How Not To Write a Signal Handler

Thumbnail 741mhz.com
37 Upvotes

r/Cprog • • Aug 23 '15

C Programming Substance Guidelines AKA Everything You Need to Know to Write Good C Code AKA The StrongLink Programming Guidelines

Thumbnail
github.com
35 Upvotes

r/Cprog • • Mar 07 '15

text | systems What a C programmer should know about memory

Thumbnail
marek.vavrusa.com
35 Upvotes

r/Cprog • • Oct 10 '14

text | humor The C Paradox: "I'm gonna live fast, die young, and leave a good-looking corpse" (2011)

Thumbnail dis.4chan.org
35 Upvotes

r/Cprog • • Apr 09 '15

text | code | systems | virtualization Emulator 101 - a detailed, step-by-step guide to writing an Intel 8080 emulator

Thumbnail
emulator101.com
34 Upvotes

r/Cprog • • Jan 13 '15

text | code | language Implementing smart pointers for the C programming language (x-post /r/programming)

Thumbnail
snaipe.me
34 Upvotes

r/Cprog • • Dec 08 '14

text | tooling | debugging Valgrind is *NOT* a leak checker « Modern Maintainable Code

Thumbnail
maintainablecode.logdown.com
34 Upvotes

r/Cprog • • Nov 28 '14

text The Unreasonable Effectiveness of C

Thumbnail damienkatz.net
34 Upvotes

r/Cprog • • Oct 15 '14

text | news musl 1.1.5 released with full C11 library support, including the C11 threading API

Thumbnail
gustedt.wordpress.com
32 Upvotes

r/Cprog • • Dec 05 '14

text | code | systems A quick tutorial on implementing and debugging `malloc`, `free`, `calloc`, and `realloc`

Thumbnail
danluu.com
31 Upvotes

r/Cprog • • Oct 22 '17

Making Unwinding Functions in C Simple: Do Not be Afraid of Using Gotos

Thumbnail giedrius.blog
30 Upvotes

r/Cprog • • Nov 16 '14

code | library | language | algorithms Cello - higher level programming in C

Thumbnail libcello.org
30 Upvotes

r/Cprog • • Apr 22 '17

Generic Data Structures in C

Thumbnail tiehuis.github.io
29 Upvotes

r/Cprog • • Apr 17 '16

sort.h - compile-time sorting library in a header

29 Upvotes

https://github.com/swenson/sort

"sort.h is an implementation a ton of sorting algorithms in C with a user-defined type that is defined at include time.

This means you don't have to pay the function call overhead of using standard library routine. This gives us the power of higher-level language generics.

In addition, you don't have to link in a library: the entirety of this sorting library is contained in the header files."


r/Cprog • • Jan 05 '16

A defense of C's null-terminated strings

Thumbnail utcc.utoronto.ca
29 Upvotes

r/Cprog • • Mar 04 '15

quiz | language I Do Not Know C

Thumbnail
kukuruku.co
29 Upvotes

r/Cprog • • Feb 22 '15

text | code | systems | networks Systems programming wiki by students and faculty at the University of Illinois at Urbana-Champaign

Thumbnail
github.com
29 Upvotes

r/Cprog • • Nov 22 '14

code | systems | humor πfs - the data-free filesystem

Thumbnail
github.com
29 Upvotes

r/Cprog • • Jul 28 '20

C2x: the future C standard

Thumbnail
habr.com
28 Upvotes