Picked C as a first language and here is everything so far
Posted by Tolba69@reddit | learnprogramming | View on Reddit | 19 comments
So for a while i have been quite interested in mostly linux topics and whatever beep boops that float around the internet nowadays but i have a problem
I dont have a PC and im not getting one until like 4 months or something but i didnt wanna stop here
I downloaded termux and a 4 hour tutorial for C from bro code and i started around 9 days ago now im in ternary operators and return statements and both of them feel a little wonky and im not really used to them so to sum up
Am i rushing stuff or not and is coding stuff myself alone is enough or should i do something else along side
fixermark@reddit
You sound good so far.
Ternary operators are just a less-fancy if statement.
Return statements give you a way to pass information from inside a function to wherever you came from.
If you want a recommendation of what to learn alongside: a little bit of assembly might make things click together a little better. You can try a simple assembly (like the 6502 chip assembler, which is what the NES used back in the 80s; here's one tutorial for it - https://skilldrick.github.io/easy6502/) or learn about modern x86 assembly, which has way more features but is one of the machine codes used these days by real computers. The fun thing about learning assembly is you can actually put it inline inside a C program (usually using the __asm__ keyword; see https://en.cppreference.com/w/c/language/asm.html) and play around with it directly!
Why am I recommending assembly? Because C was built on top of raw assembly initially so is very close to how the CPU talks about things, which is one of the reasons it works the way it does. The ternary operator, for example, is not very far from just evaluating a branch assembly instruction to store this or that data into a register and then moving the register somewhere else based on what the variables in the program are doing (a register is like a "hardware variable;" the CPU has a fixed number of them, it can change their contents directly, and move them in and out of RAM). Similarly, functions (and return) are the way they are because most CPU assembly languages support a concept called a "call stack," and functions map to that idea very cleanly (the compiler usually creates return as "put this value in some register and then execute the assembly instruction that pops a return instruction address off of the stack and resumes execution; the code we return to will know to get the return value out of the register").
godbolt.org is also a good site for exploring these things; you can paste C code into it and it will show you what assembly the compiler turns that C code into.
mredding@reddit
They're not statements at all, and that's actually a big deal. They're expressions - which means they have a TYPE. You can write this:
You can assign a conditional value - either
bazorqux, to a conditional variable - eitherfooorbar. Notice I said ternaries have A type, meaning both operands will follow promotion and implicit conversion rules to determine the nearest common type.So there lies the difference between
ifand?:. If all you need is a branch, you use anif, if you need a type, you use a ternary. You can nest ternaries (please don't), but if the expression can't resolve to a single common type, then you're forced to break the logic down into statements.If you're not evaluating the value of a ternary:
I would consider that an error needing a clearer, more concise, more correct formulation:
Maybe throw some braces in there... Since we don't program on punch cards or paper tape anymore, the clarity is more valuable than the character or line count.
fixermark@reddit
You are correct. I had discarded that from my brain because nobody I've ever worked with would ever sign off on code that used the ternary operator on the left side of assignment-equals, but it definitely works.
Now never do it in my presence. ;)
green_meklar@reddit
Not in all languages! Javascript doesn't permit this trick. I doubt Java permits it, although I didn't try it.
green_meklar@reddit
They really aren't. But when you're not used to thinking that way, such concepts can be counterintuitive. Both of those things are tools that programmers eventually learn to just use without thinking about them.
The one slightly tricky part is that the ternary operator short-circuits, that is to say, it only evaluates the required branch. Imagine this code:
Calling
test()prints:Notice how the 7770 is skipped the first time around, and the 3330 is skipped the second time around. You might expect that both the 3330 and the 7770 would run on both calls, but they don't. See how that works?
If you're understanding each concept to the point of being able to use it (for literally anything, other than triggering segfault errors) before advancing to the next concept, you're not rushing it.
Unhappy_Brick1806@reddit
An upgrade you may want to consider is a Bluetooth travel keyboard. The keyboard doesn't have to be anything fancy, it's just a crazy QoL upgrade when working with a mobile device
Afraid_Hyena_515@reddit
Im a student still in high school; I chose c++ as my first languag ead i do not regret it. forget what other people say "oh its too hard dont do it" just ignore them now im 3 years into c++ and i've been able to code games and utilities(even participate in competitions) bro code is one of the best teachers online. You arent rushing but the pace is good its better to learn terminal and linux alongside C mainly because C utilises CLI.
SchemeWestern3388@reddit
C is a very good first choice. It throws a lot at you, but that’s about one hundredth of what C++ will do to you.
Python is another great learning language. Feel free to do both at once.
fixermark@reddit
Underrated advice. To really understand how languages work, you have to learn more than one. Ideally quite a few more than one. Then you start to see the common patterns that every language is trying to enable, and those patterns mean something. Once you recognize them, it becomes easier to learn new languages because you can start hanging new languages on those fundamentals hooks.
Striking_Display8886@reddit
Most CS classes start at C. You’re doing well! Check out CS50 with David Milan through Harvard, it’s a free course. You’ll get what you’re looking for.
https://pll.harvard.edu/course/cs50-introduction-computer-science
Good luck!
Tolba69@reddit (OP)
Thank you for your support
When bro code explains stuff he sometimes doesnt mention what are the limits of a certain functions are and becuase of this i wasted an hour trying to know why switches does not work with doubles (didnt have internet at the time) only to know that later from chatGPT
(I just want to know how to prevent such situations from happening again and lessen my debugging time overall will local ai's help?)
fixermark@reddit
I don't know I would set as a goal to prevent such situations. What you experienced is the learning part. Sometimes the learning is taxonomy ("why doesn't this work? Oh, because it doesn't. It actually works in some languages but this isn't one of them"), but that's still valuable.
If you're in a place where it's hard to get consistent internet, snagging a textbook (however works for where you are: library, digital download, borrow from someone) isn't a bad idea at all, especially for a language as old and rock-solid as C. C++ it's hard to recommend a textbook because most of them will teach you "the wrong way to do modern C++," but C has much less of that problem.
YetMoreSpaceDust@reddit
Are you just following tutorials or are you solving exercises on your own? You'll learn a lot more, a lot faster, by figuring things out and a decent guide should have incrementally difficult exercises to match your level.
Tolba69@reddit (OP)
I usually get a grasp of the code and i try making 1 myself as diffrent as possible
deux3xmachina@reddit
You'll probably also want to get a copy of "The [ANSI] C Programming Language" by Dennis Ritchie and Brian Kernighan, it's the canonical entry to C programming.
For more modern resources, you can get a copy of the excellent "Modern C" here: https://gustedt.gitlabpages.inria.fr/modern-c/
I think you're on a great path, keep a list of things you'd like to build or understand better as you go too. It'll make finding new projects much easier. Additionally, you'll want to look into using versios controll software like
gitand a code forge like codeberg, sourcehut, gitlab, etc. then, when you're ready, it's much easier to share your code with others as well as enable you to work on your projects from multiple different machines.AlSweigart@reddit
You're doing fine. Keep learning.
probability_of_meme@reddit
Sounds like youre doing the next best thing when you don't have access to a computer. Hopefully you're finding it at least a little bit fun.
AdFormer9844@reddit
You should try to find homework assignments from C courses and try doing them yourself. You ain't going to learn much by just following tutorials, learn by doing is the best way of going about it.
mixedliquor@reddit
Personally, I wish I had not started with C when I was in high school.
A lot of the language did not click with me until I coded with higher level and lower level languages which added a lot of context to C.
As another respondent said, I found assembly to be easier than C.
Sounds like you're doing pretty damn good so far.