Why is writing C so hard!

Originally posted by symphonix
C - You shoot yourself in the foot.
Java - You create a model of a gun that automatically shoots you in the foot.
BASIC - You pick up a marble and throw it at your foot.
Perl - You find an efficient way to shoot yourself repeatedly in the foot with only one bullet.
HTML - You seem to be able to shoot yourself in the foot okay, but you'll need to try it out on different feet just to make sure its compatible with different footwear.

ROFL!
 
OK, just to play Devil's Advocate here, tell me why you hate C so much. I haven't heard anyone here give a reasonable explanation why C is so evil.

The include files example is an explanation of poor programming practice, not poor language design.

The #ifdef stuff is simple once you understand it. There are plenty of mystifying constructs in almost every language, what makes this different?

When you write your programs, do you document everything? Including simple constructs, like print statements? When you understand C/C++, commenting everything is ludicrous. Why the heck would I want to comment something like "i++;"?? It's simple -- increment the variable i by one.

Unix C programs are not designed for learning. They're designed to work. And I have yet to see an operating system written in anything other than C. Why is this? Because ANSI C sucks? Or maybe it's just that it's powerful?

I am not going to say that C doesn't have drawbacks -- there are plenty of reasons that C is not a good language. I just haven't seen them here. I've just seen a lot of people say "C SUX!!!!" and I have no idea why they are saying it. Does it suck because it's difficult to learn? Because it's not well documented? Because the programmer who wrote the source code you're looking at couldn't code his way out of a paper bag? What?
 
My third programming language was C (after Basic and Assembler) ... I really loved it back then. And every now and again I try and programm something small in C just to remember where a lot of it started.

I DO believe that there is most definitely still a place for C. As the previous poster pointed out, its used for system level programming. However I do application programming, and C just ain't the right language for me there. If I was doing performance critical (or real time) applications, then that would be something else. But I don't. And neither do most other programmers (that last statement is just wild speculation, and please feel free to correct me if I'm assuming too much :)))

Personally, I don't like having to declare all the variables at the top (well, you had to the last time I programmed C). I don't like the way the #includes work compared to, say, Java imports. But other than that, there aren't really THAT many differences (bar the whole OO thing ...) ... and I've seen ugly C code just as I have seen ugly Java code and ugly Perl code, etc.

As far as documentation is concerned - it took me a wee while to get used to man pages back when was at Uni - because before then I was only used to Windoze help. Their different. There's actually a lot more (helpful) information in the man pages ... but I just wasn't used to them. Same applies to API docs. If you're only used to JavaDoc API docs (which IMHO are still the best), then you'll find it difficult at first getting into the whole C API docs. But if you persist and just give it a chance for a month or two, then you'll get used to that style of docs. (Same goes for Perl/Python/C++/Delphi, etc).

Anyway, just my 2 cents (and now that we have the Euro here, that saying actually makes sense, geographically, for me to use :))))

C
 
I agree, LordCoven. Every language has its strong points. Well, except maybe Visual Basic. ;) Sorry, personal prefs showing there.

You do still have to declare variables in C, by the way. And from my experience this is a good thing. But I'm not about to argue about preferences...

Just to make my position 100% clear, I am not saying that C is the language that is better than everything else. I just want to understand why everyone else thinks C is so bad.
 
... I didn't mean declaring variables *per se* ... I meant having to declare them at the top of the function :) ... in C++/Java/Python, etc, you can declare them anywhere you want - and I prefer to declare them just before they're used :) But that's just my personal pref :)

C
 
nkuvu -

There are a lot of people who are required to take classes in VB or other ridiculous languages in order to get an AS or BS degree. Some of these people may have tried to learn other languages like C, but being used to VB style rapid development tools, C scared them away.

These "programming for the english major" classes tend to just follow along with a "Learn Visual Basic In 30µs!" book. They teach the syntax of the language without teaching much about how to program a computer.

When you don't have the basic programming foundations - knowing the different kinds of loops, how variables are stored in memory, what a pointer is for - any *real* language (C, Java, etc.) looks intimidating.

