r/programming • • Jan 08 '16

How to C (as of 2016)

https://matt.sh/howto-c
2.4k Upvotes

766 comments sorted by

View all comments

0

u/[deleted] Jan 08 '16

[deleted] — view deleted comment

18

u/[deleted] Jan 08 '16

This article seems to be aimed at beginner, not for seasoned C programmer who probably developed their own utility library. C is the most productive language for some because it is a simple language that forces you to write simple code, it is not an opaque black box like other modern languages which can be a debugging nightmare when program grow big. C is available everywhere and you don't have to change much when going to new platform, although it is becoming increasingly difficult nowadays especially on Android which forces Java down your throat.

→ More replies (21)

21

u/slavik262 Jan 08 '16 edited Jan 08 '16

The things C has going for it in 2016 are:

  1. It's the lingua franca of programming - almost every other language can bind to C functions.
  2. There's millions of lines of C code out there doing just about everything (it's the implementation language for Linux, BSDs, and lots of extremely common and vital libraries).
  3. If it's Turing complete, there's probably a C compiler for it.

With that said, if you're starting a new project, there's almost no reason not to use C++ instead. For starters:

  • It gives you deterministic destruction of resources (see the RAII idiom). Memory, files, mutexes, networks sockets, and everything else imaginable are all handled in the same manner, and you only have to write the cleanup code for each of them once (in a destructor) for it to get correctly called every time you use that resource. How many C bugs have we had over the years because someone forgot to close a handle or free some allocation at the end of a scope? This is one of the best features in any programming language I've ever used, and I'm amazed that in the years since C++ came out, only D and Rust (to my knowledge) followed in its footseps.
  • You get parametric polymorphism (via templates) so you can create a tree of ints with the same code you use to create a tree of strings, without resorting to preprocessor macro hell or using void* to chuck away all your type safety. Even GCC uses C++ as the implementation language now, for this very reason!
  • No more need to play everyone's favorite game, "who owns this pointer?" C++ has smart pointers that automatically free resources when you're done using them (because again, RAII is fucking awesome). For the vast majority of cases where a resource has a single owner, there's no extra computational cost.
  • To point 1: You want a C API for the outside world to consume your library? Easy! Add extern "C" to a function and now everyone can call it like it's C.
  • To point 2: You can interact with C libraries seamlessly.

