Re: C tutorial for Perl programmers?
by DrHyde (Prior) on Sep 03, 2013 at 10:18 UTC
|
There are two good sources from which one can learn C. I recommend using both.
The first is K&R. The second is Learn C The Hard Way. I recommend using both to learn the language - the first is beautifully concise, the second has lots of handy tips and more examples.
You will need the second to learn the toolchain. K&R hardly (at all? I disremember) mentions 'make' or tells you how to link. LCTHW has whole chapters on them, as well as chapters on testing and debugging.
| [reply] |
|
|
LCTHW looks perfect, thanks for the tip. I might be able to adapt it for Perl fairly quickly. Starting with doing a search and delete for every instance of "stupid", "asshole" and "women should stop whining".
| [reply] |
|
|
| [reply] |
|
|
|
|
|
|
Re: C tutorial for Perl programmers?
by bulk88 (Priest) on Sep 03, 2013 at 05:55 UTC
|
Teach strict types, tell them types are objects. Teach casts. Teach machine representation/memory/pointers. Teach memory management (malloc vs static vs alloca vs auto). Teach what .objs and .libs are. Teach symbol resolution/linking. Teach them "(*(__int64 **)&pointer) += 2" does not add 2 to the pointer but 16. Named parameters. Type promotion for extra credit. "if" doesn't need curlies if 1 line long. "switch". No sigils. Thats all I can think of. Both Perl and C are all algebra and PEMDAS. | [reply] |
|
|
That looks like a good start. I might leave pointer arithmetic for "if we have more time". I could probably spend a few days on that alone, likewise type promotion.
| [reply] |
|
|
Also, don’t overlook these:
- definition vs. declaration
- the preprocessor: #define, conditional compilation, #include
- arrays and const
- struct, union, enum, and typedef
- the more commonly-used standard libraries: stddef.h, stdio.h, string.h, stdlib.h
- the different uses of static
Some more advanced topics for later:
- extern
- variadic functions
- long jumps
Also of practical help when teaching memory management: explain the meaning of “segmentation fault” and “bus error” — your student(s) will be seeing a lot of these! ;-)
Hope that helps,
| [reply] [d/l] [select] |
|
|
Dont leave pointer arithmetic for later. Teach it early. It was the hardest thing for me to understand when going from Perl to C, if a pointer is just a number, why is +2 way to far off to the right in memory? Do I have to do +0.5 to get to the first slice? (no, go learn pointer arthmitic)
| [reply] |
Re: C tutorial for Perl programmers? (hard lint strict warnings bondage perlxspp)
by Anonymous Monk on Sep 03, 2013 at 08:30 UTC
|
I don't know c-well, or perlxs well, but I've done a few short programs and equally short modules with lots of cargoculting :)
Teach them to leverage the hardest of the hard and available linting tools
Is there anything like PPI or Perl::Critic for C? - Stack Overflow
Teach them similar idioms, stuff they can cargo-cult, http://rosettacode.org/wiki/C, C idioms in Perl, aww :) doesn't have c PLEAC - Programming Language Examples Alike Cookbook...
C Traps and Pitfalls/http://www.literateprogramming.com/ctraps.pdf
informative funny slideshow/presentation cartoon interview with c-programmer deep-c-and-c++ Deep C (and C++) Geektalk
and from the same guy a 2-3 day course on learning c (ITS GITHUB) cf - C Foundation course
also available at http://www.jaggersoft.com/c_course/index.html / http://www.jaggersoft.com/c_course_2/index.html
Extending and Embedding Perl (book has C tutorial)
I've not seen a book/tutorial that covers everything you're looking for, but these links may help: Re: Using a shared object in perl, Manning: Extending and Embedding Perl jenness_ch02.pdf, XSLoader, DynaLoader
http://search.cpan.org/dist/illguts/index.html
more shotgun/linkdumpexampless
perlhacktips#Portability problems -Dgccansipedantic PERL_GCC_PEDANTIC "gcc -ansi -pedantic -Wall -Wextra"
Cyclone (programming language), Buffer overflow protection,, Read perlclib, use Safefree
Maybe you can find more stuff via parent/op of Re: How to teach perl to novice programer
food luck :)
| [reply] |
Re: C tutorial for Perl programmers?
by syphilis (Archbishop) on Sep 03, 2013 at 10:02 UTC
|
I need to give them enough know-how to main XS code
I guess the first thing they need to know is the basics of C - and the fact that they already know some perl is pretty much irrelevant, IMO. That is, I think they need to be taught the C basics the same way that someone who has never programmed at all is taught the basics of C. Since these people have already programmed in perl, they may well learn more quickly than those who have not done any programming at all, but I think both groups should be given the same course (whatever that may be).
Once they have some proficiency in C, they then need to be taught how to implement C code in perl. For this, two things come to mind:
1) the perlxstut, perlxs and perlguts documentation;
2) the demos in Inline::C's C-Cookbook.pod
In the final analysis, there's not much difference between XS and Inline::C - and I think the latter's capacity to help programmers learn the former should not be underestimated.
For someone who knows perl but not C, it's a fairly steep learning curve - a curve that I would not expect to be negotiated quickly.
Cheers, Rob | [reply] |
|
|
I had been hoping I could relate things to what they already know in Perl. e.g. they've all seen things like ARRAY(0x84902a0), the deference arrow works in a similar way in Perl and C, and a lot of the syntax is deliberately similar.
I don't have a lot of hope for getting people up to XS, but I'd like to give them a good start.
| [reply] [d/l] |
|
|
I don't have a lot of hope for getting people up to XS, but I'd like to give them a good start
The nice thing about Inline::C is that it's XS under the hood but, to use it, you don't really need to know any XS at all - you just need to know some C (and, for starters, to read the perlapi documentation).
Cheers, Rob
| [reply] |
|
|
| [reply] |
Re: C tutorial for Perl programmers?
by Mr. Muskrat (Canon) on Sep 04, 2013 at 21:44 UTC
|
I've heard good things about "Understanding and Using C Pointers" by Richard Reese. In fact, I just ordered a copy.
| [reply] |