DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 30th May 2021
HopingStar HopingStar is offline
Port Guard
 
Join Date: Sep 2020
Posts: 12
Question What is the way old Unix users learn C?

It's been a very long time since I've been back here.

I really love BSD and decide to play with it more. I have learnt Go after a long time struggle in C and now I am going back to end my learning C journey. But I always fail to understand it, it kind like a beast.

How can you guys learn C from the beginning?
How can you improve it?
Are there some good sources you want to recommend to me? Also, I still wonder about the C standard which one should I learn first?
And I meet some people who still prefer old C standard rather new standard. What wrong with new C standard?

I have made a mess in my C journey from the beginning I guess. Now I am trying to solve it.
Reply With Quote
  #2   (View Single Post)  
Old 30th May 2021
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 992
Default

Since your post mentions old stuff ...

When my dad got his first computer (an IBM-XT clone with a 10 MB hard disk) in the late 1980's, the vendor threw in a few computer books for him as a sort of bonus. Some of them were about C, but they weren't of any interest to my dad, so they kind of drifted into my hands.

As I began to read them, I can remember several attempts to learn C that ended in giving up for a while, only to come back later. So I can understand where you may be at. It can be hard at first, and it didn't help that my mind had been polluted with Fortran, perhaps. I'm not sure if Go would be more or less helpful. I should add I was never a computer science student or professional programmer. Just an amateur/hobby thing.

One of the books was "The C Programming Language" by Kernighan and Ritchie. I would recommend it. It's an oldie pretty much from the horse's mouth, and quite good. Since it is old, it's from before all the C standards were finalized. This may be a very good thing for someone starting out, because you really don't need to worry about that too much just to get a feel for how things work.

Eventually I read through that book and worked a lot of the problems in it. Learning by doing, also recommended. If you can come up with small project ideas of your own to write in C, that also helps, because you'll want to do them, it won't be "just homework".

Probably someone can recommend a good more modern book. The other thing you can do, once you've read a chapter and learned about some new group of functions in the standard C library, is to look at the BSD manual pages (and/or Linux) for these functions, and compare them with your book. The man pages will give an up to date description of these things as used by practical code --- such as the very OS on your computer.

So keep at it, and good luck!

Last edited by IdOp; 30th May 2021 at 11:46 PM. Reason: grammar
Reply With Quote
  #3   (View Single Post)  
Old 30th May 2021
ibara ibara is offline
QEX author
 
Join Date: Jan 2014
Posts: 774
Default

Books:
K&R C, 2nd edition (updated for ANSI C!)
If you're specifically interested in Unix and C: APUE, 3rd edition.

I suppose they're not "easy," but they routinely come up time and again as quintessential books for Unix and C.

But... when working through the books, give yourself permission to write your answers in what I'll call "C99+" (which I'll explain a little later).

How:
Same way you learned Go, I think. @IdOp's suggestions are good ones. I'll add one more: do you have any Go programs you wrote yourself? Try porting them to C. You can trace your own thinking and see how you solve similar problems in the two languages. It does no harm to "write C in terms of Go" when first starting out. As your C improves, it will tend towards more idiomatic C and less like someone who knows Go and is mapping their Go knowledge onto C. But until then, nothing wrong with that.

C standard:
If you are using a modern clang or gcc, then it will default to the C17 standard (more precisely: it defaults to the GNU17 standard, but for now just starting out that's a difference that won't make a difference). You can see what version of the standard GCC is using by compiling with the -v flag. See this, for example with a simple hello world program compiled with gcc-12.0.0 on OpenBSD/amd64:
Code:
/home/brian/c $ gcc -v hello.c
...
GNU C17 (GCC) version 12.0.0 20210520 (experimental) (x86_64-unknown-openbsd6.9)
...
If you were to change the standard level using the -std= flag, you'll see that the output changes:
Code:
/home/brian/c $ gcc -std=c99 -v hello.c
...
GNU C99 (GCC) version 12.0.0 20210520 (experimental) (x86_64-unknown-openbsd6.9)
...
Clang unfortunately does not have that information in its -v flag, but according to their website, they claim defaulting to C17: https://clang.llvm.org/compatibility.html#c

