Something similar to #17 burned me at my first job. Did you know that if a variable is declared as a "char", the compiler gets to decide whether it's signed or unsigned? (Consistently, of course - every "char" will be signed, or every "char" will be unsigned). I didn't at the time, but I sure do now! My employer had code that ran on about 5 billion different platforms, and my job was to port it to a new one. Everythi…
Ways to get screwed by C
31–36 of 36 posts
Re: Ways to get screwed by C
#321. Non-terminated comment Use a sane editor with syntax highlighting. 2. Accidental assignment/Accidental Booleans I always wrap my assignment-conditionals with double parentheses. It sucks when you miss these but I usually type out the right sequence of equal signs when I mean equality. 3. Unhygienic macros Treat macros like a search-and-replace with a little more intelligence, but respect how literally the pre-proc…
My biggest gripe with C is string handling. Although the extremely insecure functions have been slowly phased out (such as gets), zero-terminated strings are an attack on sanity.
Re: Ways to get screwed by C
#331. Non-terminated comment Use a sane editor with syntax highlighting. 2. Accidental assignment/Accidental Booleans I always wrap my assignment-conditionals with double parentheses. It sucks when you miss these but I usually type out the right sequence of equal signs when I mean equality. 3. Unhygienic macros Treat macros like a search-and-replace with a little more intelligence, but respect how literally the pre-proc…
Yes, the points he makes are typical beginner mistakes, or at least things that 20 years of programmer conventions have found a way around. Not really big problems of the language. My biggest gripe with C is string handling. Although the extremely insecure functions have been slowly phased out (such as gets), zero-terminated strings are an attack on sanity.
There are some nice safe string libraries out there, like this one:
Re: Ways to get screwed by C
#34Earlier quoted context omitted.
Yes, the points he makes are typical beginner mistakes, or at least things that 20 years of programmer conventions have found a way around. Not really big problems of the language. My biggest gripe with C is string handling. Although the extremely insecure functions have been slowly phased out (such as gets), zero-terminated strings are an attack on sanity.
"My biggest gripe with C is string handling. Although the extremely insecure functions have been slowly phased out (such as gets), zero-terminated strings are an attack on sanity." There are some nice safe string libraries out there, like this one: http://bstring.sourceforge.net/
Still, I wish something like that was simply built-in, as external string libraries can cause interoperability issues: Each framework defines its own string handling functions and format, making it neccesary to convert between them in an application, if you use them together.
(the worst thing is that this problem still exists with C++ as of today, even though it has a built-in string people insist on rolling their own)
Re: Ways to get screwed by C
#35Boohoo. I mean it. I don't think they even make compilers that don't warn about the top 2 issues, and 5 for sure. And after that it gets down to "I did something stupid and something stupid happened." I mean: int ii = i/++i; Seriously? As if defining the order of operations would magically take the suck out of that statement. There are like 1.5 nuggets of real pain in here. (1 point for returning a stack array, 0.5 p…
I think the problem is that a lot of languages encourage you to do something clever, whereas C actively and relentlessly punishes you for being clever. My favorite example is from #19, which is something that javascript programmers do all of the time. int value = a && b && fn(a->x,b->x); (The author goes on to complain about how value you obviously be whatever fn(a->x, b->x) returns). In javascript, that's actually g…
Re: Ways to get screwed by C
#36Something similar to #17 burned me at my first job. Did you know that if a variable is declared as a "char", the compiler gets to decide whether it's signed or unsigned? (Consistently, of course - every "char" will be signed, or every "char" will be unsigned). I didn't at the time, but I sure do now! My employer had code that ran on about 5 billion different platforms, and my job was to port it to a new one. Everythi…
You're supposed to use chars for character data , in which case it's not supposed to matter whether or not the integer representation is signed or unsigned. All you generally do with characters is store, compare for equality, and print. Those should all be safe to do without ever knowing if the underlying integer is considered signed or unsigned. It's only when you start doing arithmetic on characters, or just want t…