Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have never really done a large amount of C programming but am in the middle of teaching myself low latency C++. Would it do more harm than good to read the K&R C programming book? I am a bit worried I will be reading about C conventions which may no longer hold in C++ (especially with C's fondness of global variables).

share|improve this question
    
Why?................... –  James Mar 9 '13 at 2:00
1  
Kernighan and Ritchie's "The C Programming Language" is a book on everyone's book shelf - if you're over 40 (like me). If I were learning C today I would look for something more modern and maybe better. If you're interested in low latency applications - you might look at hard real-time programming (although predictability is not the same as latency - real time systems often have to run in low-latency environments). Finally, if you're interested in learning C++, just learn the heck out of C++. –  ipaul Mar 9 '13 at 5:29
2  
K&R is still the only book that explains pointers correctly. Definitely worth a read. –  tp1 Mar 9 '13 at 14:53
    
I think I will skim through and maybe take note of chapter which explain fundamentals with the stack/heap, rather than syntax/convention. To the downvoters- no reason why? –  user997112 Mar 9 '13 at 14:54
    
C does not have a "fondness" for global variables. But, you should also know that C is not a subset of C++. There are a large number of mutually exclusive differences between C and C++. This is more true as each standard evolves. K&R (especially K&R2) is a very good book, but does not cover C99 or C11, and has very little directly to do with C++. –  Randy Howard Mar 13 '13 at 8:57
add comment

4 Answers 4

C isn't the same as C++, particularly today. They have wildly diverged in terms of good practices and idiomatics. K&R may be a classic but that's just what it is, a classic. There are better modern C books out there and certainly better resources on low-latency C++ that focus on C++ as C++ rather than C with gribblies.

share|improve this answer
1  
What are the better books? –  Robert Harvey Mar 9 '13 at 18:16
    
Yes, I'm also curious about the better books. –  faif Mar 9 '13 at 18:44
    
+1 for the first part. C and C++ are very different today. –  faif Mar 9 '13 at 18:46
    
In my experience, King's C Programming: a Modern Approach is better as far as actually learning the language. I found it easier to understand but I also know the author, so I'm probably biased. A forthcoming book called "Real Time C++" looks promising as far as low-latency goes. –  World Engineer Mar 9 '13 at 18:47
add comment

I'm a c# programmer, and I'm planning to study K&R. I'm hoping it won't corrupt me so that I'm no longer able to write c# code.

Seriously, just think of C as a low-level programming language that follows the same fundamental principles as any other programming language, which bears some similarity to C++ and share some of its features.

In other words, it's useful to study C as a language in its own right, and I believe it will be a useful primer for learning C++, if you want to go that route.

share|improve this answer
1  
I agree it useful to study C, I'm not sure I agree its a useful primer for C++ –  jk. Mar 9 '13 at 9:31
add comment

K&R is a good read regardless, if only because C is the grandmother of C++, Java, C#, S/R, ... You'll get a good sense that computers execute instructions, not abstractions. That poor little chip has to cycle away doing what you asked it to do, and the less you ask it to do, the sooner it will finish.

C++ is a terrific language but - it strongly tempts you do do things that take you far away from your goal of minimum latency. It tempts you to build massive class hierarchies, with lots of new-ing and delete-ing, notifications galore, and falling in love with container classes. The idea of doing these things in healthy moderation is usually absent.

The result is, without realizing it, you can build in sources of slowness taking you orders of magnitude away from optimality. You might have the greatest big-O algorithms, but the constant factors can be killers. However, you can get back to optimality if you know how and you're willing to take a meat-axe to the code. You can stay in C++ and have nice classes, without wasting any cycles. Here's an example.

share|improve this answer
add comment

When doing deterministic or low latency coding on very limited hardware, sometimes those "corrupt/bad/non-PC" coding practices can be beneficial, or even necessary to meet the real-time requirements and/or consume the least nano-watts of system energy. For instance, hand placing global variables in the minimum number of 1st level data cache lines or small local scratch memory bank, etc. C is useful in this manner as a sort of near-macro-assembly langauge for RISC (and PDP-11) processors. Maybe a book on assembly coding and machine language for your choosen processor would be useful as well. It's all part of having a big toolbox. Just make sure not to use the right special tool in the wrong place.

share|improve this answer
    
Could you just confirm- are you saying global variables can increase performance? If so, could you elaborate? Thanks –  user997112 Mar 15 '13 at 19:55
    
Depends on the compiler, the particular optimization settings for that compiler, and the target CPU implementation, as well as what you are comparing a global variable access against. –  hotpaw2 Mar 15 '13 at 20:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.