C Programming Tutorial 29 – Variables

submitted by mkenny400 on 03/19/18 1

This video is going to discuss 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:   int x;   We can initialize a variable like this:   x = 5;   We can do both at the same time like this:   int x = 5;   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:   int x;   printf("%i\n", x);   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.   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. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Support me! www.patreon.com/calebcurry Subscribe to my newsletter: eepurl.com/-8qtH Donate!: bit.ly/DonateCTVM2. ~~~~~~~~~~~~~~~Additional Links~~~~~~~~~~~~~~~ More content: CalebCurry.com Facebook: www.facebook.com/CalebTheVideoMaker Google+: plus.google.com/+CalebTheVideoMaker2 Twitter: twitter.com/calebCurry Amazing Web Hosting - www.dreamhost.com/r.cgi?1487063 (The best web hosting for a cheap price!)

Leave a comment

Be the first to comment

Collections with this video
Email
Message
×
Embed video on a website or blog
Width
px
Height
px
×
Join Huzzaz
Start collecting all your favorite videos
×
Log in
Join Huzzaz

facebook login
×
Retrieve username and password
Name
Enter your email address to retrieve your username and password
(Check your spam folder if you don't find it in your inbox)

×