C Programming Tutorial 9 – Variables, Expressions, Statements

Don’t forget to read the last blog on Intro to UNIX/Linux Part 3! Are you new here? Start from the beginning of the series!

We started off our C programming with the hello world program. Before you move on, you really want to familiarize yourself with this basic structure. Don’t freak out if you don’t have it memorized!

The point is not so much to memorize it because that will come with time. Just have a rough idea of where to put what and where semicolons go. Also know that the spacing is not a concern to C. You can say that C is whitespace insensitive.

The next few blogs are going to be designed to give you the basics of C programming. These basics are going to be various pieces of information that are going to help you, but I’m not going to go in a ton of depth. That’s because after we go over the basics, I am going to go in depth in many topics. I want you to be able to start working with C and not have to wait till you get to video 50 to know how to do some of the most basic things.

Variables

The first thing I am going to introduce to you is the concept of a variable  Now, the chances are you’ve worked with variables before either in some programming language or in math. We can create a variable in C very simply.

This is called the variable declaration. We are saying that the type of data we want to store in this variable is an integer. That means any whole number. Additionally, this entire line is known as a statement. A statement is when we tell the computer to do something. Think of it sort of like a command to the computer. There are tons of different kinds of statements, a variable declaration is just one type.

We’ve declared this variable. That means that it exists, but how do we give it a value? Giving a value to a variable is called variable initialization. We can initialize a variable like this:

The x is the variable, the = is called the assignment operator, and 10 is the integer we want to assign to x.

Now, anytime we use the value x, image just replacing it with the value 10.

Also, a very important word to know is syntax. This is the format of each command we can do in a programming language.

Math and C

We can also do math in C.

This shows two important things. First, we can declare and initialize a variable in one command, and two, we can use the variable x anywhere the value 10 would be appropriate. This will give y the value 5.

The way I think of variables is as a box containing some value. So we are putting the value of x/2 into y. That means if we were asked the value of y, we would say in this situation it is 5. Not x/2.

We could just have easily done int y = 10/2. The problem with this though is that if we use x a lot, we are not always going to want to use the value 10, or we might not always know the value of x. In this situation we are giving a value directly to x, but there will come a situation when we might ask the user for the value x.

Expressions

The last thing I wanted to teach you in this blog is the concept of an expression. An expression is anything that can be evaluated to one value. For example, x/2 is an example of an expression. This is important because variables can only contain one thing. Whatever expression you give it must be evaluated. You are going to hear the word expression all of the time.

In the next blog we are going to learn about outputting the value of a variable into the console when we run the program. Check it out!

Leave a Reply

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