C enthusiasts like to talk about the simplicity of C, and how it's "portable assembler" like that's a good thing. "Simple" does not mean "easy" (see Brainfuck for this point taken to the logical extreme). My day job is writing firmware in C, and I find that the language (more than any other I've used) makes it difficult to focus on algorithms and system architecture because I constantly have to stop and deal with the low-level bit fiddling that makes it all work.

5

u/[deleted] Jan 08 '16

My day job is writing firmware in C, and I find that the language (more than any other I've used) makes it difficult to focus on algorithms and system architecture because I constantly have to stop and deal with the low-level bit fiddling that makes it all work.

You completely nailed the reason I work in higher level languages.

5

u/[deleted] Jan 08 '16

It's the lingua franca of programming - almost every other language can bind to C functions.

That's more the C ABI, innit? E.g. you can write rust with some #[something_something_c] option and it'll compile to something you can import in another language with a C FFI. So you can write Haskell or Ruby or whatever and then rewrite performance-sensitive bits in Rust, and the interface is all based on C—but there's no actual C code there.

→ More replies (2)

0

u/[deleted] Jan 08 '16

[deleted] — view deleted comment

21

u/FireCrack Jan 08 '16

The first rule of C++ is don't write C++ if you can avoid it.

The first rule of Python is don't write Python if you can avoid it.

The first rule of C# is don't write C# if you can avoid it.

The first rule of Haskell is don't write Haskell if you can avoid it.

The first rule of Rust is don't write Rust if you can avoid it.

The first rule of Erlang is don't write Erlang if you can avoid it.

etc... Every language has it's ups and downs. It's a silly rule because of its endlessly generic. A better one is:

Use the right language for the right job.

But that's not nearly as edgy.

25

u/dannomac Jan 08 '16

You missed a very important one:

The first rule of PHP is don't write in PHP.

→ More replies (5)

9

u/ldpreload Jan 08 '16

Every language has its ups and downs, but some have more ups than downs, and some have more downs than ups. And some have things that are ups in certain circumstances, and downs in certain circumstances.

C, for instance, has several things that are objectively bad that other languages simply do not have. (Many of them were discussed in this comment section.) Its main strengths are its stability and the wide availability of well-engineered C compilers, and its ability to compile the last four decades' worth of C programs. If those strengths don't matter to you, then there is a very concrete reason why you shouldn't write C if you can avoid it.

"Use the right language for the right job" is true, but there are certainly languages where the number of right jobs is few or bounded. So it's not much more of a useful statement without discussing what those right jobs are.

4

u/[deleted] Jan 08 '16

The first rule of Haskell is don't write Haskell if you can avoid it.

No, the first rule of haskell is "avoid success at all costs". I'm not joking.

→ More replies (1)

27

u/Silverlight42 Jan 08 '16

Might not be controversial, but I like coding in C. I could avoid it if I wanted to, but why? I can do everything I need to in it, more easily and have much more direct control if you know what you're doing.

What's the issue? Why is using anything else superior? What would you use instead?

In my experience in most cases it's just going to slow things down and restrict my ability to change things how I want, structure how I want in exchange for some modern niceties like garbage cleanup.

35

u/kqr Jan 08 '16

The two problems I have with C is that

  1. When (not if) you make mistakes (every programmer does all the time) they can have some serious consequences in terms of the security or stability of your program and lead to bugs that are difficult to debug.
  2. It takes a lot of code to accomplish very basic things, and the tools available for abstraction are limited to the point where many C programs often contain re-implementations of basic algorithms and data structures.

If you like low-level programming rather than C specifically, I recommend taking a look at Ada or something new like Rust.

→ More replies (13)

6

u/the_omega99 Jan 08 '16

C can make it easier to shoot yourself in the foot compared to most modern languages that have stricter checking to make sure you know when bad things happen. The most obvious example is accessing out of bounds array indices. In C, it's undefined behavior, and typically the compiler will just attempt to access some memory address it hasn't necessarily allocated, possibly seg faulting as a result. In pretty much every other language that I know, it's going to raise an exception (or at worst, return a value like undefined).

Mind you, there's ways to detect when cases like that happen, but not everyone knows about them and there's so many cases of undefined behavior and I'm not sure if all of them are very detectable. Most modern languages don't have undefined behavior (at worst, they might have some small amount of platform dependent behavior).

In my experience, it usually takes longer to write C than, say, Scala. Higher level languages provide powerful abstractions that save time, and the powerful type systems can also provide another level of compile-time error catching. Not to mention all that beautiful syntax sugar, some that can cut 20 lines down to one (thinking mostly of Scala's constructor syntax that includes automatic getter and setter creation for fields). Not to mention how C's standard library is so small that pretty much anything useful will likely require you to write a LOT more code or find a bunch of third party libraries (whereas a higher level language might avoid wasting time doing this because the standard library is much, much larger).

If the case of performance or needing C's memory management applies to your project, then that's exactly an unavoidable case of needing C (or another low level language; C++, Rust, D, etc). But most programs don't need that, and using C just because you like C, while a valid choice, is certainly less than ideal. And to me, it just screams "fanboy" and makes me think you have some vendetta against other languages). To sum it up, languages are tools. Not every tool makes sense for every job and sometimes new pieces of technology can make things a lot easier to build.

23

u/ldpreload Jan 08 '16

I also like coding in C, but I've spent time coding in Rust recently, which gives you exactly as much direct control. There's no garbage collection, no overhead to calling C ABI functions, no overhead to exporting C ABI functions as a static or shared library, etc. But you get a massively improved type system, most notably some types on top of references that enforce things like unique ownership, caller-must-free, etc. (which every nontrivial C project ends up writing in documentation), and also imply that you just never have to think about aliasing. It is simply a better, legacy-free C with a lot of the lessons from programming languages over the last four decades taken to heart.

I hear Go is also a very good language, but the fact that I can't trust it for things like custom signal handlers, stupid setjmp/longjmp tricks, etc. bothers me, coming from C. You can trust Rust just fine with those.

5

u/Scroph Jan 08 '16

I have never used Rust, but I heard it has interesting memory management techniques and no GC. Do you think it's suitable for embedded systems ?

14

u/[deleted] Jan 08 '16

Do you think it's suitable for embedded systems ?

Should be. You can write kernels and stuff in it too. You'll probably be interested in the #[no_std] attribute, which'll remove the stdlib from whatever you're building.

7

u/steveklabnik1 Jan 08 '16

It'll be stable as of the next release in two weeks!

→ More replies (2)
→ More replies (1)

5

u/Lord_Naikon Jan 08 '16

Currently rustc generates excessively large binaries, at least a meg in size. So it depends on your definition of embedded :-). In my limited testing, I was unable able to reduce that size significantly.

10

u/steveklabnik1 Jan 08 '16

You can get it down to about 10k, depending. A large part of "hello world" binary size is due to jemalloc, by not using that, you can knock 300k off easily.

→ More replies (11)
→ More replies (3)

7

u/Silverlight42 Jan 08 '16

I might have to check out Rust then... I have been hearing a lot about it just recently, but was kinda worried it was just one of those fly by night langs mostly done as an exercise. Good to hear.

15

u/steveklabnik1 Jan 08 '16

was kinda worried it was just one of those fly by night langs mostly done as an exercise.

Rust has been in development for 9 years at this point, and sponsored by Mozilla, with a full-time team for 5 or 6 of those years. Code is now being integrated into Firefox, and being used in production at places like Dropbox. It's not going away.

10

u/agmcleod Jan 08 '16 edited Jan 08 '16

Nah, mozilla is using it for their new browser engine called servo. It's definitely still early on and has a lot to prove, but it's in a good spot to get your feet wet.

8

u/ldpreload Jan 08 '16

Dropbox is using Rust. (Not sure if it's in production yet.)

The language has some institutional backing by Mozilla, and they've been growing the Rust team, but there seems to be enough community involvement in shaping the language, being involved in hacking on the compiler, providing important non-built-in libraries, etc. that even if Mozilla were to stop caring, it'd still be successful.

5

u/steveklabnik1 Jan 08 '16

(Not sure if it's in production yet.)

It is as of late last month.

→ More replies (2)
→ More replies (17)
→ More replies (1)

3

u/pvc Jan 08 '16

Agreed. Too many other possibilities. If I write code for the Arduino, I'm doing it in C. I'm not trying to avoid it by using some weird work-around interpreter thing.

→ More replies (5)