Fellow Dopers, it looks like it is time for me to learn to program in C, specifically for use in mex functions in MatLab. From what I gather The C Programming Language (2nd ED.) is the book to learn C from. I am going to order that, unless someone objects. My purpose in learning C is to use it in MatLab’s mex functions. I am proficient in MatLab’s programming language, but have not used mex functions. Do I need to get a book specifically for C in MatLab, or is the C Programming Language going to be good enough?
I am hoping to buy one book and work through it to learn what I need to know. I have access to a University library so if C Programming Language (2nd Ed.), or another book, teaches me 95% of what I need to know, I can fill in the gaps with another book. Any recommendations?
K&R has everything you need to know about C in it, although I wouldn’t recommend it to someone who has never written a program in any language before; I assume you don’t come into this category.
Another book that will be found on the desk of the hypothetical competent programmer is Code Complete, by Steve McConnell. It’s primarily orientated towards C, and covers the techniques needed to create well-engineered code, rather than stuff that just happens to work through sheer luck.
Maybe, I’ve written many scripts in MatLab, and done a few little programs on my calculator. I know the general basics of programming i.e. if, for, end etc. but I’ve never written anything longer than say 30-50 lines of code.
Well, “competent programmer” isn’t necessarily my end goal. My trade is as a Mechanical Engineer, and programing is a tool that I need in my repitoire. So I won’t be programming say a game, rather I would probably be limited to writting programs to do numerical computations or maybe a little program to control a motor. I probably should have been clearer in my OP, although I will still check out your recommendation.
I use Matlab all day every day and have never found a book that I’d really recommend. I use the online documentation all the time, though. I’ve only delt with mex a few times and I didn’t notice very much unique to Matlab. If you do pick a matlab book, be sure to get something for matlab 7. I’ve found some of the changes between 6 and 7 to be significant.
I’d find any old C book that suits you and work with that. To help narrow down the choices, you might pick a ‘C programming for ___ applications’ and work through that. If you have an idea of what area of engineering you’re leaning towards, pick a book that focuses on that.
OK, K&R is probably a bit over-the-top for what you need, although it’s still a good book. The Absolute Beginner’s Guide to C by Greg Perry is quite a bit cheaper, and covers the basics in a rather more accessible fashion.
To answer your question, the book I learned C from was “A Book on C” by Kelley and Pohl. I think it’s fine for your needs.
Believe it or not, down the road you might have to use a “true” programming language so you might as well learn C++ instead of C from the start.
So, let’s start off with something confusing. . .
I assume that when calling C from Matlab, you compile something in C first, making an object file, then you load that object file, and then have access to the function name in Matlab. If you choose to build in C++ instead, you’re going to need to add something to your function name like extern “C”. Don’t worry about why.
(Yes, I just recommended skipping C to learn C++ and then making your first C++ program be called in a C style. )