3 ms·
Ask HN: Imagine C was the only programming language you had. What would you do?
If you had to do your day to day development in C, what would you do to make yourself more productive?
Are there tools, frameworks, libraries that can supercharge modern C development and make it easier, quicker to code, safer and more full featured?
Can modern web applications and REST API's be built in any practical way using C, or is it just too painful?
- gherlein 12y agoBe happy!
- gherlein 12y agoBe happy!
- brudgers 12y agoWrite a Lisp interpreter?
- andrewstuart 12y agoI knew there would be plenty of responses like this i.e. "I'd do something else apart from C?" Ha!
- deleted 12y ago[deleted]
- brudgers 12y agoHow else do I get Emacs?
- angersock 12y agoBeen there, done that. Some helpful things: Make use of modern standards and libraries. For example, use <stdint.h> to get sane and guaranteed-width integer types. Use functional programming style wherever possible. Function pointers combined with void pointers lets you emulate closures with explicit context binding--something that's pretty powerful. Functions that map over collections and filter over collection structs are powerful. Write a solid unit test macro framework. In less than 200 lines of C you can setup a project that will let you easily add unit tests and make sure your code is working. Hide platform-specific things behind functions and typedefs. Writing a simple cross-platform binding to OS primitives like mutices and files and whatnot can save you a lot of trouble if you ever need to port, as can any of the libraries out there that provide such things. My friends and I have a pretty well-vetted one we like to use for these projects. Spend time writing safe arithmetic routines. Writing a routine that lets you do different-sized integer addition and multiplication while catching overflows is invaluable if you are ever parsing or writing binary formats. Abandon your error handling. C can be made very, very verbose if you continually check for errors. For example, consider how often you can actually recover from a failed malloc()--and then consider that Linux will just plain lie to you. So, don't bother sprinkling all of your code with "well, tell somebody we bombed out" unless it is truly fixable in the context of the caller. Consider macros for a DSL. The simple macros in C are not amazing, but can be used with a little thinking to build things similar to Flask route annotations or Java Hibernate annotations. Allocate memory all the time. Don't mess about with buffers which might get overfilled or cause you to chunk things...just accept that you'll allocate frequently and trust in libc until you know you've got a problem. Those aren't so much libraries as just techniques, but I think that covers a lot of what helps write C code quickly.
- hagope 12y agoThat's pretty good advice… Can you point to a source that does this pretty well?
- angersock 12y agoIf I had more sources which had done it well, I'd never had learned in the first place! :) For some of the safe arithmetic, this page has good tricks: http://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign http://graphics.stanford.edu/~seander/bithacks.html#CopyInte... (note that Hacker's Delight is also handy!) The Half-Life 1 SDK has some interesting examples of macro usage, though it's in C++. In general, a common place for people to use macros is defining state machines for game AI. The Flipcode archives also have some old tutorials and articles which are handy: http://www.flipcode.com/archives/ http://www.flipcode.com/archives/ Go read through the Quake 3 source code to learn about how to structure a large codebase in C for realtime apps (look especially at how the game engine is separated and traps the syscalls made by the game logic: https://github.com/id-Software/Quake-III-Arena https://github.com/id-Software/Quake-III-Arena The Linux kernel has some expert-level macro usage and organization as well. ~ I've cited a lot of game code here, because that's what's probably most accessible, and is where I cut my teeth doing stupid things and learning. There are doubtless better places (especially for best practices), but I think that open game code is a good starting point. EDIT: Dr. Dobb's had a good writeup on the unit testing stuff: http://www.drdobbs.com/testing/unit-testing-in-c-tools-and-conventions/240156344 http://www.drdobbs.com/testing/unit-testing-in-c-tools-and-c... That's a bit different from how we've solved it in the past, but still good!
- 15155 12y ago> Can modern web applications and REST API's be built in any practical way using C, or is it just too painful? Sure, you can write anything in any Turing-complete language. I've never seen one, but I am sure a C Sinatra clone exists. Quick googling finds things such as: https://github.com/tyler/Bogart https://github.com/tyler/Bogart (while written as a joke - this should illustrate the possibility. afaik tyler is a reader here) Possible: absolutely. The question that needs to be asked, though, is: "Why?" There are far better-suited (task: web application servers) languages that compile to C (Nim is a great example). Nearly every language has C interop in some capacity (at the very least, FFI to C. In the case of Rust and select others, the capability of compiling with C-compatible ABIs.)
- _RPM 12y agoI'd have a job quick. I'd have fun projects to work on quick. I'd be at an advantage over all the hip and cool kids who know JavaScript.
- airza 12y agoHaving one-off internet facing applications written in C as the norm sounds great to me, but I test the security of one-off internet facing applications for a living :)
- rdc12 12y agoYou could do a web-app in C if you really wanted to, but you wouldn't be playing to the strengths of the language. But you could write a compiler or interpreter for a new language that is well suited to that... But then you would be just reliving history.
- piouokjghfbfg 12y agoHuh? So, what, you think that C can handle writing the language you write your web app and similar in but somehow it suddenly falls apart when transferring text back and forth across a network socket? Anyway, I would write a library akin to the STL, then write a library that overlays that which handles whatever I needed to do (id est HTTP library with an extension that handles REST/etc). Then I'd sell all of the extra servers I no longer needed and hire actual programmers to replace you.
- cbd1984 12y ago> So, what, you think that C can handle writing the language you write your web app and similar in but somehow it suddenly falls apart when transferring text back and forth across a network socket? Well, if you care about buffer overflows, and you are a human, as opposed to... no, actually, a computer can't statically check to ensure that your code has no buffer overflows, either. You need dynamic instrumentation to do that, which C doesn't give you, or at least not standard C, as opposed to some C-like language we can define, at which point we might as well go all the way and write in Rust or Python. > Anyway, I would write a library akin to the STL C, not C++. C doesn't have templates, which makes a Standard Template Library quite meaningless.
- S4M 12y ago> If you had to do your day to day development in C, what would you do to make yourself more productive? I wish I could come up with the idea of writing a scripting language in C during my spare time.
- zimpenfish 12y agoCCAN has some potentially helpful snippets and libraries. http://ccodearchive.net/list.html http://ccodearchive.net/list.html
- AnimalMuppet 12y agoWhat do you want to do? Do you want to use C, or do you want to do web programming? C is by far not the best tool for doing web programming. Yes, it can be done, but it will be much more pain than necessary. Or are you in a situation where for some reason you have to use C, and you still need to do web programming?