r/cprogramming • u/TurtleSlowRabbitFast • 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
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
ais and makes makes a pointer to it. Since we assign result of this operation to variableptrwhich is a pointer to int,aclearly 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 variableptrtherefore ptr is an int pointer.A more complex example that gets many: function pointer declarations:
int (*mypointer)(int, short);You read it as such:
intis what you get after you takemypointervariable, dereference it, and then call it with two arguments:intandshort. Thereforemypointeris 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;