![]() |
|
[C] Read before posting "How to learn C/C++" [Ask Questions] - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] Read before posting "How to learn C/C++" [Ask Questions] (/showthread.php?tid=5488581) Pages: Pages
1
2
|
Read before posting "How to learn C/C++" [Ask Questions] - caiveman - Dec 9, 2016 Experienced programmers, please provide constructive criticism My Skype: [email protected] Add for any C programming help Hello All, I see many posts especially on this section of HF that go something like "Learning C" or "How to learn C++". Hopefully this post will answer some of your questions that I had when I was in your position and end this spam. Before we start, I want to make sure nobody has the - "I want to learn C by making my own botnet/crypter" mentality. You are guaranteed to give up at the first difficulty if you have this mindset. Introduction/Background Information Firstly, an introduction to C. C was developed by AT&T makes up most of UNIX (and Linus' open source copy, Linux). It therefore is a native language to Windows. Many programmers prefer C because of its speed, portability, and the ability to interface with low-level aspects of the operating system, despite it's steep learning curve. C is not the easiest language to learn as your first. If you have never programmed before, I would highly recommend starting with Python as it is incredibly powerful yet has a simple syntax, and is very hard to fuck up your PC. Prerequisites To learn C, it is essential to know basic computer science, aka the stack, bytes, endian-ness, pointers, memory, etc because of the low-level nature of the language. My recommendation for this is to watch this series of lectures by UNSW which covers the basics of computer science. The ~20 videos are available on youtube to watch for free. It is absolutely crucial to know this stuff to program in C, especially the concept of pointers and addresses. Tools needed
Do these things before reading on Google (in order) to familiarise yourself:
Read Actual Programming Okay, now to the easiest part of learning C - the syntax. A common misconception that beginners hold when learning a new language is that the most important and hardest aspect is the syntax, or actually writing the code. Any good programmer should be able to learn the syntax to a new language in a couple of days, but it can take months to develop an efficient and effective programming style. I will put "quiz" questions along the way so you can make sure you know the stuff before moving on. Deconstructing a C program Lets have a look at a typical C program Code #include <stdlib>Now, lets go line by line and analyse the code Variables Variables store data in memory. In C, you do it like this. Code int year = 2016;A constant variable is just a variable that cannot be changed. Code const int encryption_key = 5497512145; ORthe "const" can come before or after data type declaration, pick one and stick to it. Protip: an "unsigned" variable basically means it can only be positive, doubling the "range" of the variable. Quiz: How many bytes does a signed int hold? Global variables vs Local variables Global variables are variables that are declared outside of any function. aka: Code #include <stdlib>Protip: Try to avoid using global variables, if not at all. 99% of the time there is a better way of doing it (especially in c++) Loops and conditionals These are the two core principles found in any programming language. There are a number of loops that you can use in C, the two most common ones being Code while (condition)Code for (variable; condition; iterator)counting to 100 using each loop Code #include <stdio>There are more loops, such as the do - while loop, but you don't really need to know these, and it's more "syntactic sugar" to save the programmer a few lines of code. Input and Output Input There are couple of input methods in C, and it is a little bit contentious on which one you should use. Some people use fgets() or getline() but for the purpose of this tutorial I will use scanf. Example: Code #include <stdlib>Quiz: Think of how you could get multiple characters of input using loops and scanf()... Output Use printf for output in C Code printf("%d\n", integer);Strings Strings are a tricky one in C, because unlike other languages where they are stored as an object, strings are literally just arrays of characters. Two ways of making a string Code char* str = "this string can be as long as u like, but you can't change it once defined (easily)";All strings end with the null character '\0' so the pc knows when to stop reading. There are obvious advantages and disadvantages to both. In C++, it is much better because there is actually an inbuilt function in the <string> header std::string str("sick string with lots of functions"); Further reading on C++ strings Moving onto C++ C++ retains 99% of the syntax from C, but adds a crucial new concept. Classes and proper Object Oriented Programming. I don't have time to explain in OOP detail but it is essentially programming Objects that interact with each other. Read this -> https://www.codeproject.com/Tips/679615/OOP-Explained-in-Seconds Watch this -> https://www.youtube.com/watch?v=lbXsrHGhBAU This keeps the code nice and organised so other people can understand what you are doing. It's really awesome once you get the hang of it. Conclusion and further reading Now if you actually did all those steps and learnt some basic computer science you should have a shallow understanding of the amazing language that is C. The most helpful piece of advice I can give you is to read lots of good C code, and try to understand it, and replicate it. Further reading and googling:
Projects to complete from here:
~caiveman * - Killer Kamal - Dec 9, 2016 Nice HQ thread! Definitly worth reading. * - caiveman - Dec 9, 2016 This post is reserved for further updating * - g4143 - Dec 9, 2016 You missed one of the crucial things a rookie programmer should be encouraged to use...constants. It won't be apparent to the rookie why constants are important but they should get into the habit of using them wherever they can. Some say it's better 'for the rookie' to declare or define everything as constants or literals and then change them to immutable variables as needed. * - caiveman - Dec 9, 2016 (Dec 9, 2016 07:28 AM)g4143 Wrote: You missed one of the crucial things a rookie programmer should be encouraged to use...constants. It won't be apparent to the rookie why constants are important but they should get into the habit of using them wherever they can. Some say it's better 'for the rookie' to declare or define everything as constants or literals and then change them to immutable variables as needed. dude i'm still writing it, and I wouldn't consider constants to be absolutely crucial. * - g4143 - Dec 9, 2016 (Dec 9, 2016 07:37 AM)caiveman Wrote: dude i'm still writing it, and I wouldn't consider constants to be absolutely crucial. Constants and literals are two of the most underappreciated methods for producing maintainable, sane code. * - caiveman - Dec 9, 2016 (Dec 9, 2016 07:53 AM)g4143 Wrote: Constants and literals are two of the most underappreciated methods for producing maintainable, sane code. Okay - can you please give me an example? * - g4143 - Dec 9, 2016 (Dec 9, 2016 08:34 AM)caiveman Wrote: Okay - can you please give me an example? An example of what? Syntax or a case where constants/literals are preferred? * - Dostoevsky - Dec 9, 2016 (Dec 9, 2016 08:34 AM)caiveman Wrote: Okay - can you please give me an example? Well, Google have found constants important enough to add it to their coding style guides. When using local constants, you prefix the variable name with "k". Constants are incredibly useful for showing intention and imposing it (despite the fact you can cast away constness). Some people also fail to realise where STL things are made constant and then write inefficient code. E.g. if you tried to create a map declared like: PHP Code PHP Code Small things like this can demonstrate how understanding the importance of const can stop you from writing bad code. The example I described above was an example by Scott Myers regarding how using auto for types is a good way get the desired type rather than assuming the type. Essentially: use const auto& if you don't want to make a copy when iterating maps. Const correctness is vital to writing good C++ code. * - caiveman - Dec 9, 2016 (Dec 9, 2016 04:56 PM)Dostoevsky Wrote: E.g. if you tried to create a map declared like: Thanks for this example. I'll add something similar to the tutorial. * - Infinity. - Dec 10, 2016 https://www.youtube.com/watch?v=Ps8jOj7diA0&list=PLD28639E2FFC4B86A An interesting series for C programming. * - caiveman - Dec 11, 2016 (Dec 10, 2016 10:11 PM)Infinity. Wrote: https://www.youtube.com/watch?v=Ps8jOj7diA0&list=PLD28639E2FFC4B86A Wow, just watched the first lecture and definitely looks interesting. Thanks! * - dendro - Dec 11, 2016 (Dec 10, 2016 10:11 PM)Infinity. Wrote: https://www.youtube.com/watch?v=Ps8jOj7diA0&list=PLD28639E2FFC4B86A Those are some quality videos, thanks for sharing. * - caiveman - Jan 3, 2017 Bump because people can't use the search bar * - Sajuuk - Jan 4, 2017 Thread is useless to most because one of the prerequisites is a brain. |