I would learn what I call "C99+," which is C17 but without you feeling like you need to use C11 and C17 features. Stick with a baseline of C99 and add C11 and C17 features as you want to and as make sense for your programs.

Fortunately, C is a slow-moving language and cares a lot about backwards compatibility. With some important exceptions, notably gets() and variable-length arrays, you can be confident that your C99 code will compile without issue in C11 and C17, and the future C2x standard. I would say don't worry so much about "which standard does this adhere to" and more "how do I solve problems with C." If you ever get paid to code to a specific, older, C standard, knowing how to solve problems with C will make adhering to the letter of the law easy.

What is wrong with newer C standards:
Nothing. Some people are just curmudgeonly. I suppose to give those people the benefit of the doubt, they may be working with codebases on platforms that require the use of older standards, but that's not the same as there being something "wrong" with C11 or C17. Again, learn "C99+" and you'll be fine here: you'll be able to converse just fine with the curmudgeons and you'll enjoy the enhancements of the newer C standards.

Last edited by ibara; 30th May 2021 at 10:40 PM.
Reply With Quote
  #4   (View Single Post)  
Old 30th May 2021
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 992
Default

Quote:
Originally Posted by ibara View Post
K&R C, 2nd edition (updated for ANSI C!)
Excellent! I think I have the first edition, it's copyright 1978.
Reply With Quote
  #5   (View Single Post)  
Old 31st May 2021
Prevet Prevet is offline
Package Pilot
 
Join Date: Oct 2017
Posts: 188
Default

C can be a beast if you want it to be. It can be three languages at minimum.
You have C, then you have macros, and then you have to learn multiple
build systems because people keep making up new ones.
Some people make up build systems just for a project they
are working on...

I didn't read any books to learn C, I just did a one semester course,
so perhaps you can download a course free from a university?
I am reading books now to see if there is anything I missed.

I think they may not like the new C because it is starting to
look verbose like C++.
C is nice because it looks simple and tidy.

How would I improve learning C?

Just learn the basics and start making programs like you would scripts.
Learn if statement, for statement, while statement, etc.
Learn variables, arrays, and strings.
Learn how to do math, how to read data in and print it back out.
Learn pointers.
Find some very simple programs and try to understand how they work.
When you can do that mod them do something else.
No macros or make files until you know C.

On OpenBSD to build a program I just add the main function to a file
add some code and then 'make file_name' without the .c extension and it builds.
On github people are making single header libraries so they don't have
to deal with build systems at all. The just hash include the headers and compile without make files.
Search on "single header C libs github"

To debug programs you will need a debugger. You should probably make it
easy on yourself and get one with a GUI.
To debug you will need to know the correct compiler flags for debug

Last edited by Prevet; 6th June 2021 at 10:03 AM.
Reply With Quote
  #6   (View Single Post)  
Old 31st May 2021
HopingStar HopingStar is offline
Port Guard
 
Join Date: Sep 2020
Posts: 12
Default

Thanks guys. You guys have a great answers.

Also, will you use C over everything? I don't know how to say that, but will it better if I only keep my C knowledge in write kernel, drivers and low lelvel stuff instead of writing software? C is a beast some people tell me that language is a real beast if I don't want to have more troubles I shouldn't use it much. They said to me "Memory management is a thing you should worry"

Last edited by HopingStar; 31st May 2021 at 03:10 PM.
Reply With Quote
  #7   (View Single Post)  
Old 1st June 2021
Prevet Prevet is offline
Package Pilot
 
Join Date: Oct 2017
Posts: 188
Default

Quote:
Originally Posted by HopingStar View Post
Thanks guys. You guys have a great answers.

