:: Re: [DNG] Learning C
Etusivu
Poista viesti
Vastaa
Lähettäjä: Steve Litt
Päiväys:  
Vastaanottaja: dng
Aihe: Re: [DNG] Learning C
o1bigtenor via Dng said on Mon, 23 Sep 2024 21:05:02 -0500

Hi o1bigtenor,

I'm going to address your question of how to best learn C in 2024,
rather than your problem with the compile command:

* Compile and run a Hello World program (you've done this already)

* Use #define to define HELLOWORLD as "Hello World\n", and
printf(HELLOWORLD);

* Add a for loop that counts an integer and prints that integer along
with a string.

* Change the preceding for loop into a while loop.

* Within the loop, add an if statement.

* Add an else statement

* Add an else if statement between them.

* Declare and define a string variable.

* Create a pointer equal to that string variable.

* Iterate through the string without accessing memory outside the
string.

* Create and use of function with no arguments and no return.

* Add one or more arguments to the function.

* Have the function return something.[1]

* Study, use and understand strlen()

* Study, use and understand strncpy(), strncat(), and why you ALWAYS use
the form with an "n" in it.

* Study the relationship between arrays and pointers, and use them in
code.

* Create and use a struct without declaring a variable.

* Create and use a typedef for the struct.

* Create and use a struct that declares a variable.

* Pass the address of a variable into a function as an argument
so that the function can directly change the variable.

* Study and use malloc() and free().

* Study and use sizeof(). This is vital to prevent buffer overruns.

* Understand the difference between malloc() and calloc(), and when to
use each.

* When you get this far you'll be able to web search how to do other
things you want such as pointers to functions. Also, when you get
this far, it's time to start reading other peoples' code and asking
why they do things a certain way.

Each of the preceding steps is very easy to web-research and to add.
Most of your learning will come from the fact that you do each one
wrong the first time, and gain insights and distinctions debugging
them. No book necessary. No tutorial necessary.

I'm not concerned about C versions. If it has function prototypes and
can comment with either /* */ or // , I'm satisfied. By the way, I
started with K&R version 1, which had neither of those, and it was a
pain in the ass.

And like you've been doing, ALWAYS compile with -Wall, and address
every single warning. Any warning is likely to produce a wrong answer,
a memory leak, a buffer overrun, a crash, or something else nasty, so
fix all warnings ASAP.

I personally use GNU gcc. It comes with Linux. But I used to use Turbo
C, and I've used Microsoft C, and Whitesmith C, and Vax C, and they're
all good if they're less than 30 years old.


[1] Never return any kind of local array or string, or you'll have
nasty intermittents. I'll explain why later: It has to do with scope
and stack.


SteveT

Steve Litt

http://444domains.com