11 ms·
Essential C (2003) [pdf]
- cbd1984 10y agoThis predates the stdint.h header, which gives you int32_t and so on.
- _kst_ 10y agoSort of. The copyright is 1996-2003. <stdint.h> was introduced in the 1999 ISO C standard. But as of 2003, support for C99 wasn't very widespread, so omitting <stdint.h> was understandable.
- npx 10y agoYeah, that was my first thought after a cursory reading, y u no <stdint.h>? There seems to be a lot of great content regardless. I'd love to see an updated version.
- aninteger 10y agoNice to see strlcpy in there. How do we get it into glibc?
- _kst_ 10y agoEven nicer to see that it warns users away from strncpy.
- nwmcsween 10y agostrlcpy returns offset of dst + len of src which requires strlen(src) which isn't optional and sort of sucks imo. Something very similar but that doesn't do strlen(src) such as https://godbolt.org/g/Etpuhz https://godbolt.org/g/Etpuhz would be better.
- dimman 10y agoWell one could argue that in C a string is not a string unless it's NUL terminated... It's a choice they made to make it easy to detect truncation. If you don't care about the return value of strlcpy you can modify it to skip that part (which IMO will make it worse) but get rid of traversing src until \0 is found. If you know you're working with non-terminated "strings" then just use memcpy instead. If you're using proper functions you shouldn't end up with a non NUL-terminated string in the first place, although there are shortcomings in the commonly used libraries here unfortunately yes. It's hard to create a function that solves all problems. (Side note: IMO The return value of latest OpenBSD version of strlcat (cat not cpy) is a bit weird when size is smaller than strlen(dst))
- nwmcsween 10y agoMy issue isn't that src is or isn't NUL terminated (strings in c absolutely must be NUL terminated) its that strlcpy requires a call to strlen (probably inlineable with lto) which is going to be a lot slower than not. Also there is a bug in my 'strmcpy' in the last for loop it should be n && *s
- e19293001 10y agoI've gone through a lot of exercises[0][1] from this site especially on how to implement a linked list in C. The time spent from the tutorial was absolutely worth while. I feel happy and satisfied with my self after solving one problem at a time. For those who already know the syntax of C but wanted to learn more, the links I mentioned is a good exercise and opens up a lot of possibilities on what you can implement. You'll soon be able to realize the importance of linked list and why you'll want to use a linked list instead of just plain arrays. [0] - http://cslibrary.stanford.edu/103/LinkedListBasics.pdf http://cslibrary.stanford.edu/103/LinkedListBasics.pdf [1] - http://cslibrary.stanford.edu/105/LinkedListProblems.pdf http://cslibrary.stanford.edu/105/LinkedListProblems.pdf
- conceit 10y agoTo the contrary, linked lists are cache killers and hardly used nowadays. Or so they say on here, if I understand correctly.
- huhtenberg 10y ago> hardly used nowadays https://github.com/torvalds/linux/search?utf8=%E2%9C%93&q=INIT_LIST_HEAD https://github.com/torvalds/linux/search?utf8=%E2%9C%93&q=IN... That's unbearable. They should just redo this in JavaScript. That's what everyone uses nowadays. Or so they say on here, if I understand correctly.
- hornetblack 10y agohttps://github.com/charliesome/jsos https://github.com/charliesome/jsos
- conceit 10y agoI tried to appreciate your response, but I cannot fairly judge the code. For example, those could be performance uncritical parts of code or remnants of 20 year old code where the cache was less effective than now. Are 2000 search results a lot in a source of this size? Cache, as opposed to 20+ years ago, or whenever the pattern was developed, is indeed a thing that everyone is using just now.
- camperman 10y ago'char ASCII character -- at least 8 bits. Pronounced "car".' Pronounced 'car'? Bollocks. Pronounced char as in charred.
- cbd1984 10y agoPronounced care as in the first syllable of character.
- Nursie 10y agoThe first syllable of character sounds nothing like "care"...
- TwiztidK 10y agoIt does to a lot of Americans.
- cbd1984 10y agoHad I written it in IPA, people would be complaining about that, too... It's /kɛər/, as you can see here: https://en.wikipedia.org/wiki/Help:IPA_for_English https://en.wikipedia.org/wiki/Help:IPA_for_English And, for the record, I have the Mary-marry-merry merger: https://en.wikipedia.org/wiki/English-language_vowel_changes_before_historic_/r/#Mary.E2.80.93marry.E2.80.93merry_merger https://en.wikipedia.org/wiki/English-language_vowel_changes...
- corndoge 10y agoPronounced char as in care
- mynameisnoone 10y agoNope; it's not [care] and it's not [kahr], it's [chahr]. It's as noob as when someone pronounces nginx [njinkx] instead of [engin-x], or linux as [li-nux] instead of [lin-ux].
- mynameisnoone 10y agoDon't use this. It's really rusty and teaches bad habits superseded by C99. Here's a proper reference on C99 and C11: http://en.cppreference.com/w/c http://en.cppreference.com/w/c And see also reallocarray.
- partycoder 10y agoFor many of these examples, there's a debugger called "ddd" that can graph structures and can be useful.
- stevetrewick 10y agoI want to love ddd, really I do, conceptually it is awesome and beautiful. In practice it is (IME) buggy (almost) to the point of uselessness. It's been a couple of years since I last used it, but it was last updated around 2009 :-(
- exDM69 10y agoddd isn't my day-to-day debugger but I sometimes break it out when I need to visualize a data structure or keep track of multiple values when doing some more difficult debugging tasks. It's far from perfect but still a useful tool.
- stevetrewick 10y agoOh for sure, I've used it a fair bit, it can be an absolute lifesaver for RE work or building simple VMs, for instance where you need to grok a structure in its fullness.
- jandrese 10y agoI'm in the same boat. I've tried it out a few times and it's always a crashy mess. I want to like it, but the project seems to have been abandoned in a barely usable state.
- eplanit 10y agoThis is an enjoyable comment section (rare to say). The arguments about the pronunciation of the char keyword is all part of being C programmers. This is great. :-)
- deleted 10y ago[deleted]
- Xophmeister 10y agoThe "C Programming Cleverness and Ego Issues" is a good bit of advice: > Build programs that do something cool rather than programs which flex the language's syntax. Syntax -- who cares? Otherwise, I'd never heard of/seen the `#pragma once` preprocessor directive. (Not that I do that much C programming!) How common is it to use this over include guards?
- exDM69 10y ago> How common is it to use this over include guards? Not very. It works in Microsoft's C compiler but can't be relied on elsewhere.
- Xophmeister 10y agoThen why mention it over include guards, which are not discussed. Is Stanford a Microsoft shop, so to speak?
- exDM69 10y agoThey might have used MSVC for their exercises on this course. They're not using some C99 features such as stdint.h, which were not available in MSVC when this article was written.
- claudius 10y agoIt depends. Clang, GCC and the Intel C++ compiler all support #pragma once perfectly. Personally, I prefer it, as it is only one line rather than three and I don’t have to think of a unique name for the header guard. Supposedly it breaks if you play weird tricks with the filesystem (s.t. the compiler thinks two identical files are actually different), but I have never encountered such breakage.
- thatsadude 10y agoThis and "C traps and pitfalls" [1] were my favorites back to the days. [1] http://www.literateprogramming.com/ctraps.pdf http://www.literateprogramming.com/ctraps.pdf