Welcome to my c++ introduction! I hope that I do a good job explaining this.
EDIT: I have a new lesson for all to see on Repl talk!
I will post something every Sunday I will try to post something every Sunday.
EDIT: Thanks so much for the cycles(upvotes). It means a lot to me and I will definitely post on Saturday!
Fellow c++ers: If something is incorrect or can be explained better, tell me!
People reading this: Questions? Comment on this post and I will get back to you as soon as possible.
OK, let's get started!
Including files using preprocessor directives
If you've learned another language before, you probably know that you need to include files.
If you haven't, picture this: You are in a library, and you need to borrow a book. You use it in your book report.
- To include a file, use this:
- Your first file (credit to @DynamicSquid for making this more clear):
#include <iostream> // used for input and output to the screen. This is a //basic header file.
- Comments(credit to @Highwayman for asking me to add this)
- Comments are used to document what the code does. They won't change the program
- Person: Why use comments if they don't change the program?
- Me: OK, imagine this: You are working for a programming company and you send your giant program(let's say 100,000 lines) to the big boss for review. If you didn't add comments, they would have to figure out all 100,000 lines by themselves!
- Person: Fine, fine, but I want to just program for fun. No one will look at my work, just me. So do I have to put comments then?
- Me: Of course. Say you have a program you've worked on for a looong time(the same 100,000 lines of code). It finally works! Now, you leave it for 2 months or so. Then, you realize that there's something more you want to add to it, so you go back. Now, you have to retrace all your code again! (Real life experience: I ended up deleting some code I had been working on because I didn't understand it after coming back from a vacation. Please don't make this mistake!)
end of offshoot
- Ok, so anyway, let's look at the difference between single and multi-line comments.
//This is a single-line comment. //For each new line you make, //You need a new pair of forward slashes(//) to make it //a comment.
/* This is a multi line comment. You can have as many lines as you want, as long as it is between the /* and the thing you will see right now. */
That's all folks! Tune in next week to learn about the main function!
(insert jazzy music here)
Uh, quick thing: to fix your use of the markdown, just put a space in between the hashes and the actual thing you want to be shown as a header. Also do the same for the pluses in your list. When making a code black, you can actually have it have coding highlighting by putting the name of the language next to the top beginning block thing.
#### header text
Another thing, simply stating that an entire line is a comment can be a tad confusing for a beginner who’s never seen code or comments before, so for those of you who are still a little bit confused: what the OP is talking about and showing you is called a single line comment. comments don’t affect you code. To begin a single line comment in c++, simply place 2 forward slashes(
//) in front of you comment like the OP demonstrated.
#include <iostream> // fhdheu blah blah blah. Notice how this 'comment' will not stop until you // make a new line, so be careful to always put it at the end of a line of // code, or on a line by itself.