r/cprogramming • • 20d ago

Would learning C as my first programming language also be useful for picking up other languages later on? What are your thoughts on this course?

Wanting to learn C as an intro into software engineering. I know it’s low level and I’d like to eventually switch over to high level languages such as JS/TS, Java, and GO.

Does the “The Complete C Programming Bootcamp” course by Byte Garage be a good choice to learn from?

35 Upvotes

54 comments sorted by

View all comments

Show parent comment

1

u/70Shadow07 19d ago

I wrote a comment briefly explaining that as a reply to the user ZubSero in this thread. Read that one first, if you have more questions after that and need me to clarify I can do that.

I'll add few more things to that comment just in case.

Let's separate few concepts first so it's more clear.

In int *ptr = &a; line there's quite a lot going on. It's both a declaration of a variable and taking address of another variable and an assignment. But this line is basically equivalent to these two:

int *ptr;
ptr = &a;

The second line is pretty simple, it just takes whatever variable a is and makes makes a pointer to it. Since we assign result of this operation to variable ptr which is a pointer to int, a clearly must be of type int. This part should not be particularly surprising or difficult to grasp, the devil is in the declaration part. (the first line)

Many novices (virtually all to be honest) trip on this, C type declarations are pretty unintuitive. You don't really specify a type directly, you specify a usage of the variable instead. It's much easier to understand on more difficult examples, as ones I wrote in the aforementioned comment, but the gist of it is:

All C declaration have 2 parts: resulting type on left hand side (in our case it was int) and expression with a named variable on right hand side (in our case it was *ptr)

The logic is the following: int (resulting type) is what you get after you do a single pointer dereference operation on variable ptr therefore ptr is an int pointer.

A more complex example that gets many: function pointer declarations:

int (*mypointer)(int, short);

You read it as such: int is what you get after you take mypointer variable, dereference it, and then call it with two arguments: int and short. Therefore mypointer is a pointer to a function taking int and short parameters and returns int.

The confusion kinda originates from the fact that C is whitespace insensitive so if you slap the star on the resulting type it still reads it correctly.

This logic can be further verified beyond shadow of the doubt when you use comma declarations. Comma works on the expressions, not on the resulting type when declaring.

int* foo, bar; //OR int *foo, bar;

This line declares foo as int pointer and bar as just int. (because star binds just to foo variable) If you wanted to double declare two pointers you would need to write this:

int *pointer, *pointer, this_is_just_int, **this_is_pointer_to_a_pointer;

2

u/Previous-Box2169 19d ago

I think I got it at the function pointer declaration example! So the syntax highlights that you're getting a variable of that type (say int) after you dereference the pointer, and since you dereference the pointer through the star (*) you should attach the star to the pointer's name (ptr). So the declaration reads "declare a pointer ptr such that, when dereferenciated, becomes an int variable". That's how "int *ptr" makes sense to me now. Now, the "= &a" part is tricky because it looks like normal assignment but it's not making *ptr equal to &a. Instead it's making ptr equal to &a. So you can't really read the whole "int *ptr = &a" and expect it to make sense. It only makes sense as "int *ptr; ptr = &a;", am I right?

2

u/70Shadow07 19d ago

Yes. You are correct, you need to parse the int *ptr = &a as different syntax that combines declaraction and assignment into one line. you are assigning &a to ptr variable not to *ptr.

1

u/Previous-Box2169 19d ago

You have been extremely helpful and I must thank you for your time and patience. Since we're at it, are there any other "complex" syntaxes in C that I would need to parse first in order to understand it? Anything that comes to mind. I'm thinking the creators of C might have condensed two different things together (like in this case) for efficiency purposes (definitely not for readability!). Also if you don't mind suggest one good source to learn C from, as I'm currently doing most of the job being tutored by AI

2

u/70Shadow07 19d ago

You welcome, I am glad i could help a lost soul navigate this topic hahaha

Truth be told you might be giving creators of C programming language a bit too much credit - the self-reflective declaration syntax is widely considered as one of C's worst sins and I think even creators admitted that it's bad. (Function pointers are bad alone, but you will snap when you see how to define a function that returns a function pointer) There is a reason no other C-style language made since does types this way. (I admit though that I kinda vibe with the atriociously unintuitive C declarations cuz they seem so magical once you finally get them)

I can't think of any syntaxes in C that are more complex than C variable/typedef declarations. Aside from this little thing, most constructs in this language are pretty simple. If I had to direct you to niche syntax things worth knowing maybe, then check out "compound literals", "anonymous structs/unions" and learn what *ptr++ does as its an idiom quite often used by some programmers.

When it comes to footguns and mind-bending shit then you should probably know a bit about, then it would be "strict aliasing" optimization (and what unexpected undefined behaviour it can cause and how to turn it off in compiler flags so you can sleep at night)

Unfortunately there's no 1 god source to learn this language from and a lot of publically available info is very low quality. You need to dig deeper and learn computer fundmanentals a bit too, but its not too bad.

I once wrote a comment about learning resources as a reponse to someone in similar situation as yours so I will let myself just give you the link to it and not repeat myself.

https://www.reddit.com/r/cprogramming/comments/1nyufor/comment/nhxzpmy/?context=3

1

u/Previous-Box2169 19d ago

Hearing someone call C declarations "atrociously unintuitive" is a relief, I thought I was loosing my mind! To be honest it's a shame all the online resources are low quality as one would think that such an old language would have high quality learning sources by now. I'll take a look at the links in your other post and the more obscure syntaxes you talked about. Thanks again and have a nice day (or night)

2

u/70Shadow07 19d ago

Not *all* resources are bad. Some are really really good, it's just hard to separate gold from the weeds. Especially when you are starting out and do not know where to look. By "no 1 god source" I meant that theres no single definitive source, the knowledge is fragmented over various websites, blogposts, conference talks and books.

Either way, good luck on your quest for knowledge.