Thus, you get stuff like "C is a retard kind of language", "C SUX!!!!!!!!!!!!" (may not be enough !'s :D ), etc.

- shrill -
 
If you haven't seen an OS written in anything other than C, you haven't been around long. Pascal and Assembler come to mind. The reason C is popular here is the same reason that assembler is popular, it allows you to do bit twiddling and other obscure, awful, hardware-centric data manipulation that should only be necessary in device drivers. So, C is like a really good assembler, but I fear continuing to call it a programming language.

C is hard because it's huge. First, it wasn't written with the intention of public use, it was someone's pet project, and the keys and symbols used were chosen because they were easy to type, not because they had meaning, or were readable. Next, you have the files, which isn't really C per se, but a precompiler construct that effectively puts the code referenced in place of the #include, making it really one big file for the compiler. Then the issue of typing, late binding, templating, and other constructs of C that have been avoided, worked around, or otherwise bastardized so that C could be the all seeing all dancing crap of the world. So now you have duplicate constructs that do the same thing, (struct vs class anyone?) and many possibilities over what you can do, because C can do anything ... it's assembler ... and many ways to do it.

And then on top of that, on unix, you have makefiles, which is actually its own scriptable application again, with its own syntax and rules, and of course if you're new to unix, you have to figure out the file structure as well ... so you have to create the entire universe just to bake an apple pie. That is, in short, why I hate C.

and if you look for help in the man pages, they have their own syntax based in abstract language description syntax, so even they are confusing until you know the syntax for the man pages.

I like java, and I just wish that java hadn't settled on accepting the C syntax for a couple of things, like = and == ... it's just bad. I would like to say though, that java chose to allow this as the syntax, but java is NOT based on or derived from C. I'm offended. C is the bastard child of a hundred madmen. Learning java will allow you to program in C, but it will not allow you to read it, as C has even more obscurity and breadth than English. (American or British)

Perl at least is a scripting language that you can pretty much live in without having to learn the rest of the universe, but if you are even a touch dyslexic, you're so screwed. I can appreciate perl while hating it; C, I mostly just hate.
 
C is the bastard child of a hundred madmen

Hahahahahah!!!!


... it allows you to do bit twiddling and other obscure, awful, ...

Hey now. I like bit twiddling. There's nothing like shifting bits off the end, and wondering where they go.
 
in writing drivers for my classes, and doing making hardware do things we still used x86 assember in class, and C was like a driver for the assembler part.
 
If you haven't seen an OS written in anything other than C, you haven't been around long. Pascal and Assembler come to mind.
So tell me, O Grand Master, which operating systems were written in Pascal and Assembler. Then tell me the operating systems that are in use today that were written in Pascal and Assembler. Sorry for the tone -- it sounds like you're insulting me...

Structs can't contain functions, so they're not the same as classes. And last time I checked, C doesn't even implement classes, so this is a moot point.

Then the issue of typing, late binding, templating, and other constructs of C that have been avoided, worked around, or otherwise bastardized so that C could be the all seeing all dancing crap of the world.
Agreed.

and of course if you're new to unix, you have to figure out the file structure as well
Which of course is all C's fault. I thought we were discussing C, not operating systems...

C has even more obscurity and breadth than English. (American or British)

Perl at least is a scripting language that you can pretty much live in without having to learn the rest of the universe
It seems to me that you're contradicting yourself. Like you're saying that C is obscure, but Perl is not. Forgive me if I have misinterpreted -- but again I say that any code can be obfuscated.

Insert the standard "I'm not trying to offend" disclaimer here.
 
There is an advanced programming book at the University Bookstore .

It's on how to design your own compiler.....in C !

LOL

It all depends on what you are trying to create with your time . I know that much from my studies , I have looked at Java and Widgets for my own GUI software moduels , but C just has what I need to build the utils I want .

I remeber when I first got into computers 2 1/2 years ago after an abcense of 14 years . I was a hacker when the internet was first released , telnet baby . I was quickly turned off when neighbourhood kids were busted for pirating software . I never went from floppy to hard drive and I never got a 300 baud modem of my own . C++ was just comming out when I finally put my computer away and picked up a guitar , I was using pascal back then for fun .

With C , if you have the time the world is in your hands and Assembly can be used to tighten up your code if needed . I remeber someone telling me back in 90 to to get off DOS and upgrade to a 386 and get a student version of a Unix flavour ( I was still yakking about hacking even though I was just a wanna bee ) . He went on and on about learning C and how I could do anything in the world with it .

I want to get into embedded stuff now so thats my angle I geuss .
 
I don't know of any modern OSes written in Pascal, since our poor dear OS 9 has departed, which still had pascal stubs till its last day. As for assembler, someone wrote one a little while ago for x86 architecture that was noted on slashdot, had really impressive file sizes like Be did originally, OS, Nic driver and web browser all on a floppy, etc.

And when I say C I tend to refer to the family of C, C with classes, and C++. And as such, structs and classes are the same in C++, especially if you explicitly state what's public and private. Structs can contain functions, but it's convention not to, as that's not their intended purpose. Both contain data members, but structs default public, classes default private.

As for my other comments bringing make, precompilers, and unix into question, the original was about why it's so hard to learn from C code. This is definitely part of it. When you're not sure what is what, it looks like C has 4 different forms of syntax. I hated learning programming under C, as I spent more time learning C than learning to program. I had already programmed under Pascal, and never got used to C's terse syntax.

The comparison to Perl was made because at least Perl lives within itself, you don't also have to learn Make for instance. But it's butt ugly all the same.

Nkuvu, you have some valid points, and I respect your opinions, but I will continue to hate C in general, and you seemed to be going a little farther than I thought was valid, so I played devil's advocate. You called my bluff, err, exaggeration, and here we are. Not really trying to offend, but not really trying not to either. :)
 
Originally posted by theed
I don't know of any modern OSes written in Pascal, since our poor dear OS 9 has departed, which still had pascal stubs till its last day. As for assembler, someone wrote one a little while ago for x86 architecture that was noted on slashdot, had really impressive file sizes like Be did originally, OS, Nic driver and web browser all on a floppy, etc.
This is kind of what I was talking about. The fact that there are Pascal stubs or even full functions (stub to me means non-working function, btw) does not an operating system make. I don't think Pascal is powerful enough to make an operating system. I only have passing acquaintance with Pascal, but I've never heard of the low level abilities of C within Pascal. Assembly definitely has the power, but I don't think it has the maintainability to compete with a higher level language like C in the operating system market.
And when I say C I tend to refer to the family of C, C with classes, and C++. And as such, structs and classes are the same in C++, especially if you explicitly state what's public and private. Structs can contain functions, but it's convention not to, as that's not their intended purpose. Both contain data members, but structs default public, classes default private.
Almost every post in this thread has mentioned ANSI C as the language in question. C++ is a mutated beast that I really think should belong to another class of language (hybrid bastard child to be more precise, but I digress ;) ). I have never seen a struct with functions, nor have I ever heard that it is possible. I'll take you at your word, however, and stand (or sit) corrected.
As for my other comments bringing make, precompilers, and unix into question, the original was about why it's so hard to learn from C code. This is definitely part of it. When you're not sure what is what, it looks like C has 4 different forms of syntax.
Again I'll concede the point. I never learned C from examining Unix source code, so when I did examine Unix source code I already knew what was what. I made the classic pair of mistakes: Not reading the original post thoroughly enough and not putting myself into the new user's shoes. I will not, however, agree that you can count the man pages in with that new learning. Every single programming language has a help system of some sort, be it Windows help files (shudder!), man pages, perldoc, or the web. Since the help files all have the common problem of not being the same as the source code (syntactically speaking), I think it should be ruled out of the equation. Common denominators, if you will. Hey, it's a small point, but I need to make a stand somewhere... ;)
I hated learning programming under C, as I spent more time learning C than learning to program. I had already programmed under Pascal, and never got used to C's terse syntax.

The comparison to Perl was made because at least Perl lives within itself, you don't also have to learn Make for instance. But it's butt ugly all the same.
I find this interesting -- you never got used to C's terse syntax, but can use Perl even though it's "butt ugly"? I can make beautiful C source code, and cryptic Perl code, it all depends on me. Don't blame the language for my style (or lack thereof).

As I mentioned, I only have a passing acquaintance with Pascal -- what about it is so wonderful? If you're talking BEGIN and END vs { and } I'll make a note to myself that you're a lunatic. :) And if you already knew Pascal, why did you need to learn to program when you learned C? Programming is programming -- the syntax varies, but the concepts stay the same.

I also don't agree that you have to learn makefiles to learn C. If you're examining pre-existing code, yes. But I don't make makefiles for most of my programs -- they're almost always self contained, just like Perl.
Nkuvu, you have some valid points, and I respect your opinions, but I will continue to hate C in general, and you seemed to be going a little farther than I thought was valid, so I played devil's advocate. You called my bluff, err, exaggeration, and here we are. Not really trying to offend, but not really trying not to either. :)
I never tried to convince you otherwise. I just wanted someone to specify why they hate C so much, and be prepared to explain themselves. You hate C, I don't like C (truly!), we can all hate VB together. ;) I swear, it took a lot of prodding to get any response from someone who wanted to discuss this. So don't be confused -- I was trying to get a reaction, so may have poked a little harder than necessary.
 
nkuvu - I also don't agree that you have to learn makefiles to learn C.

No, you just have to learn makefiles. Period. ;)
 
yeah, the reason I even mentioned perl was that it is just as hard to read on its own as C, but it is a scripting language, not a language compiled via a mechanism, with some precompiler magic, in a wacky file structure ... that's all. Both ugly, Perl doesn't need compiling.

The reason I said learn programming, was that I took programming courses which assumed no prior experience, their intention was to teach programming. For the sake of brevity, I said learned. Brevity backfired as you quoted my entire message to reply. It's backfiring extra now as I re-explain.

As for Pascal, MacOS was originally written in Pascal, that's why the pascal stubs existed, you needed to make your code, no matter what it was written in, conform to the pascal calls that the OS (API's) still expected.

No pointers, that's one thing I liked about Pascal, and something I like about java all over again. Not just pointers, but the syntax introduced by them. Having some dyslexic tendencies, *, &, $, #, @ all look about the same to me when they start running together. I get physically ill trying to read C++ with pointers thrown around.

Also, I like the words "and" and "or" a lot better than && and || ... and why the fsck anyone chose = for assign ... it's ambiguous. I'm not the only one who has irretrievably lost hours of life to a =/== error. And C doesn't complain, it simply spits out false, because there isn't really a boolean type anyway. Pascal had booleans. I like booleans. ... I got off track and went over the edge there, I apologize

Anyway, Pascal used := for assign, and words instead of symbols. It was legible. People liked it for the same reason they like AppleScript now. Well written code simply told you what it was doing.

On the vein of not telling you what it's doing... C convention is to use i as a counter variable ... why? Because it was a limitation in fortran, where variables' types were defined by their first letter, thus i was an integer. All single letters might as well be x to me, I really like my variables to be things like count or loop. Math formulas are really hard for me to remember by the way. Anyway, conventions to minimize typing make things hard to read.

for(int i = 1;i < 5;i++) just isn't obvious. Let alone the if then shortcuts. And for whatever reason C programmers usually get it into their heads that shorter source files produce smaller and faster executables. So in an attempt to make their code faster, they make it hard to read beyond the inherent complexity of optimization itself. And that %d%x%s is why I hate C.

I should have been asleep hours ago. does it show?
 
I'm quite sure that Domain/OS, the
Unixy-like OS used on some of the first
true 'workstations' by Apollo Computer
was done in Pascal.
 
lol someone got busted for piracy?!
:p I grew up in greece and piracy ran rampid :p everyone did it (I did not have a computer back then and I was so envious of all my friends with Atari STs, Amigas and Amstrads :( )


I have to say that makefiles make life easy, but they are a biatch to decipher at times. I got a crash course on them but I still confuse myself with them.
 
Originally posted by theed
Also, I like the words "and" and "or" a lot better than && and || ... and why the fsck anyone chose = for assign ... it's ambiguous. I'm not the only one who has irretrievably lost hours of life to a =/== error. And C doesn't complain, it simply spits out false, because there isn't really a boolean type anyway. Pascal had booleans. I like booleans. ... I got off track and went over the edge there, I apologize
The = for assign isn't ambiguous at all, just different. Yeah, the =/== error is an easy one to do , though. Perl has the same construct.
Anyway, Pascal used := for assign, and words instead of symbols. It was legible. People liked it for the same reason they like AppleScript now. Well written code simply told you what it was doing.
Well written C code tells you what it is doing...
All single letters might as well be x to me, I really like my variables to be things like count or loop.
Agreed, one letter variables are mostly a bad idea, but I'm used to i and j as counters, so it really doesn't matter to me.
for(int i = 1;i < 5;i++) just isn't obvious.
Sure it is. If you suddenly see an i in a for loop, I almost never go back and check the loop declaration -- I expect that int i to be there.
Let alone the if then shortcuts.
If I write a sentence, I usually won't use the word then. I personally think the then syntax is clunky.
And for whatever reason C programmers usually get it into their heads that shorter source files produce smaller and faster executables. So in an attempt to make their code faster, they make it hard to read beyond the inherent complexity of optimization itself. And that %d%x%s is why I hate C.
Uh, shorter source files generally do produce faster code. The short part that I am referring to is the number of instructions. To take an example from this thread a while back -- "the whole thing would have been simple with three extra lines and another variable". But that's at least three more instructions, probably more, extra memory space, plus operations to store and manipulate that memory. So it's faster to write terse code.

This does ignore such things as order analysis of the program as a whole, and micro-improvements like this make little difference if you're in an order N^3 algorithm...
I should have been asleep hours ago. does it show?
No, you've stated your case well. For the most part, I agree with you.

Anyway, I have to get ready for work...
 
Complicated makefiles are sure sign of lacking frameworks. Let us examine a makefile needed to create an executable on OS X:
Code:
PROG= myprog
.include <bsd.prog.mk>
will convert myprog.c into myprog, and myprog.1 into manpage myprog.1.gz

If you need more source files:
Code:
PROG= myprog
SRCS= main.c file1.c file2.c
.include <bsd.prog.mk>
will do the work.

Both variants will take care of all the obvious dependencies.

Edit: code did not like &lt; and &gt;
 
Back
Top