You are not logged in.

#1 2011-09-20 23:50:53

karabaja4
Member
From: Croatia
Registered: 2008-09-14
Posts: 1,000
Website

How do you learn/code C (rant)

Hi.

Ever since I used Linux I had an argument with C programming language. Somehow I can't grasp it. I'm talking about something more than "gcc code.c -o code".

Lets start with most obvious thing: writing and compiling the code.

As I understand it, coding in C requires only gcc. So, I am supposed to write Makefile etc. all by my self? And the actual coding, edit each of the sources individually and just write code in a plain text editor?

Then, the compiling. I am supposed to know and manually include directories like /usr/src/linux, plus know what cflags to use, and the actual syntax of the compile command?

Let's say I want to make something useful, with a GUI. Then, I'm supposed to use GTK+.

But, now I am faced with all the problems from the above (but they got way more complicated because of using complex library). Also, I am supposed to know GTK+ syntax as well: commands like gtk_container_set_border_width or g_signal_connect_swapped or gtk_god_knows_what.

The thing about other high level languages (like C++/QT or C#) which helps alot, is *actual* code completion. It is there to help you learn the syntax of the commands. For example, in QT creator you can just write a name of an object, such as label1-> and it will give you a list of its members/functions, and then, after writing a "(" you can list through available functions for that object.

In C, I have to do all that by myself. If I'm using GTK+, for each object, I should go and read the documentation to know what to use, and there is enormous amount of things to remember. Even if I somehow remember all of them, if I begin to code something, I will probably open GTK+ documentation just to remind myself how it's all done. This is only for GTK+, it's same story all over again for cairo, xlib, etc.

So, what's the right way to code in C? Is using an IDE the answer? Note to that - I'm yet to find a complete C IDE that can be matched in features to, for example, QT creator. If not, how the hell should I remember and learn all that?

Last edited by karabaja4 (2011-09-21 00:04:58)

Offline

#2 2011-09-21 00:06:16

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 858
Website

Re: How do you learn/code C (rant)

Um, basically, if you don't know C, starting out by writing a GUI app is just going to frustrate you in all likelihood.  Get really good at "gcc code.c -o code", then move on to "gcc -Wall -O2 -g code1.c code2.c -o code", then "gcc -Wall -O2 -g -c code.c; gcc -o code code.o", then "make".

An IDE is not the answer, at least in my philosophy.  I think it helps greatly to understand everything that's happening "under the hood," even if you don't have to deal with it directly.  So you can use things like autotools to create your Makefiles for you, but you should know how to write one first.  And code completion may save some people time (I don't use it), but learn the API by reading the documentation.

It takes time to master, but I don't consider C a "throw something together quickly" language.  I write things in C when I care about the details that C forces you to care about.  If I don't care about those details, I write in C++ or Python or whatever high-level language is appropriate.

Offline

#3 2011-09-21 00:07:19

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: How do you learn/code C (rant)

Maybe that's why GUIs are written in C++, often in QT Creator, I suppose.

Offline

#4 2011-09-21 00:12:21

karabaja4
Member
From: Croatia
Registered: 2008-09-14
Posts: 1,000
Website

Re: How do you learn/code C (rant)

tavianator wrote:

Um, basically, if you don't know C, starting out by writing a GUI app is just going to frustrate you in all likelihood.  Get really good at "gcc code.c -o code", then move on to "gcc -Wall -O2 -g code1.c code2.c -o code", then "gcc -Wall -O2 -g -c code.c; gcc -o code code.o", then "make".

An IDE is not the answer, at least in my philosophy.  I think it helps greatly to understand everything that's happening "under the hood," even if you don't have to deal with it directly.  So you can use things like autotools to create your Makefiles for you, but you should know how to write one first.  And code completion may save some people time (I don't use it), but learn the API by reading the documentation.

It takes time to master, but I don't consider C a "throw something together quickly" language.  I write things in C when I care about the details that C forces you to care about.  If I don't care about those details, I write in C++ or Python or whatever high-level language is appropriate.

I can't say if I know C. I'm familiar with it, I learned my programming on it (used it on university, computer science), but I'm yet to write something useful in it. The learning curve just looks way to steep for me to climb...

Offline

#5 2011-09-21 01:14:53

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: How do you learn/code C (rant)

Start by writing simple things (solutions to programming problems/exercises, project euler, etc), then gradually move on to more complicated things as you improve. C is a very simple language, writing a basic Makefile is also very simple - when you know a little about them, this will come with practise.

Last edited by Barrucadu (2011-09-21 01:15:23)

Offline

#6 2011-09-21 01:27:26

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,797

Re: How do you learn/code C (rant)

All of the tools are just that; tools.  IDEs are not code generators.  They will not architect a program.  They should not be depended upon to build a skeleton.
Once you learn how to code, the tools can help you develop code.  They can help prevent typos.  They can reduce keystrokes.

You must learn the language before the tools can help.  Do not even try to use them as a crutch.
You do not need the make tools, the GNU autoconf or GNU autoconf tools to build simple C applications.   
You should limit yourself to the basic libraries for CLIs until you learn the language.

Once you know the language, and the basic (simple) libraries, then start looking at things like glibc.  GUI tools like GTK are completely out of the question until you understand the language.

Things like Qt, wxWidgets, and Gtkmm are for C++ after you learn C and after you grasp object oriented programming. 

How to learn C?  Read and completely understand everything in The C Programming Language (K&R) Brian Kernighan and Dennis Ritchie
Then, work your way through The C Puzzle Book Jeffrey Pratt to ensure you understand the concepts (especially pointers)

Walk first.  Then run with the C glib libraries.  Then try GTk.  Then tackle Object Oriented Programming, C++, Qt, Gtkmm, and wxWidgets

Edit: fixed tags

Edit: Oh, most importantly:  Use an editor like vi or emacs, and compile from the command line.

Last edited by ewaller (2011-09-21 01:32:28)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#7 2011-09-21 05:10:50

Murray_B
Member
From: Germany
Registered: 2008-07-29
Posts: 134

Re: How do you learn/code C (rant)

I am trying to learn programming in C, too. And yes, I feel the same like you, things like writing programs with GTK-GUI are difficult. I have the feeling, programming for Linux is more about reading library-documantation and finding things in the internet than really WRITING the code. But if you want to avoid those things, you have to use another language with GUI and other libraries included, say Java, Delphi, Visual Basic. Or a script language with "batteries included" like python.

I am just learning C for fun, because I want to program again (stopped with it maybe 10-12 years ago). I have a book, where I can learn from and I just search for C exercises in the internet and will try to solve them. There are already many of them in german (mostly from universities), and I think I will find much more in english. Maybe I write a page with those exercises. Or maybe we could yust start a thread here in the forum and ask for exercises, maybe there are already some, don't know. I think it will take a while until I can write my own mail-program or something like that, but I hope, I will have lots of fun until then.

Ah, another hint: There is an IRC-channel on freenode for C: ##c The people there seem to be very friendly, even if you ask some stubid newbie-questions. ;-)

I wish you good luck with learning C!

Offline

#8 2011-09-21 08:47:29

the_isz
Member
Registered: 2009-04-14
Posts: 280

Re: How do you learn/code C (rant)

Imho, an IDE will never replace reading documentation, not for C, not for C++,
not for any library or other language.

Yes, an IDE with auto-completion can help you type faster and maybe remind you
when you remember only part of a function or class name. But you still need to
look up things like:

  • Which classes are available?

  • How do classes interact with one another?

  • What values (and for dynamically-typed languages: what types) are valid for
    a certain function parameter?

That being said, I tend to always have documentation open (e.g. Qt Assistant for
Qt) for when I need it. I use Vim to auto-complete on a textual basis, which
works regardless of edited source (C, C++, Latex, python, whatever). The more I
code with a given language or toolkit, the less I need the reference. And I
think, that's what programming usually works like.

Offline

#9 2011-09-21 12:23:35

inch
Member
Registered: 2010-12-21
Posts: 49

Re: How do you learn/code C (rant)

Oh, maybe we should have a C thread here smile

+1 for the Kernigham and Ritchie C book, it's really well written and probably one of the best starts into the language. Also recommendable is cprogramming.com.

Offline

#10 2011-09-21 19:20:45

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: How do you learn/code C (rant)

The main problem here is that it not suffices to know plain C alone (which is quite easily mastered) but need to know the basic functionality of the glibc standard library at least (which might prove a formidable task for a beginner). No development tool whatsoever does relieve you from that.


To know or not to know ...
... the questions remain forever.

Offline

#11 2011-09-21 22:29:01

ninjaaron
Member
Registered: 2010-12-10
Posts: 296

Re: How do you learn/code C (rant)

karabaja4 wrote:

Let's say I want to make something useful, with a GUI.

There's your problem.  If you want it to be useful, skip the gui.

tongue

Offline

#12 2011-09-21 23:03:07

karabaja4
Member
From: Croatia
Registered: 2008-09-14
Posts: 1,000
Website

Re: How do you learn/code C (rant)

So, the conclusion I made from your posts is that there's no easy way about it. Reading few C books would be beneficial, and that's something I *never* did. I did learn C in college, and have written simple, assignment applications, but it seems that's not nearly enough to be able to begin writing real-world applications. Read books and work my way up... ewaller has put it together nicely.

Also it seems that learning C to the point where I can freely make applications in it would require large amount of free time, which for me is in short supply these days...

So, until I can fully dedicate myself to learning C completely and thoroughly, I'll just stick to higher-level languages like Python and QT to do my (express, on-need-basis) programming.

Last edited by karabaja4 (2011-09-21 23:19:05)

Offline

#13 2011-09-22 07:27:46

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: How do you learn/code C (rant)

ninjaaron wrote:
karabaja4 wrote:

Let's say I want to make something useful, with a GUI.

There's your problem.  If you want it to be useful, skip the gui.

tongue

I don't get why people shun GUIs.

I don't use them myself but 90% of the world does, so if you don't want to alienate everyone you are pretty much required to make a GUI.

Unless you are joking, which is fine.

big_smile

Offline

#14 2011-09-22 08:33:05

the_isz
Member
Registered: 2009-04-14
Posts: 280

Re: How do you learn/code C (rant)

karabaja4 wrote:

So, until I can fully dedicate myself to learning C completely and thoroughly, I'll just stick to higher-level languages like Python and QT to do my (express, on-need-basis) programming.

Every language has its quirks and difficulties and requires you to get
acquainted to it, not just C. But if I may add this: You should choose the
language you're using based on the job you want to do. Using C for writing GUI
applications is - in my opinion - wasted time.

C is a rather low-level programming language compared to ones like Java or
Python. That means, it gives you more tools to tweak your program's internal
behaviour (e.g. memory management) like few others would (e.g. Assembler), but
on the downside also requires you to think about this stuff all the time.

When your goal is to write GUI applications that don't care about using the last
bit of processing power of your machine (which is true for many applications),
you're far better off using a language that takes care of the low-level tasks
and let you concentrate on other things.

And even if your application does need a lot of processing power, it is often
used in a few functions that can be easily put into a C library which is then
called from a GUI program written in a higher level programming language.

So my advice for you would be: Prefer learning a high-level programming language
like Python and get used to working with a GUI toolkit you like (Qt or GTK come
to mind). Then, if you come across a task that actually requires you to use C,
learn C and profit from everything you've learned and used in the meantime.

Offline

#15 2011-09-22 21:34:12

Cyrusm
Member
From: Bozeman, MT
Registered: 2007-11-15
Posts: 1,053

Re: How do you learn/code C (rant)

ninjaaron wrote:
karabaja4 wrote:

Let's say I want to make something useful, with a GUI.

There's your problem.  If you want it to be useful, skip the gui.

tongue

Ha, indeed. GUI's may be pretty, but generally they can be skipped, and you'll probably end up with a better/faster/more reliable program. In my experience, all GUI's do is give you a big complicated chunk of code that is probably going to be a giant bug-ridden nuisance.

I've become familiar with C by first, writing a lot of programs. especially the small simple programs "just to try something neat out."  It's slow going at first having to dig
through pages of documentation to find something that you need. However as you gain experience with the libraries involved, it will begin to just come to you over time.
To learn a language you must start out slowly and keep practicing, just like with any language.  you can't pick up a "Spanish for dummies" book and expect to write the lexical equivalent to "Don Quixote" in a week (or even a year, or probably a lifetime).

after some study, you will find that C is a super simple language to use, documentation becomes easy to search through, and you will begin to develop your own personal library of functions and snippets that you use the most in your programs thereby further accelerating your code output and increasing your project complexity.

Along with writing your own programs, I also suggest reading other peoples code to see how they put projects together.  Find some C based program on github or whatever, check out the code, and start playing around with it.  I think that is the best way to really learn some of the tricks involved with using multiple files, headers, pre-compiled libraries, and pre-processer directives (which you can do some AMAZING things with).

so in summary: quit crying about it, crack a book, and start typing some code already. smile


Hofstadter's Law:
           It always takes longer than you expect, even when you take into account Hofstadter's Law.

Offline

#16 2011-09-22 21:49:07

ibrunton
Member
From: Canada
Registered: 2011-05-05
Posts: 270

Re: How do you learn/code C (rant)

This summer I decided to start learning C, and I haven't actually found it different from learning anything else (HTML, CSS, JavaScript, Java, Perl, Lisp).  There's always looking up stuff in the documentation.  I think C actually has an advantage there, because anything in the standard libraries is available in the man pages.  I learned how to construct a Makefile by reading tutorials on the web and by looking at Makefiles in open-source projects.  To be honest, I thought this was a major strength of C -- it forces you to learn about a lot of the steps of programming and compiling that higher-level languages hide from you.

Offline

#17 2011-09-23 04:16:18

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: How do you learn/code C (rant)

I suggest looking at some of the C code at suckless.org.

Offline

#18 2011-09-23 04:36:18

Basu
Member
From: Cornell University
Registered: 2006-12-15
Posts: 296
Website

Re: How do you learn/code C (rant)

Please keep one thing in mind: C was supposed to be a systems programming language. It's meant to be used for writing fairly low-level, high-performance code. Things like basic operating system utilities, servers, device drivers etc. should be in C. You seem to be more interesting in writing user-facing applications with nice interfaces that solve interesting problems. I do not think C is the best choice for that. I would suggest that you think carefully about *what* kind of problems you want to solve and then pick the language accordingly.

Also, if you want to learn programming in general, C is also not a good place to start unless you want to learn about computers from the bottom up (starting with the hardware and then moving into applications).

Remember that languages are tools -- just as you reach for a hammer or a screwdriver depending on what your problem is, reach for a language based on what program you want to write. I can give more detailed examples if you would like but I now I must go to sleep so that I can write a TCP proxy in C tomorrow morning (I'm not kidding).


The Bytebaker -- Computer science is not a science and it's not about computers
Check out my open source software at Github

Offline

#19 2011-09-23 17:02:00

mike_r
Member
Registered: 2009-01-22
Posts: 114

Re: How do you learn/code C (rant)

In the sciences, it is very typical to use a high level language (e.g. I use python/numpy) for almost everything like GUIs, file access, etc. Then where there is a real bottleneck like a compute-intensive inner loop, pass the data to a C routine to do the crunching, then pass the results back to the high level stuff. In this way, you get the ease of high level languages, GUI toolkits, IDEs, etc. for the bulk of the work, but the performance of C when you really need it.

So it is the usual advice in the end - use the right tool for the job. Though I will grant that figuring out the right tool may be hard; it's hard to tell the difference between the handles of a hammer and a screwdriver if you're in the dark ... there is no substitute for experience for figuring out the right tool.

Mike


Linux User #353 - SLS -> Slackware -> Red Hat -> Mandrake -> Fedora -> Arch

Offline

#20 2011-09-24 02:58:48

synorgy
Member
From: $HOME
Registered: 2005-07-11
Posts: 272
Website

Re: How do you learn/code C (rant)

I too am currently learning C, if for no other reason than a lot of higher level languages inherit a lot from C and C++ (Java & PHP come to mind). I've got no magical solution, but the one that I'm testing out now is simply finding a *VERY* small program that solves a problem for you and tweaking it to make it perfect for your usecase.

For instance, I'm a dwm user (that isn't yet conversationally fluent in C - we do exist wink ). I like my dwm statusbar to be populated with useful information. For about 6 or 8 months now, I've been using a personal fork of statnot because I'm good in python already and it intercepts dbus messages - which is just frickin cool. However, I found the dwmstatus project over at suckless and realized that I was wasting cycles just for the convenience of writing it in python, so I'm reimplementing my personal changes from statnot into dwmstatus. These mostly consisted of reading files from /proc or running commands and parsing output. Nothing *HARD*, but not within my realm of C syntax knowledge at the time. Since I had most of the stuff already written, I was focused on learning how to "C-ifiy" my code.


"Unix is basically a simple operating system, but you have to be a genius to understand the simplicity." (Dennis Ritchie)

Offline

#21 2011-09-24 10:20:47

xamaco
Member
From: Corsica, France
Registered: 2010-04-05
Posts: 87

Re: How do you learn/code C (rant)

I admit it shamelessly: I'm a C lover. In my (biaised) opinion, you cannot write good C if you have'nt, at least, some knowledge about assembly. And that's it. C is what allowed Uni? (the x would be a trademark infringement) to run on every platform ever built. A kind of lingua franca. If you know well assembly and C, you can almost predict which assembly code your C program will produce...

In the times where computers had very little RAM and CPUs were very slow, writing in C gave you a significant advantage (not speaking about writing directly in assembly, which was even better). Now, unless you write things like device drivers, kernels, and other very metal-close things, using C is  a loss of time. Most of the times, now, I use a so-called script language (Python, or much better, my second best love after C: Perl). If you really need at times to be close to the metal, you can mix-in a bit of C code in them.

As for Oo languages, as a programmer, I lived through a time where it was thought they would be the universal panacea. I believed in this credo for a time. And it's true that Oo languages are very good at expressing several things (like an user interface: QT for ex.). But they can also be un-necessary complicated for other things.

The beauty of C resides in its simplicity. Its reference book (K&R: C programming) is about 170 pages, plus appendixes. With these few pages, you can teach any computer to dance anything you want. Pure beauty, in my eyes.

Offline

#22 2011-09-25 18:30:47

inch
Member
Registered: 2010-12-21
Posts: 49

Re: How do you learn/code C (rant)

xamaco wrote:

In the times where computers had very little RAM and CPUs were very slow, writing in C gave you a significant advantage (not speaking about writing directly in assembly, which was even better). Now, unless you write things like device drivers, kernels, and other very metal-close things, using C is  a loss of time.

Well, no. If you want to write programms high performance and low memory footprint applications, C is always the way to go.

Offline

#23 2011-09-25 18:55:08

Cloudef
Member
Registered: 2010-10-12
Posts: 636

Re: How do you learn/code C (rant)

C is the language I enjoy most, just my two cents.
But like most here say, GUI's are better for HL languages, I myself use python for this task while doing the core functionality in C.
There are also times when I play with Qt, but it's really only playing that I do.

But yeah, I pretty much use C for everything else.
About the documentation, I would highly recommend keeping always docs open while you code. If the documentation is any good, they can give you lots of details about inner workings of code.

Last edited by Cloudef (2011-09-25 18:57:24)

Offline

#24 2011-09-25 20:21:27

Grinch
Member
Registered: 2010-11-07
Posts: 265

Re: How do you learn/code C (rant)

Well, I'm another one square in the C camp. Although I've done a good deal of development in C++ and some in Python, after all these years C still remains the language I feel most comfortable with. Part of it is likely because it more closely mirrors how I think, as in not so much 'abstract' terms which suits C++ and even higher level languages (maybe it's the assembly programming I did in my youth which has scarred me big_smile ). I've seen fellow programmers spend days perfecting their class hierarchy/template structure to be as flexible as possible 'just because they must generalize everything!' it's a sickness I tell you wink

Meanwhile I can write my version in 5% of that time, it won't be flexible (unless it's a prerequisite) but it will be fast and get the job done, and a million times easier to debug. Obviously this is purely anecdotal and refers only to my own experience, so take what I say with a large grain of salt! Please don't hurt me! big_smile

That said, recently I've started to warm up to Go, it sort of sits between C and C++ and is syntatically very clean with some nice things I miss in C like interfaces, multiple return values, duck-typing, composite literals, defer..etc which all help to make the code syntax cleaner while not skimping on the control you have. I would have preferred optional automatic garbage collecting rather than enforced, but I can see their rationale for choosing the former. Anyway, I've merely played with it sofar but I'm considering moving one of my indie game projects over to it if I find that the Go sdl library bindings are functional enough. If you like C,C++,Python, I'd suggest you give it a try.

Offline

Board footer

Powered by FluxBB