Coding Standards for pure C (not C++)

2019-01-30 19:48发布

I come from a java background (from my CS classes) and a semester of C++. I am just finishing up a OpenCV project for my Co-Op that's in pure C, so I'm a bit late in asking this question.

What are the design processes and coding standards for pure C?

I'm familiar with object oriented programming, design and best practices. I'm just a bit at a loss at a non-object oriented language like C. Every single variable and function appears to be global. It makes it feel like a real mess to me.

9条回答
做自己的国王
2楼-- · 2019-01-30 20:40

You may be interested in checking out the answers to a similar question I asked not too long ago. Moreover, if you're interested in C style guides, you may want to take a look at this page since it is a repository for C (and C++) style guides. If you're in the mood for a good laugh, please take a look at the NASA C Style Guide. In particular, take a look at the massive comment... you'll know which one I'm talking about. Don't write comments like this please.

I personally recommend the Indian Hill C Style Guide with some modifications. Furthermore, you may want to purchase the book C Interfaces and Implementations if you're having trouble designing large-scale programs in C.

查看更多
Deceive 欺骗
3楼-- · 2019-01-30 20:43

Good news is that you can program in a semi-object oriented fashion in C. You can protect data, expose access functions, etc. It may not have all of the fanciness of C++, but from what I have seen of other people's C++ code, many people do not use the fanciness anyway. In other words, people write C code inside a class, where in C you'd write the same code without the class container.

First, read a book on C programming and style, K&R is fine. Second, I'd recommend checking out CERT Programming Standard. Even though this site is primarily focused on "Secure" coding standards, much of the content here is general code quality standards everyone should follow. Doing the things mentioned here will improved your quality, eliminate pesky bugs, and as a side effect, make your code more secure.

查看更多
做自己的国王
4楼-- · 2019-01-30 20:45

You can make a function or object's scope local to its source file by declaring it "static". That helps a bit.

Otherwise, the typical idiom I see for avoiding namespace clashes is to put some kind of facility identifier onto the name. For instance, everything in foo.c might be named foo_*

查看更多
登录 后发表回答