Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ask HN: An Advanced C Tutorial
12 points by SeriousGuy on July 16, 2010 | hide | past | favorite | 17 comments
Can any one point me to an Advanced C tutorial?

I already know C++ quite in-depth but unfortunately never got to learn C. I was directly taught C++ in my high school, and then I went on to Matlab, Python, Java and finally advanced C++ (STL, why not to add default arguments in an overloaded virtual function, how virtual pointer table works and stuff).

Thus now I wish to learn C really in depth!, this is partly by ongoing discussion about C++ which I can clearly identify with.



Advanced C, C Traps and Pitfalls, and The Standard C Library.

Those are the classics.

C Interfaces and Implementations is over-recommended by people who haven't read it. It's a literate book that consists of heavily commented code. The comments distract from the prose and you're better off getting the library sources and jumping through it with ctags.

They faithfully address all of C's quirks. It's such a little language that most quirks lurk in the syntax and machine-specific parts, like storage classes, pointers, and its crappy little type system. But to master C fully you will need to learn some assembly and play with systems software. Try to replace libc with your own standard library and see how far you go.

Write something you care about in C. Small binary analysis utilities are a fun way; write an ELF parser or something. It excels in bit-manipulation. However, doing GUIs in C or processing strings would be an exercise in boredom.



This. This remains the most concise and to-the-point programming book I have -ever- read. It bothers me some that people (myself included) keep writing 900 page programming books when this can do nearly the same in 200.

Once you're done with that, pick up one of the W. Richard Stevens (RIP) books — Advanced Programming in the Unix environment, or Unix Network Programming (Vol i/ii) if you're into networks. They'll teach you the basics of so much operating system stuff (it'll be Unix, but the core concepts will apply to any modern OS) you'll be growing a long unkempt beard and wearing sandals/suspenders in no time.


You write 900 page programming books?


You didn't tell us how well you learned C++. Do you understand pointers and how to manually build simple binary search trees and linked lists?

You see, C is a really small language and "learning it in depth" includes learning arcane rules, such as what constitutes undefined behavior, what names are reserved, etc.

I would recommend reading the "C Interfaces and Implementations" book by David Hanson. Another book that I'd recommend is "Expert C Programming (dark C secrets)" by van der Linden.


I understand how pointers, Iterators etc work, I know several algorithms from binary search to SVD. I also have very good knowledge of IA 32.

"learning it in depth" includes learning arcane rules, such as what constitutes undefined behavior, what names are reserved, etc.

That's what I exactly want to learn. Esp how to optimize for memory management and pthreads and stuff.


C is a simple language with some pearls and a lot of corner cases. For the corner cases "C. Puzzle Book" by Alan R. Feuer it's a funny read with a lot of oh-oh moments.

For the pearls... well I can give you a couple: - sizeof() macro is a good friend - ISO C99 <stdint.h> introduces Exact-width integer types http://en.wikipedia.org/wiki/Stdint.h


thanks for the tips!

One interesting use of sizeof i remember in my C++ project was to override new and delete operators where a void pointer is passed and sizeof is used to keep track of how much memory is allocated/deallocated.


One interesting usage of sizeof() is to calculate the dimension of an array at compile time:

  #define dim(x)   (sizeof(x) / sizeof(x[0]))	
and this is handy for static arrays with dimension defined by its data, likes:

  static const struct {
    someEnum_t enCommand;
    void       (*Fxn)(stCommands_t *a_psCommand);
  } m_stCmdMap[] = {
    {CMD_SD_FREESPACE,         _sdFreeSpace},
    {CMD_SD_DISK_INFO,         _sdDiskInfo},
    {CMD_SD_FORMAT,            _sdFormat},
  }
When you add a new array elements, it just keep the stuffs going calculating it at any recompilation:

  for (i=0; i<dim(m_stCmdMap); i++) {
    if (m_stCmdMap[i].enCommand == stCmdLocal.enCommand) {
      m_stCmdMap[i].Fxn(&stCmdCopy);
      break;
    }
  }
Another useful macro to work with structs is offsetof(), its incredibly usefull when you have to port some code to different compilers/target with different alignments and paddings.


After K&R, the most helpful book for me was Expert C Programming: Deep C Secrets by Peter van der Linden. He even gives a little preview of C++.


My advice, First of all do not look for an advanced C tutorial. Second, Go and grab a copy of Kernighan & Ritchie's C programming language book (commonly known as the KnR book). Third, Do this book ... (yeah, don't just read it, do it).



I learned a lot from IOCCC winning entries. (http://www0.us.ioccc.org/years.html)


If you know C++ in depth, you aleady know C at a professionally usable level, or at least get there with a couple of projects. C is a subset of C++ anyway. True, there are minor differences hera and there, but shouldn't really be deadly.

I guess the more difficult part will be to transition from object oriented to procedural paradigm.


I know few things such as structs etc. and how you can derive a class from a struct and stuff. but i have never used calloc or malloc (shudder!) and have only relied on new/delete [] for all memory management.

Thus its hard for me to understand what constitutes as C and what dosen't. I mean i dont know if C supports typedef's


Well you'll learn if typedef is supported pretty quickly, when you hit enter after your compile command.

As for the malloc's and various memory allocators, learn malloc and free, the rest are convenience methods, which you can live without.





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: