C Programming Tutorial 16 – Comments

Don’t forget to read the last blog about using functions in C! Are you new here? Start at the beginning, Intro to C!

This blog is going to introduce you to the concept of comments. In general, comments are pretty easy to understand. I’m going to try not to waste your time by giving you some extra information relating to comments that may come in useful for you when you work on bigger projects.

To do a single line comment, use

Anything after that will be ignored.

The comment allows you to type in whatever you want and the compiler does not care if it is here. Run the program and see that it does exactly the same thing.

A multiple line comment allows you to put in comments as long as you want and it will continue to the next line…

These are useful for when you want to make a block comment that is usually used for describing a program or something:

These actually do not have to be on multiple lines. I’ll show you an example where these comments might come in handy.

We haven’t discussed if statements, but here is a sneak peak of what is going to be coming up in the future. You can see that I was able to throw a comment in the code and then continue coding on the same line. Run this and see that it does exactly what you expect it do.

Now, a lot of people use comments to describe what some confusing code does. It’s super important though that you always keep comments up to date. Nothing is more confusing than reading a comment that says code does something when it really does something else.

When Should you Comment?

When should you comment? Well, if you see any kind of confusing math or confusing syntax, leave a comment before it to explain it. Imagine you are going to read it in 5 years and you want to know what it is doing.

When there is some piece of code you need to remember to implement or something you need to remember to fix, you can start the comment with TODO. That way, when you are finishing up your software you can do a search for TODO. To search in Vim all you have to do is type / followed by the word you are searching for:

Then press n to go to the next value of it.

When to not comment? Don’t comment on anything that is obvious. Often in college they want students to leave a comment on everything. Personally, I think this is a bad thing to teach students as it teaches them to over comment on things that should be clear from the code itself, but whateverrrrrrrr.

Honestly, I would avoid using large comment blocks at the top of a program when possible. I think the only thing you should really write at the top is the purpose of the program.

You can put them for now, but once you start working on more sophisticated software, you are going to use a software such as Git which is going to keep track of all dates and people anyways, so it’s a waste of effort and just another thing that can be wrong.  

For example, you might get hired at a company and all of their code has the author of the previous guy who worked there who got moved to a new department. Are you supposed to change it to you? Add your name? It just introduces confusion.

In this blog I taught you about comments, but I also touched on how to search in Vim. Vim might not be so crappy if we actually know how to use it a bit. So in the next video we are going to learn some ways to make Vim a little bit more user friendly.

Are you ready to move onto the next blog? We’re going to talk about using Vim basics!

Leave a Reply

Your email address will not be published. Required fields are marked *