9 ms·
I don't do embedded systems, but as a self-respecting software engineer, I am intending to develop proficiency in C/C++ over this next year. Does anybody have a
by kevmo 8y ago
I don't do embedded systems, but as a self-respecting software engineer, I am intending to develop proficiency in C/C++ over this next year. Does anybody have any recommended books/courses/projects?
- badpun 8y agoThe prevailing opinion is that one does not simply develop proficiency in C++ in a year.
- Legogris 8y agoC is definitely doable though. Loads of good recommendations of you search for them. Everyone will recommend K&R. |This book also goes through a lot of the lower-level things, debugging, etc: https://nostarch.com/hacking2.htm https://nostarch.com/hacking2.htm.
- imdsm 8y agoI have this and K&R. Recommend both /very/ highly.
- tonysdg 8y agoA quick word of warning: be sure to treat C and C++ as two completely separate languages that just happen to have similar syntax. Yes, you can use C++ as "C with classes" (I and many others sure have at times), but you're doing yourself a disservice most of the time if you do.
- Waterluvian 8y agoHuh. I always perceived C to be a subset of C++.
- rhn_mk1 8y agoCurrent versions of C have some features that are not present in current versions of C++ (e.g. designated initializers). But that's not the point. C and C++ are in practice used completely differently, so writing C++ using C concepts with some additions rarely happens in the wild.
- 95014_refugee 8y ago... except in the firmware space, where important C++ mechanisms (especially RTTI and exceptions) tend to be unavailable, giving rise to a (useful, powerful, fun) hybrid of the two.
- adrianN 8y agoThat used to be true, but becomes less true with each new version of the respective language specs. Sometimes the differences are obvious because legal identifiers in C can be keywords in C++, sometimes the difference is subtle, like with the rules regarding type punning. But the major point is that there are almost always safer or more ergonomic ways to do things using C++ features that are not present in C.
- SamReidHughes 8y agoFWIW I think he's wrong. Basically, when writing C, you should write code the same as if you'd write it in C++, except that you're using C. Because sometimes that's annoying, things can get less type safe, but in your head, there's a C++ program that you're representing.
- jstimpfle 8y agoPrograms written in this mindset might be the number one reason why C has a bad reputation. Of course, if you just emulate what other languages automate for you, you should better write in these languages. But in reality, why C is still the best programming language for large projects (IMO) is exactly that the programmer is allowed to choose a suitable structure, such that the program can fulfill the technical requirements. Other languages force the project into a structure that somehow never fits after a couple thousand LOC.
- SamReidHughes 8y agoWhat. What good programs are written in C that don't have well-structured memory management the way C++ does it with RAII? Edit: "But in reality, why C is still the best programming language for large projects (IMO) is exactly that the programmer is allowed to choose a suitable structure, such that the program can fulfill the technical requirements. Other languages force the project into a structure that somehow never fits after a couple thousand LOC." Yeah, this doesn't make any sense. The reason is, C++ doesn't impose anything on your program structure that C doesn't, while C, with the limitations it has, imposes a tax on all sorts of ways of structuring your program. For example, you can't practically write a program using a futures library (such as the Seastar framework) in C. And every program you write in sensibly written C can be translated to C++. The exception might be really really small-scale embedded stuff that doesn't allocate memory.
- pjc50 8y ago> What good programs are written in C that don't have well-structured memory management the way C++ does it with RAII? MISRA C standards, popular in embedded projects especially automotive, ban the use of memory management altogether. The whole point of RAII is that the compiler manages it for you as far as it can. This is impossible in C because you have to do it manually. You might end up writing malloc() at the top and free() at the bottom of functions but that's the opposite of RAII.
- barrkel 8y agoC enum values are convertible from int; C++ enum values aren't. This is one of the biggest differences in fairly idiomatic C code and has been the case for a very long time (i.e. not dependent on newer C features not being in C++). #include <stdio.h> typedef enum EFoo { FOO_A = 1, FOO_B = 2 } TFoo; int main() { TFoo foo = FOO_A | FOO_B; printf("foo = %d\n", foo); } This compiles in C but not C++.
- elteto 8y agoWhat? https://godbolt.org/z/vnwvch https://godbolt.org/z/vnwvch
- antaflos 8y agoThat's not the same code, is it? GP's code does not seem to compile in C++: https://godbolt.org/z/5oAIYE https://godbolt.org/z/5oAIYE But it does in C: https://godbolt.org/z/zdTKiE https://godbolt.org/z/zdTKiE
- adrianN 8y agoTry creating a foo https://godbolt.org/z/AlHDhg https://godbolt.org/z/AlHDhg
- signa11 8y agoimho, c :: c++ == ipv4 :: ipv6 (where '::' should be read as 'is to' relation)
- wheels 8y agoC++ is largely backwards compatible to C, but idiomatic code is very different in modern practice between the two. A somewhat dated blog post I wrote on the topic a decade ago: https://blog.directededge.com/2009/05/21/c-and-c-are-not-the-same-language/ https://blog.directededge.com/2009/05/21/c-and-c-are-not-the...
- gnulinux 8y agoI like to think what is JSON to Javascript is C to C++ (not capability wise). JSON and Javascript are completely, entirely different things even though JSON is technically a subset of Javascript (every valid JSON sentence is a valid Javascript sentence). C and C++ are extremely different languages -- mostly due to cultural reasons -- even though every valid C program is a valid C++ program (not strictly true, C and C++ diverged slightly, but you got the point). I, for example, write C-ish C++ and compile it with g++ but call it "C" because it's "nothing like C++" and "it's basically C with some extra keywords". I know this comment sounds absurd but C++, especially modern C++, is a very complicated beast with a complex ecosystem and culture attached to it. If you inherit a C program and add one C++ line and compile it with g++, even though it's "technically" C++ now, I really wouldn't call it C++. The way you solve problems in C and C++ are extremely different, and this matters.
- Waterluvian 8y agoCan I be pedantic? If I'm wrong someone will correct me and I'll learn something. Json is not exactly a subset because it supports representation of numbers of any precision. JavaScript will convert to an IEEE 754. But it's up to a json deserializer to decide how many decimals to use. I think.
- gnulinux 8y agoI mean sure, doesn't change my point (pedantically C isn't a subset of C++ either). That was just a metaphor.
- pjc50 8y agoDo C first. Achieve a small functioning project of your own, like the calculator in the back of Kernighan and Pike. This will give you a good understanding of pointer orientated programming and the C tooling and debug strategies. Then pick whether you want to start from the "front" or "back" of C++; i.e. learning the language in chronological order or in reverse. C++17 idiomatic style is very different from the 99 style that most existing C++ is written in. I would suggest picking a codebase to work on and learning its style and subset of C++ first.
- nwhm 8y agoI highly recommend Computer Systems: A Programmer's Perspective for learning C. In particular chapter 3 of that book is what made it all click for me. Understanding how C is translated down into assembly is incredibly useful for understanding pointers and reasoning about your code. EDIT. The book also comes with a collection of excellent projects, which the authors have made freely available online at http://csapp.cs.cmu.edu/3e/labs.html http://csapp.cs.cmu.edu/3e/labs.html
- jtsnow 8y agoAgreed! The labs are very informative and fun. One of my favorite undergrad courses used this book/labs ~10 years ago. Glad to see it is still being used!
- depr 8y agoZed Shaw's Learn C the Hard Way.
- rhexs 8y agohttp://hentenaar.com/dont-learn-c-the-wrong-way http://hentenaar.com/dont-learn-c-the-wrong-way
- depr 8y agoNah, that author wrote: >before you rewrite your C book, perhaps you should take the time to actually dig into it and learn C first (inside and out.) which is uninformed, as Zed wrote Mongrel and Mongrel2 in C. Saying he doesn't know C is ludicrous. He might have a different approach to C, but then argue this view instead of claiming your way is the only way. The author of that blog post is saying the book is bad because it is not the way he writes C. Not because it is objectively bad. Also, replies to that post like "Just for info K&R stands for "Kernighan" and "Ritchie", please, don't compare the bible of C with "just another book on C". It is a blasphemy." are hilarious. People are just parotting "read K&R!!" off of each other. The term Stockholm syndrome is overused but it is very appropriate for people who think C is actually good.
- yarrel 8y agoK&R is a remarkably good and concise introduction to C. I think that C is actually good, and that C++ is a Scooby-Doo sandwich of fail. But this is an opinion concerning a particular domain (low-level programming) that doesn't carry over to anywhere else.
- aleksei 8y agoAside from K&R, you might want to look at the Modern C PDF https://gustedt.wordpress.com/2016/11/25/modern-c-is-now-feature-complete/ https://gustedt.wordpress.com/2016/11/25/modern-c-is-now-fea... for an opinionated but sane guide. Also 21st Century C has been recommended, and it goes into tooling as well.
- jenscow 8y agoI learned it with "Advanced Programming in the UNIX Environment". > as a self-respecting software engineer Then you probably already know more about C than you realise: if, while, for, etc. all work the same as most other languages. What you're probably not used to is needing to define functions in advance, pointers, and memory (de)allocation. If you learn by doing, then with google and github, you can create a few simplified versions of unix utilities (cat, ls, grep -F). If you're on Windows, I recommend using a Linux VM, WSL, or Cygwin - it's easier to setup than the C tooling in Windows (and you're stealth learning something else). Once you know C, you can then move onto C++ (I stopped at C).
- lsofzz 8y ago> I learned it with "Advanced Programming in the UNIX Environment". Agreed. APUE is perhaps one of the best book available covering SUS, POSIX layer in detail.
- rajaganesh87 8y agoK&R,and Richard Stevens's books
- StillBored 8y agoPlease, please, please don't suggest people use the k&r book to learn C. It is one bad practice after another and many of the suggestions in that book have given C much of its reputation for buffer overflows, stack smashing, etc.
- rajaganesh87 8y agoGive me an example?
- pjungwir 8y agoFor the language, K&R is good, and Expert C Programming: Deep C Secrets by Peter van der Linden is a great second book. But all the fun stuff happens when you start talking with your OS, so get a book about that too. If you are planning to develop on Linux, Michael Kerrisk's The Linux Programming Interface is excellent. Much of it will be familiar to someone used to shells and the terminal, but there will be plenty of new ideas too, and even the stuff you know will get a much deeper perspective.
- jschwartzi 8y agoThe Linux Programming Interface is an excellent book. Beyond that, if you're looking to go deeper into the libc in Linux I would recommend taking a look at the man pages. They're very comprehensive, especially the pages in sections 7 and 8 which explain the operating system and administration tasks.