# Recommend A Book to Learn C in MatLab

**URL:** <https://boards.straightdope.com/t/recommend-a-book-to-learn-c-in-matlab/348396>\
**Category:** In My Humble Opinion\
**Created:** [March 15, 2006, 1:01am UTC](https://boards.straightdope.com/t/recommend-a-book-to-learn-c-in-matlab/348396 "2006-03-15T01:01:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![treis](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@treis](https://boards.straightdope.com/u/treis)\
**Post date:** [March 15, 2006, 1:01am UTC](https://boards.straightdope.com/t/recommend-a-book-to-learn-c-in-matlab/348396/1 "2006-03-15T01:01:36Z")

</div>

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.)](http://www.amazon.com/gp/product/0131103628/qid=1142383529/sr=2-1/ref=pd_bbs_b_2_1/103-8141244-2604638?s=books&v=glance&n=283155) 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?

---

<div class="post-metadata">

**Author:** ![Tevildo](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Tevildo](https://boards.straightdope.com/u/Tevildo)\
**Post date:** [March 15, 2006, 1:31am UTC](https://boards.straightdope.com/t/recommend-a-book-to-learn-c-in-matlab/348396/2 "2006-03-15T01:31:20Z")

</div>

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](http://www.amazon.com/gp/product/0735619670/ref=pd_sim_b_1/103-9215697-6467844?%5Fencoding=UTF8&v=glance&n=283155), 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.

---

<div class="post-metadata">

**Author:** ![treis](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@treis](https://boards.straightdope.com/u/treis)\
**Post date:** [March 15, 2006, 1:48am UTC](https://boards.straightdope.com/t/recommend-a-book-to-learn-c-in-matlab/348396/3 "2006-03-15T01:48:25Z")

</div>

> [@Tevildo](#):
>
> 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. 🙂

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.

> [@Tevildo](#):
>
> Another book that will be found on the desk of the hypothetical competent programmer is [Code Complete](http://www.amazon.com/gp/product/0735619670/ref=pd_sim_b_1/103-9215697-6467844?%5Fencoding=UTF8&v=glance&n=283155), 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.

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.

---

<div class="post-metadata">

**Author:** ![Kakofonous](https://avatars.discourse-cdn.com/v4/letter/k/e9a140/32.png) [@Kakofonous](https://boards.straightdope.com/u/Kakofonous)\
**Post date:** [March 15, 2006, 4:32pm UTC](https://boards.straightdope.com/t/recommend-a-book-to-learn-c-in-matlab/348396/4 "2006-03-15T16:32:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tevildo](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Tevildo](https://boards.straightdope.com/u/Tevildo)\
**Post date:** [March 15, 2006, 4:32pm UTC](https://boards.straightdope.com/t/recommend-a-book-to-learn-c-in-matlab/348396/5 "2006-03-15T16:32:33Z")

</div>

> [@treis](#):
>
> 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.

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](http://www.amazon.com/gp/product/0672305100/ref=pd_sim_b_2/103-9215697-6467844?%5Fencoding=UTF8&v=glance&n=283155) by Greg Perry is quite a bit cheaper, and covers the basics in a rather more accessible fashion.

---

<div class="post-metadata">

**Author:** ![Trunk](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@Trunk](https://boards.straightdope.com/u/Trunk)\
**Post date:** [March 15, 2006, 5:35pm UTC](https://boards.straightdope.com/t/recommend-a-book-to-learn-c-in-matlab/348396/6 "2006-03-15T17:35:45Z")

</div>

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.

Unsolicited advice:

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. )