Also, will you use C over everything? I don't know how to say that, but will it better if I only keep my C knowledge in write kernel, drivers and low lelvel stuff instead of writing software? C is a beast some people tell me that language is a real beast if I don't want to have more troubles I shouldn't use it much. They said to me "Memory management is a thing you should worry"
If someone is telling you that memory management is too hard, they are telling you they are a not a good programmer, so you can stop listening to them.
I recommend you seek out very good programmers and learn from them. These days its easy to find good programmers to learn from, as they are on the Internet.

Memory management isn't hard when you know how a computer works.

This guy uses C for everything he does. So it is possible.
https://www.youtube.com/watch?v=eAhWIO1Ra6M

Last edited by Prevet; 6th June 2021 at 10:03 AM.
Reply With Quote
  #8   (View Single Post)  
Old 1st June 2021
ibara ibara is offline
QEX author
 
Join Date: Jan 2014
Posts: 774
Default

Quote:
Originally Posted by HopingStar View Post
Also, will you use C over everything?
You should use the right tool for the job. It is clear that over the 50+ year lifespan of C, it has been used for just about everything. Modern C certainly can be used to solve an extremely wide array of problems. It should be just one tool in your ever-growing toolbox.

Notably, none of the BSDs use C for everything: OpenBSD uses Perl for its package manager, FreeBSD famously used Forth for its bootloader, and NetBSD has Lua support in-kernel for rapid prototyping (though this last one is still listed as experimental).

Quote:
Originally Posted by HopingStar View Post
I don't know how to say that, but will it better if I only keep my C knowledge in write kernel, drivers and low lelvel stuff instead of writing software?
C can and is being used to write general-purpose and highly specialized software all day everyday. I've used C to write general-purpose software. But I have also used languages that are not C to write general-purpose software as well. Again, choosing the right tool for the job is important.

Quote:
Originally Posted by HopingStar View Post
C is a beast some people tell me that language is a real beast if I don't want to have more troubles I shouldn't use it much. They said to me "Memory management is a thing you should worry"
Manual memory management can be an issue, but learning how to use C properly can help. That's going to be true of any programming language though. There was a recent article by the curl lead developer about how C mistakes only account for less than 1% of total bugs in curl's lifetime, and C mistakes are caught and fixed significantly more quickly than other bugs. Make of that what you will: https://daniel.haxx.se/blog/2021/03/...re-c-mistakes/

As @Prevet mentioned, C can be difficult to learn due to the fact that you are really learning two languages: C preprocessor and C. Not to mention the myriads of build systems (stick to good old make when starting out). This macro processing in C in the form of the C preprocessor needs to be taken in historical context: such macro preprocessing would not have been seen as so unusual back in those days, as it grew out of assembler macro preprocessors. With that said, when first starting out, with the exception of #include directives and potentially some simple #define directives to make your life easier, you can minimize your use of the C preprocessor and therefore focus on just learning C. Your early C code is highly likely to be exemplary of C's original "portable assembler" ethos anyway, which can also help limit your C preprocessor use.
Reply With Quote
  #9   (View Single Post)  
Old 14th June 2021
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Tcpdump Spy
 
Join Date: Apr 2008
Location: Indonesia
Posts: 2,236
Default

As a general comment on "how to learn a language", the best way is usually to *use it*.

You can read all the books about carpentry or guitar playing you want, but this does not make you a good carpenter or guitarist. Indeed, some of the best carpenters and guitarists have never read a book on the topic at all and just acquired their skill through practice.

That being said, of course it does help if you have a good book which teaches some fundamentals.

The K&R book was mentioned; it's a classic, although there maybe are better books out there. Just start reading the book and:

- Do the exercises in the book! No really, do them!

- Muck about with the exercises. "Gee, I wonder what happens if [..]" or "maybe it would be nice if this example program could also do [..]".

- There are also additional exercises available at e.g. https://exercism.io/tracks/c I don't usually enjoy "programming for the sake of programming" myself (some people do), but when starting out these things can be quite useful. Like it said: it's all about actually *doing it* to practice.

