9 ms·
C Primer
- larkeith 8y agoArchive link: http://web.archive.org/web/20180504113302/https://www.enlightenment.org/docs/c/start http://web.archive.org/web/20180504113302/https://www.enligh...
- sevensor 8y agoI have mixed feelings about this, since it's got a lot of great information for someone getting started with C. I can see why you'd want to avoid scattering dire warnings all over the place, but it skips blithely past a whole host of hazards, stopping briefly only to mention that synchronization can be a cause of threading bugs. It's leading people down the garden path, and then leaving them at the bottom where there's a hungry tiger.
- jws 8y agoThe threading section has a reasonable warning sign before the tiger… All threads can read and write to all memory within that process. This is extremely dangerous, so avoid threading until you have fairly well mastered C without threads. If you must use threads, even if you are experienced, sticking to a model where threads share as little data as possible and very carefully hand off data from one thread to another and then never touch it again is far safer.
- loeg 8y ago> I can see why you'd want to avoid scattering dire warnings all over the place, but it skips blithely past a whole host of hazards Much like the enlightenment codebase itself, unfortunately. If you want to see some quite bad C code, just check out the source.[0] [0]: https://what.thedailywtf.com/topic/15001/enlightened https://what.thedailywtf.com/topic/15001/enlightened
- sevensor 8y agoWow. Glad I use Qt!
- simias 8y agoIt's been way too long since I've learned C for me to put myself in the shoes of somebody attempting to learn it for the first time but it's a bit steep for a "primer", isn't it? I mean just looking at the examples it goes from introducing structs to a complex examples dealing with malloc, free, enums, pointers, NULL, arrays and for loops. I also think the section about "the machine" is interesting but it's a bad idea in a C tutorial. When you code C you code for the C abstract machine, not your CPU. It's very important to understand that difference, especially if you want to write portable code. For instance integer overflow is most likely very well defined on your machine, but it's not in the C virtual machine. C is not a macroassembler. Furthermore I really don't think the details in this sections are relevant in the early stages of learning C. There also are a few technical inaccuracies: >So bytes (chars) can go from -128 to 127, shorts from -32768 to 32767, and so on. By default all of the types are signed (except pointers) UNLESS you put an “unsigned” in front of them. That's true on most systems but "char" can be either signed or unsigned (IIRC on ARM they default to unsigned) and the C standard only gives minimum ranges, so 32bit chars and 64bit shorts are theoretically possible (and can occur on systems where the smallest addressable value is larger than 8 bit such as some DSPs). It's pure nitpick of course but why even bother mentioning that in a C intro? Introduce stdint.h instead, that's actually very useful for any C coder. >You can have a pointer to pointers, so de-reference a pointer to pointers to get the place in memory where the actual data is then de-reference that again to get the data itself >So keep this in mind as a general rule - your data must be aligned. >Note that in addition to memory, CPUs will have “temporary local registers” that are directly inside the CPU. Are we seriously talking about nested pointers, alignment and CPU registers before we've even introduced printf? If a newbie manages to get past that and continue through the examples I congratulate them. I don't want to sound too harsh, it's definitely very commendable to try and share your knowledge with other people and writing tutorials is very time consuming and not always very rewarding. That being said I definitely wouldn't advise anybody to start learning C using that document, especially since there's already a wealth of resources for learning C, both online and off.
- abainbridge 8y ago> 32bit chars and 64bit shorts are theoretically possible Indeed. I worked at Cambridge Silicon Radio. Their Bluetooth chips often had two processors: a XAP for the BT stack and a Kalimba DSP for audio processing. They had 16-bit and 24-bit chars respectively. About 1 billion such chips have been sold, so it's not just some wacky architecture invented by a mad bloke in a shed.
- oldcynic 8y agoNot a bad summary. Albeit with some odd choices. [EDIT: Reading a little more I'm revising down as summary. There's quite a bit that's amazingly tortuously explained, or slightly wrong. e.g. '//' doesn't have to be the first 2 chars of a line, it's anywhere to EOL. He refers to both parentheses and braces as braces! Explanation of needing ';' at EOL is not helping any beginner who;d be better off with the traditional "all C statements end with a ';'".] Couldn't call it a primer though as it takes an express trip through the features from my first program to pointers to functions in a dozen page downs. I don't think "downside of function pointers is the added mental load to handle the indirection" rates as enough of a warning, at all. How's about letting our beginners trip up on arrays and pointer arithmetic, in a few example programs, before giving keys to all the self inflicted weaponary? "void An undefined type, used often to mean no type (no return or no parameters) or if used with pointers, a pointer to “anything” Undefined? That strikes me as a way for the beginner to get totally the wrong idea. It's used to indicate no return value or parameters in function definitions. A void pointer is generic - it can be cast implicitly to anything. You can't do pointer operations until it has been cast to a type. It is not undefined.
- _kst_ 8y ago"Explanation of needing ';' at EOL is not helping any beginner who'd be better off with the traditional "all C statements end with a ';'".]" Almost all C statements and declarations end with a semicolon. But compound statements don't.
- oldcynic 8y agoThanks. Missed 'individual'... Mind I'd stick with the K&R approach and refer to compound statements as blocks in a beginner's primer. What I wouldn't do is "explain" in a way that will probably have a beginner rescanning multiple times - I had to do a double take: "Other lines that are not starting a function, ending it or defining control end every statement in C with a ; character"
- scandox 8y agoWhenever I was trying to learn C (and as ever making small bits of progress), what I always wanted was a great visualisation system for pointers. I've seen various attempts at this online but I've never really found something that felt intuitive. I think a good on-paper means of reasoning about pointers would be a huge help in becoming fluent in C.
- groundCode 8y agoDavid malan’s CS50 course has some nice material on this. Some of the clearest well presented and enthusiastic lectures I’ve seen.
- mcjiggerlog 8y agoWhere can we watch them?
- sus_007 8y agohttp://cs50.harvard.edu/ http://cs50.harvard.edu/
- mcjiggerlog 8y agoAs far as I can tell, only the last lecture is available.
- philangist 8y agohttps://www.youtube.com/watch?v=EApk15pCIEA https://www.youtube.com/watch?v=EApk15pCIEA
- b3b0p 8y agoThere are multiple websites and communities. The /r/cs50 [0] is popular. Also, there is cs50.tv [1]. Tons of information and where or how to work through the course. Many options. [0] https://reddit.com/r/cs50 https://reddit.com/r/cs50 [1] https://cs50.tv https://cs50.tv
- bluetomcat 8y agoThis is the kind of article that touches on so many issues and aspects, not giving the experienced any new knowledge, while giving a fake sense of "completeness" to beginners, letting them build an overly simplified and artificial view. You understand what linking is when you understand how addresses in machine code work, you understand the call stack when you understand the principal layout of variables in memory, etc.
- rhencke 8y ago"This is not a theoretical C language specifications document. It is a practical primer for the vast majority of real life cases of C usage that are relevant to EFL on todays common architectures. It covers application executables and shared library concepts and is written from a Linux/UNIX perspective where you would have your code running with an OS doing memory mappings and probably protection for you. It really is fundamentally not much different on Android, iOS, OSX or even Windows. "It won't cover esoteric details of “strange architectures”. It pretty much covers C as a high level assembly language that is portable across a range of modern architectures."
- baby 8y agoI actually found it great and took some information from it, so it was useful for me at least :)
- ejanus 8y agoYours is not ideal . There are always multiple paths , and none is perfect. Let's all keep walking, mentors and mentees , noobies and experts . BTW I understood the power stack when I tempted to code a language...
- W4ldi 8y ago> int bobs > struct sandwich > enum filling
- JTbane 8y agoGreat introduction for a beginner. C remains my favorite programming language, even after all these years. It's one of the most simple languages, yet also very powerful. Yes, it is easy to make horrible bugs- but that's the price you pay for speed.
- bluetomcat 8y agoI would consider it a very compressed summary of key terms and aspects and not something to build knowledge from if you are a beginner. The trip to a healthy understanding of all these concepts goes through a lot more detailed examples while solving relevant problems and realising the motivations for the existence of each concept.
- harel 8y agoIt's interesting that this is from the enlightenment website. It's been years since I tried their desktop/window manager. Glad to see they are still around.
- zafiro17 8y agoI checked the link expressly to see if it had indeed come from the Enlightenment Project. If you want an easy way to try it again, I recommend Bodhi Linux, which packages Ubuntu fundamentals with a modified Enlightenment Desktop. It amazes me every time I use it. So gorgeous, and resource intensive (remember when Enlightenment was considered "heavy"? All the major DEs are far heavier now).
- pjmlp 8y agoThe UI was great, it had some kind of Amiga feeling with its gorgeous graphics. The actual internals not so much, to the point not everyone is pleased with its use on Tizen.
- deleted 8y ago[deleted]
- nshm 8y agoI often find tutorials about C language miss details about standard object-oriented programming with C. How to do inheritance, type checking, fields and methods, ref counting and so on. The practices are used in almost any C software today but rarely covered systematically.
- klez 8y agoColor me interested. Where can I find a codebase that uses this? Especially inheritance.
- nshm 8y agoLike here https://www.codeproject.com/articles/108830/inheritance-and-polymorphism-in-c https://www.codeproject.com/articles/108830/inheritance-and-.... For examples of this you can check gnu tools like gcc or gdb, or glib-based libraries or the kernel source.
- natch 8y agoC for people who know what EFL stands for. I believe I know C but I can’t for the life of me think of what EFL might mean. Yes I’ll google it but what does that say about this author?
- zafiro17 8y agoEFL are Enlightenment Fundamental Libraries - the underpinnings of the Enlightenment Desktop Environment. And the author is writing on the Enlightenment website: it's clearly intended for current developers/users of Enlightenment, who would probably know what EFL stands for. So it doesn't say anything about the author, who is writing for a known audience.
- natch 8y agoEnlightenment Foundation Libraries?
- mcsb1 8y agoNostalgia... Rasterman was light years ahead of his time with enlightenment. Same programmer's league as Torvalds and Bellard.
- pjmlp 8y agoThen he went working for Tizen. https://what.thedailywtf.com/topic/15001/enlightened https://what.thedailywtf.com/topic/15001/enlightened
- teddyh 8y agoSee also: Summary of the "C" language (https://www.csee.umbc.edu/courses/undergraduate/CMSC104/spring02/burt/C_summary.html https://www.csee.umbc.edu/courses/undergraduate/CMSC104/spri...)
- msla 8y agoIt always amuses me which "names" some "people" put in "quotes", and if they distinguish between "C" and C, or JAVA and Java. Probably some subtle technical distinction a programmer wouldn't understand...
- aap_ 8y agoAlpha is certainly not big endian. And MIPS today is probably little endian more frequently than big endian.
- nategri 8y agoA little light on pointers, considering how important they are and what a conceptual stumbling block they can be. I wrote C/C++ for years in a scientific setting and didn't completely conquer the damn things until I had to write some assembly for a hobby project.
- _kst_ 8y ago"In C an array is generally just a pointer to the first element." No, arrays are not pointers. "A String is generally a pointer to a series of bytes (chars) which ends in a byte value of 0 to indicate the end of the string." No, a string is by definition "a contiguous sequence of characters terminated by and including the first null character". It is not in any sense a pointer. Anyone who things arrays or strings are "really" just pointers should not be writing C tutorials. (See section 6 of the comp.lang.c FAQ, http://www.c-faq.com/ http://www.c-faq.com/ .)
- AceJohnny2 8y agoIndeed, "arrays are pointers" is a polite fiction we use to more easily pass array references around, but there are subtle differences that the compiler usually hides from you. Until it doesn't.
- _kst_ 8y agoI wouldn't call "arrays are pointers" a polite fiction. I'd call it a dangerous misconception that makes it more difficult to understand what's really going on. The language is partly at fault for a couple of rules that implicitly convert or "adjust" arrays to pointers in certain contexts, but the solution is to explain the differences, not to gloss over them.
- webkike 8y agoExactly. I think this point is most easily observed through extern.
- szemet 8y agoOk. But I do not blame people who unify the two things, because C does not really stress the difference. On the contrary - even in small details, like for example the equivalence of p[i] and * (p+i), so you can legally write 3[p] in C...(==p[3])
- simias 8y agoIt's because that C makes it easy to confuse the two concepts that it's very important to stress when and how they're different. What creates the confusion in C is that arrays try very hard to decay into pointers at any occasion and on top of that you have "fake" arrays when they're declared as function parameters (something that's probably the most insane "feature" of C IMO. And don't get me started to the a[static n] syntax that solves nothing and introduces even more confusion on top of adding yet an other completely new meaning to the 'static' keyword). Most crucially they behave very differently when it comes to using sizeof. It's also very important to understand the difference to be able to understand `const char p = "abcd"` vs `char p[] = "abcd"` or `struct s { / ... /; char data; }` vs. `struct s { /* ... */; char data[]; }`.
- gcb0 8y agowho still call a log significand a "mantissa"? specially in a easy to understand the basics kind of article.
- Senderman 8y agoThis is great, wish I'd seen something like this when I was first interested in C. Most introductions are afraid to go into anything complicated, which is ultimately patronising to a reader and doesn't help in the long term.
- cup-of-tea 8y agoK&R is the C primer. Read the entire comp.lang.c FAQ for more advanced stuff.
- AceJohnny2 8y agoI love the K&R, as a good example of a concise, well-explained, with exercises, introduction to a language. I measure every other programming book relative to K&R. That said, I wish there was a 21st century update to the K&R book, to take into account improvements made the at least the C99 standard, maybe even C11, update the programming style a bit to avoid some of the more egregious risky behaviors (always use strncpy), and some coverage of the preprocessor. All of which while still maintaining the clarity of the original book.
- _kst_ 8y ago"always use strncpy" I disagree; you should almost never use strncpy. Its name implies that it's a "safer" version of strcpy, but that's not what it is at all. If the target array isn't big enough, it can easily end up not containing a null-terminated string. As it happens, I wrote about this a few years ago. http://the-flat-trantor-society.blogspot.com/2012/03/no-strncpy-is-not-safer-strcpy.html http://the-flat-trantor-society.blogspot.com/2012/03/no-strn...
- jwr 8y agoIncidentally, a long time ago I was looking for GUI libraries under Linux, and looked at Enlightenment code. I was very, very impressed. I expected a mess, because Enlightenment was mostly (superficially) known as a fancy window manager with all the doodads, bells, and whistles. What I found instead were cleanly structured libraries with great concepts and nice C code.
- known 8y agoCute and in http://archive.li/GRxiT http://archive.li/GRxiT