C Programming Tutorial 29 -Variables

Did you read the last blog on the bool Data Type? Are you new here? Start at the beginning, Intro to C!

Let’s talk about variables! Now, I know, we’ve said a lot about variables, so this video is going to be pretty short.

The first thing I want to make sure you guys are clear on is the difference between defining a variable and initializing a variable. We can define a variable like this:

We can initialize a variable like this:

We can do both at the same time like this:

You don’t always have to initialize a variable. For example, if you are going to get the value of this integer from user input in the console, you do not need to initialize it. That’s because when we use scanf() a similar function, it will assign that value to the variable for us.

You always have to be careful not to use an uninitialized variable. Let’s figure out why:

Run this and you’ll see that the value of x is completely meaningless. This is not the kind of data you want showing up in an application you write.

The next thing I want to talk about is naming variables. As you know, we as programmers are allowed to choose our variable names. But there are some rules and some guidelines that I have for you.

The first thing is the characters you are allowed to use are all letters, numbers, and the underscore character. The only thing is that you have to start with a letter or underscore, not a number.

The Sensitivity of Variables

Additionally, variables are case sensitive. What case sensitive means is that uppercase and lowercase characters are considered to be different when it comes to naming variables. So Tacos, tAcos, and TACOS are all different variables.

Even though you are allowed to use different casing, be very cautious of having two variables with the same name but different casing because it can be very easy to use the wrong one.

As for symbols, the only one you are allowed to use is the underscore. You may get by with using some others, for example you can try creating a variable with a $ and it might compile, but that is not a universal statement and not all compilers are going to be okay with that.

Make your variable names descriptive. A variable named “amount” is super vague…amount of what? Additionally, I would recommend avoiding making your variable names too long. And please, please, please do not use shortened words unless you have a very clear system.

Don’t call amount amt. The variable named amountSold at least says what it is about, but using the variable name amtSld makes your code harder to read.

Another thing is that it is sometimes recommended to declare all variables at the beginning of a block (inside the main function for example), but this is less relevant today as the compilers are okay with declaring variables about anywhere.

Because of this, I personally think it is more readable to declare variables that are going to be used throughout a section of code at the top, but declare variables that are going to only be used briefly right before the section where you need them. I find it annoying when I’m reading code that references a variable for the first time and the variable was declared way before then.

Are you ready to learn more? Check out the next blog on Intro to Operators now!

Leave a Reply

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