- Think of a real-world actual program you want to write as soon as possible, perhaps even before you start any of this. And try to *write* this program as soon as possible. You don't need to finish the book first, just as soon as you think you might have a vague idea how to do it, start mucking about while also continuing to read the book. This is often the hardest part; not the writing of the program, but thinking of something useful to write!

Everyone is different of course; and some other ways work better for some other people. But I found this works quite well for me personally.

All of that being said: if you don't really have anything useful to write in C, then it's probably a lot harder than if you do. Personally I'd say that learning C just "for the sake of knowing C" is kinda useless, and not very motivating.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
Old 15th June 2021
frcc frcc is offline
Don't Worry Be Happy!
 
Join Date: Jul 2011
Location: hot,dry,dusty,rainy,windy,straight winds, tornado,puts the fear of God in you-Texas
Posts: 224
Default

Could not have said it better..............................
Reply With Quote
Old 15th June 2021
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Tcpdump Spy
 
Join Date: Apr 2008
Location: Indonesia
Posts: 2,236
Default

There's another thing I forgot: takes notes!

When you're working through the book just keep a ~/c.markdown file (or whatever works for you) where you explain the core concepts in your own words. Actually, I wrote Learning a programming language on my website a few years ago which mirrors most of what I said in my earlier post, which also contains a small example from back when I was learning Go.

These notes aren't really intended to be referenced later *as such* (although they can be), it's just that the act of writing down stuff in your own words (don't copy/paste it) helps your brain store the knowledge better. I discovered this far too late in life; they told me this at school, but I had an easy time there and never needed it, and was a stubborn little bastard too, so I didn't believe them. But they were right!

Writing down notes is a useful thing to understand stuff in general; when I'm working on some code I don't fully understand (e.g. projects I never worked on before, like an open source project I want to submit that one patch to) I will often write down extensive (temporary) comments where I try to construct how it all works.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
Old 16th June 2021
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 992
Default

Quote:
Originally Posted by Carpetsmoker View Post
Writing down notes is a useful thing to understand stuff in general; ...
Agreed! It can force you to think everything through from scratch, without just taking whatever has been written by someone else at face value.
Reply With Quote
Old 24th June 2021
HopingStar HopingStar is offline
Port Guard
 
Join Date: Sep 2020
Posts: 12
Default

Those are great tips! Thanks. By somehow I have understood some parts of C programming which I have never understood it before and it is quite clear to me. All of your advice help me to fill the gaps in my knowledge. I bet my C programming knowledge will be better and clearer in someday. Right now, I am learning hard to pass my national high school exam, I shouldn't go to Internet and try to learn C. But I will try my best to pass it to prove that I have improved myself day to day.

I will tell you guys after I have passed national high school exam and done my C programming course. Wait for me.
Reply With Quote
Old 24th July 2021
HopingStar HopingStar is offline
Port Guard
 
Join Date: Sep 2020
Posts: 12
Default

I think I have known C better than before. I know the basic enough so that I could read source code and understand it. I have started ported my first project and it is worked pretty great. I still have a lot of problems through but I will fix my project later.

I guess everything need time so that I can understand how thing work. Everyone can learn C maybe in 1 day or maybe 1 weeks but I guess they have to practice it all the rest of their life. Programming is just like other normal activities if you practice it more you will be better. Not sure how long I can master it. But I am happy that I have escaped of my learning loop and start a new journey for me.

Thanks guy.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Learn OpenBSD ports! ibara OpenBSD Packages and Ports 6 8th August 2014 02:32 PM
learn assembly ephemera Book reviews 5 26th December 2012 06:29 PM
How to learn to program under BSD? Sunnz Programming 5 24th December 2008 11:45 PM
Would BSD be right to learn networking? php111 Off-Topic 17 25th September 2008 07:02 PM
How did you learn to program? TerryP Off-Topic 25 6th September 2008 04:00 PM


All times are GMT. The time now is 07:45 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick