Live data from Hacker News

Essential C

cslibrary.stanford.edu

41–50 of 57 posts

Re: Essential C

#43

Earlier quoted context omitted.

A lot of code is still written in C89 for maximum portability, and I regularly do that. The only thing I really miss from C99 is the mixed declarations and code, but you can instead use nested scopes or just move the declarations up to the top.

What portability do you need, specifically, beyond C99?

Until recently, even MSVC was C89. I believe they only added some C99 features because the C++11 standard required them (in VS2013, I believe), and full C99 support only came with VS2015.

Re: Essential C

#44
post #17

Oh, memories. I took CS107 in 1996, which was apparently the date this handout was written. Nick was my academic advisor, and his CS108 (which was C++ at the time) was one of my favorite courses of all time. Bunny World... Maybe I'm just a grumpy old programmer, but I feel like kids these days are missing out on something important by learning to program in a language that doesn't require them to truly grok memory ma…

Most often, in general purpose programming, you don't need burden of managing memory manually. Things just go fine with a good runtime.

And that is what I think beginners should be introduced to - how to write code, how to frame algorithms in your mind and convert them into lucid code. Python does all of this wonderfully. It hides all of the internal details and exposes just the part that is needed for sometime writing a computer program for the first time in their life - simple statements which do simple things.

There is no need for a beginner to understand memory management at all. Trying to understand what is code or how algorithms work is more important to build that mental capacity IMO. Pardon me, but it does seem that just because you had to undergo the pain of trying to understand pointers, memory layout and allocations, you want everyone to go through the same experience as well.

Re: Essential C

#45

Does anyone have any recommendations for a document like this that describes how NOT to write C? Like the security issues and tricks to avoid the pitfalls?

http://www.literateprogramming.com/ctraps.pdf

Also, consider looking into MISRA C guidelines. They contain many related tips.

Re: Essential C

#46
The last link to "the great tree-list recursion problem" is an awesome assignment for thinking about pointers. (Though not really in any way representative of useful code.) Still fun to do after 20 years of writing C.

Re: Essential C

#47

Earlier quoted context omitted.

I don't know if I would count any part of C11 as "essential", particularly given its still-spotty implementation coverage.

A lot of code is still written in C89 for maximum portability, and I regularly do that. The only thing I really miss from C99 is the mixed declarations and code, but you can instead use nested scopes or just move the declarations up to the top.

And stdint.h, variadic macros, restrict, inline (admittedly less necessary as compilers become more clever), flexible array members...

I mainly still stick to variable declaration at the beginning of the block out of habit but it would be a pain for me to go back to C89 and forsake all these other nice features.

Re: Essential C

#48
post #8

I recently started a graduate course that originally required C++. My only exposure to C++ basically went through the fundamentals of the C part without getting to any of the ++. Is there something similar to this for C++? I understand that C++ is a much larger language and I'm definitely not looking for something comprehensive, more of a solid overview of a workable subset of the language. Enough to be productive, p…

Word of advice. Don't learn any of the OOP stuff in C++, many learned it, but later they had to unlearn it. Search for data oriented design.

That’s as bad as saying OOP is the cure for all your problems! Learn all the paradigms and use them where they’re best used, don’t shun any or advise someone against it just because you’ve had a bad experience.

Re: Essential C

#49

Does anyone have any recommendations for a document like this that describes how NOT to write C? Like the security issues and tricks to avoid the pitfalls?

This is the book for this particular subject:

https://www.amazon.com/Expert-Programming-Peter-van-Linden/d...

Every C programmer should have this on their bookshelf!

Re: Essential C

#50
First mistakes I found are on page 4.

"the compiler "promotes" the smaller type to be the same size as the larger type before combining the values." Values smaller than int are always promoted to int.

"Promotions do not lose information -- they always convert from a type to compatible, larger type to avoid losing information." This isn't true when a signed type is promoted to unsigned.

It seems that the author didn't read the C language specification.

Post reply on HN