Did you read the last blog? Go read it on Implicit Type Promotion! Are you new here? Start at the beginning, Intro to C! Now, let’s give an example of explicit conversion: In this situation, we are splitting the pieces of pizza with our friend. We are trying to figure out how many pieces each person should …

Continue reading "C Programming Tutorial 40 – How to use the Type Cast Operator"

The post C Programming Tutorial 40 – How to use the Type Cast Operator appeared first on Caleb Curry.

]]>Did you read the last blog? Go read it on Implicit Type Promotion! Are you new here? Start at the beginning, Intro to C!

Now, let’s give an example of explicit conversion:

In this situation, we are splitting the pieces of pizza with our friend. We are trying to figure out how many pieces each person should get.

Both operands are integers, so the result will be an integer. There are two ways we can fix this. First, we can add .0 to the 2. This works, but it will not always work. For example, we could have this:

Print this out and see that the result of the double is actually 8, not 8.5.

Let’s assume that the second operand is given to us as an integer and we cannot change it for some reason. The only solution is to use an explicit cast:

This explicitly casts the value of slices to be a double.

Now, when you output the data, it should be 8.5.

The important thing to know here is that the cast does not affect the original value of the variable. The casting is actually just another operator. Think of it as a unary operator that does not change the value of the variable.

The value of the variable is first given, and then that value is changed to a double, but the original variable stays the same. Once you define a variable as a certain type, you’re stuck with it.

It’s important to think of the casting as a unary operator. That means that it only works on one operand. Which operand? Is it this:

or is it:

It’s actually the second one. It only affects the thing to the direct right.

You can cast an entire expression using parenthesis, so the first one is completely valid. The only problem is that the result will still be 8. That’s because we divide two ints by one another and get 8, and then we cast 8 to a double, which stays 8.

Because of this potential area of confusion, I recommend you try to avoid casting a large expression that is in parenthesis unless you know exactly how it is going to work.

TRIVIA TIME!!!

What will this output?

This program illustrates that even if one of the operands is floating point, the entire expression does not use floating point arithmetic. In the case with the variable d, the 25/2 evaluates to 14, then * 2 evaluates to 24.0. Both printf()’s output the same thing.

Remember that the decimal is always truncated, never rounded. So even if some division ended with .99999, it would just delete the .99999 and not round up.

Ready to learn more? Read the next blog on our first quiz covering the past content!

The post C Programming Tutorial 40 – How to use the Type Cast Operator appeared first on Caleb Curry.

]]>The previous blog was foundational to this blog, so go read that first! Are you new here? Start at the beginning, Intro to C! Essentially, casting is when something of a certain data type is converted to a different data type. This blog is going to be showing examples of both implicit casting and explicit casting. When …

Continue reading "C Programming Tutorial 39 – Implicit Type Promotion"

The post C Programming Tutorial 39 – Implicit Type Promotion appeared first on Caleb Curry.

]]>The previous blog was foundational to this blog, so go read that first! Are you new here? Start at the beginning, Intro to C!

Essentially, casting is when something of a certain data type is converted to a different data type.

This blog is going to be showing examples of both implicit casting and explicit casting. When you study implicit conversion in C, you will likely come across the term promotion.

This article talks about implicit conversion, and the section on promotion talks about what data types can be converted to other data types without loss of information.

http://en.cppreference.com/w/cpp/language/implicit_conversion

A common example of this is when we pass floats to functions.

*float x = 50.0f; //* You can put the f here if you want to be specific that this is a float, but either way should work just the same).

The printf function actually takes the x as a double. This means that 50.0f is **promoted** to 50.0. If we are talking about constant values, a float value always has an f. If you leave the f off, it is assumed to be of type double. You can use the %f conversion character for both floats and for doubles because they are actually always being printed as doubles… Even if you pass in a float.

Now, it’s important to understand that the actual value or data type does not change. The variable x is still of type float.

Another common type of implicit conversion is when we are doing assignment. If we have something such as this:

The integer 60 is implicitly casted to a double. That’s because an integer can fit inside of a double without losing data.

Sometimes, it’s not that easy and we risk losing data. That’s what we are going to be talking about in the next blog! Read it now!

The post C Programming Tutorial 39 – Implicit Type Promotion appeared first on Caleb Curry.

]]>Did you read the last blog on Strongly Typed Vs Loosely Typed Languages? Are you new here? Start at the beginning, Intro to C! In the previous video we mentioned how C was a strongly typed language. Imagine C as a grumpy old man who you owe a dollar bill to. Well, he wants his money now, …

Continue reading "C Programming Tutorial 38 – Type Casting"

The post C Programming Tutorial 38 – Type Casting appeared first on Caleb Curry.

]]>Did you read the last blog on Strongly Typed Vs Loosely Typed Languages? Are you new here? Start at the beginning, Intro to C!

In the previous video we mentioned how C was a strongly typed language. Imagine C as a grumpy old man who you owe a dollar bill to. Well, he wants his money now, but all you have is some change.

You look at your change, grab four quarters, and find out that it adds up to exactly a dollar. But unfortunately, you owe him a dollar bill, not just 100 cents. So yeah, he beats you over the head with his cane.

That is the way you ought to look at data types in C. You could say that the guy wanted 1 dollar, but you gave him .25 + .25 + .25 + .25 = 1.00. This my friend is unacceptable.

Well, to fix this problem, you could go the bank, exchange your four quarters for an actual dollar, and then go pay the man. The bank in this situation has fixed your problem by converting your change to whole dollars. In computer science terminology, you can say that** your number of the double data type was casted to a number of the integer data type**.

So, **type casting** is the process of converting one data type to another.

There are two main categories of type casting, implicit and explicit type casting.

**Implicit type casting **is when the conversion happens automatically and you do not have to worry about it.

**Explicit type casting** is when you have to say specifically that you want to convert some data from one data type to another. You can think of this as “explicitly” telling the computer what to do. It’s not just assumed.

In general, you can think of the implicit type casting as “lossless”. A good example of this is converting an integer to a double. If you convert 1 to 1.00, nothing is lost.

You can think of explicit type casting as “lossy”. For example, if you convert the double value 2.54 to an integer, you truncate the number down to 2.

An example of implicit conversion is if you assign 1 to a double value. You gave the assignment operator an integer, but it gets stored as a double. There’s really nothing you have to do.

The easiest way to explicitly type cast is to use an operator where you put the type you want the value to be in parenthesis before the data. For example, we could do something like (int) 2.5. This will convert the 2.5 to 2 (an integer).

In the next video we will be going over some examples of implicit and explicit conversions and some things to look out for that can cause problems.

In the next blog we are going to be discussing all of the Implicit Type Promotion. Check it out!

The post C Programming Tutorial 38 – Type Casting appeared first on Caleb Curry.

]]>Did you read the last blog on Operator Precedence? Are you new here? Start at the beginning, Intro to C! This blog is going to be an introduction to how programming languages consider data types. As we know, there are many data types in C programming. C is an example of a strongly typed programming language. Strongly typed …

Continue reading "C Programming Tutorial 37 – Strongly Typed Vs Loosely Typed Languages"

The post C Programming Tutorial 37 – Strongly Typed Vs Loosely Typed Languages appeared first on Caleb Curry.

]]>Did you read the last blog on Operator Precedence? Are you new here? Start at the beginning, Intro to C!

This blog is going to be an introduction to how programming languages consider data types. As we know, there are many data types in C programming. C is an example of a strongly typed programming language**. Strongly typed** means that every piece of data has a specific type.

Imagine if we stored everything in strings. This would become a problem because the computer would never know how to work with certain data. For example, let’s say we have “5” and “6” and we try to add them. Should the computer add the numbers to be “11”, or should it combine the strings to be “56” (The process of combining two strings like this is known as **concatenation**).

Concatenation doesn’t work quite this way in C, but in general, these are the kinds of problems that come up when we do not have things strongly typed. The opposite of a strongly typed language is called a loosely typed language. Now, a loosely typed language is not quite this loose.

**A loosely typed language** will usually have a few general types such as numbers, characters, or strings, but they are not nearly as strict with the typing.

What is a good example of this?

In C, if we do something like this: 1/3, the result is going to be zero. That’s because the type of data being used are all integers. A loosely typed language is probably not going to be that obnoxious about types.

The benefit of using a strongly typed language is that it teaches you to be very discrete about what you want the computer to do. I think of a loosely typed language like water: it’s flowy and forgiving. I imagine a strongly typed language as very strict.

Using a loosely typed language can be more simple starting out because you don’t have to worry about all of the different data types, but there is less protection as we will see soon.

For example, in a popular programming language known as JavaScript the type system is loosely typed. This means instead of having data types like integer, double, float, signed and unsigned, we just have numbers.

Some people call the way JavaScript works with data types as duck typing…. Which is weird, but basically it says something like “if it looks like a duck, if it talks like a duck, it is a duck.”

Let’s say you write a function (which is just a section of code you can call multiple times) which expects a number. Whoever uses that function can pass in whatever they want, or pass in nothing at all.

Because JavaScript is not strongly typed, you have to do extra work inside of the function to make sure the function responds appropriately. What happens if somebody throws in an array? What happens if somebody throws in a string? What happens if somebody throws in a potato?

With a strongly typed language, you often have to pay extra attention to data types, but you have an extra layer of protection, too. Different people like different things.

In C, it is not easy to flow between data types. Because of this, we need something called type casting. That is what we will be discussing in the next video.

In the next blog we are going to be discussing all of the Type Casting. Check it out!

The post C Programming Tutorial 37 – Strongly Typed Vs Loosely Typed Languages appeared first on Caleb Curry.

]]>Did you read the last blog on Assignment Operators? Are you new here? Start at the beginning, Intro to C! You may have heard from math class of this fancy thing called order of operations. We discussed this in a previous video so I’m not going to waste your time by repeating myself, but I am going …

Continue reading "C Programming Tutorial 36 – Operator Precedence"

The post C Programming Tutorial 36 – Operator Precedence appeared first on Caleb Curry.

]]>Did you read the last blog on Assignment Operators? Are you new here? Start at the beginning, Intro to C!

You may have heard from math class of this fancy thing called order of operations. We discussed this in a previous video so I’m not going to waste your time by repeating myself, but I am going to look at the same concept from a bigger point of view.

The point of the order of operations is to say which operators happen first and from which direction. For example, we know for the c operators, the multiplication happens first. This order in which operators happen is known as **precedence**.

For example, you can say that multiplication has precedence over addition. And if we have multiple multiplications in one expression, it happens from left to right. The direction of which way a specific operator happens is known as the operator’s **associativity**.

This is a good reference page that lists the C operators, their precedence, and their associativity.

https://en.cppreference.com/w/c/language/operator_precedence

A good example of left associativity is if we do something like this:

This is valid C and shows that we can declare two variables on one line and also assign values on one line. In general, I try to do things one step at a time, but sometimes you will encounter statements like these.

Although either way the result is going to be that x and y are both equal to 5. The order in which this happens is from the left and from the right. First, y = 5 will happen. Next, x = y will happen.

If you have two operators on one line, the first thing that happens is the operator with the highest precedence. For example:

This could either be evaluated as:

or

The second is the one that will happen according to the operator precedence.

In the next blog we are going to be discussing all of the Strongly Typed Vs Loosely Typed Languages. Check it out!

The post C Programming Tutorial 36 – Operator Precedence appeared first on Caleb Curry.

]]>Did you read the last blog on Increment and Decrement Operators? Are you new here? Start at the beginning, Intro to C! In the previous blog I showed you how you can add one to a variable. You can use the ++ operator. I also showed you what to do if you wanted to add more than …

Continue reading "C Programming Tutorial 35 – Assignment Operators"

The post C Programming Tutorial 35 – Assignment Operators appeared first on Caleb Curry.

]]>Did you read the last blog on Increment and Decrement Operators? Are you new here? Start at the beginning, Intro to C!

In the previous blog I showed you how you can add one to a variable. You can use the ++ operator. I also showed you what to do if you wanted to add more than one to a variable. For example, we can have something like this:

The left side of this will evaluate to 200 and then that will be assigned to pizzasToEat. The = here is actually an operator of its own. It’s a binary operator because it takes some value on the right and assigns to the variable on the left.

There is actually a collection of operators that exist to make this a whole lot easier. The syntax is a bit odd at first, but you will get used to it.

This will actually add 100 to the variable.

The name for this operator sounds just like it looks. It’s called the **plus equals operator**.

There are a multitude of assignment operators. The most important ones to know are =, +=, -=, /=, *=, and %=. There are some others but they are very complicated compared to where we are right now, so we’ll ignore those for now. The thing to keep in mind, though, is that these are assignment operators, so they are all going to change the value of the variable.

Let’s go through some more examples.

pizzasToEat -= 100 will subtract the value of the variable by 100.

pizzasToEat /= 5 will divide the value by 5

pizzasToEat *= 2will multiply the value by 2.

pizzasToEat %= 2 will give the remainder when divided by 2.

These operators are great because once you are familiar with them they more clearly express what you are trying to do. They also reduce the chance of errors because you only have to type a particular variable one time, not twice.

In the next blog we are going to be discussing all of the Operator Precedence. Check it out!

The post C Programming Tutorial 35 – Assignment Operators appeared first on Caleb Curry.

]]>Did you read the last blog on Unary Plus and Minus? Are you new here? Start at the beginning, Intro to C! Let’s say we have a variable and we want to add one to it. The hard way to do this is to go like this: What the assignment operator does here is take the entire …

Continue reading "C Programming Tutorial 34 – Increment and Decrement Operators"

The post C Programming Tutorial 34 – Increment and Decrement Operators appeared first on Caleb Curry.

]]>Did you read the last blog on Unary Plus and Minus? Are you new here? Start at the beginning, Intro to C!

Let’s say we have a variable and we want to add one to it.

The hard way to do this is to go like this:

What the assignment operator does here is take the entire expression on the right and evaluate it to a value. So pizzasToEat + 1 = 124. Then, it assigns that value to the variable on the left. So now pizza will equal 124. We can output this data before and after to see this in action.

This is pretty great if you want to add a certain amount to a variable. For example, let’s say the local pizza shop goes out of business and since I’m their biggest customer, they sell me 200 pizzas. We could do this:

If you are just adding one to a variable, there is actually a shortcut:

This operator is a **unary operator** because it only takes one operand, the variable. It is also unique in that it changes the value of the variable. pizzasToEat + 1 does not actually change the value of pizzasToEat unless you assign it back to the variable itself.

The ++ is called the **Increment operator** and it is one of the most popular operators in programming so please become familiar with it and practice using it.

Now, I’m going to introduce some trivia for you.

Let’s clean up a bit…int pizzasToEat = 100; is the only thing we have in our code.

What will this output:

5…4…3…2…1…

The answer is 100!

This is one of the trickiest things to get used to with the increment operator. We are actually saying we want the ++ to happen after the value is assigned. If we want to say increment the value before it is assigned, we put the ++ in front.

Now let’s start with int pizzasToEat = 100 again. What will this output?

5…4…3…2…1…

The answer is 101!

We can print out the value afterwards and see that I’m not lying.

There is also a **decrement operator**:

This is the same as doing:

These two operator are very foreign to beginners so it is often neglected. So make sure you understand what is going on in this video! If you don’t understand, keep trying examples and watch this video a few more times.

In the next blog we are going to be discussing all of the Assignment Operators. Check it out!

The post C Programming Tutorial 34 – Increment and Decrement Operators appeared first on Caleb Curry.

]]>Did you read the last blog on Modulus Operators? Are you new here? Start at the beginning, Intro to C! This video is going to be talking about the minus and plus operator. But wait, don’t leave yet. We’re not talking about the binary minus and plus operators, rather the unary ones. As a reminder, the difference …

Continue reading "C Programming Tutorial 33 – Unary Plus and Minus"

The post C Programming Tutorial 33 – Unary Plus and Minus appeared first on Caleb Curry.

]]>Did you read the last blog on Modulus Operators? Are you new here? Start at the beginning, Intro to C!

This video is going to be talking about the minus and plus operator. But wait, don’t leave yet. We’re not talking about the binary minus and plus operators, rather the unary ones. As a reminder, the difference is how many operands they work on. The binary minus operator, for example, will take one operand, subtract the second operand from it, and generate a result from that. Neither of the operands are modified.

The **unary minus**, on the other hand, just takes one operand, and negates it. To **negate** something means to take the negative of. To make this clear, let’s go through an example.

double money = 25;

double bill = 15;

double total = money – bill;

This example uses the binary operator. But now let’s say you go to pay your bill and they say, you know what, we are going to give you some money instead:

double total = money – -bill;

Now, we essentially have 25 – -15, which becomes 40.

Now, what is the value of bill?

The value of bill is still 15. This operator does not change the value. In the next video we are going to talk about a unary operator that actually does change the operands value.

There is also a unary plus, which is less useful, so I’m not really going to talk about it. I would encourage you if you are an eager learner to look up the use of the unary plus operator.

In the next blog we are going to be discussing all of the Increment and Decrement Operators. Check it out!

The post C Programming Tutorial 33 – Unary Plus and Minus appeared first on Caleb Curry.

]]>Did you read the last blog on Arithmetic Operators? Are you new here? Start at the beginning, Intro to C! This video is going to introduce you to a new operator called the modulus operator (%). In the last video I gave you this example: I told you that the value of y is going to be …

Continue reading "C Programming Tutorial 32 – Modulus Operator"

The post C Programming Tutorial 32 – Modulus Operator appeared first on Caleb Curry.

]]>Did you read the last blog on Arithmetic Operators? Are you new here? Start at the beginning, Intro to C!

This video is going to introduce you to a new operator called the **modulus operator** (%).

In the last video I gave you this example:

I told you that the value of y is going to be 1. Did you figure out what this operator does? Well it actually takes the remainder. I’ve always been pretty bad at visualizing a remainder, so I use imagery. Imagine you have a pizza with 5 slices and you want to share the pizza with your pet rat, Pepper Jack.

Well you give a piece to Pepper Jack, a piece to you, a piece to pepper jack, and then another piece to you. Now, you have one left over:

Now you might suggest just ripping the pizza in half, but you have to realize that piecesOfPizza is an integer. This means that we cannot split the piece in half.

This brings up the good point that the modulus operator is designed to work with integers. That’s because if you were using numbers of type double here, you and your pet rat would both get 2.5 pieces of pizza and the remainder would be zero. The modulus operator would be useless working with floating point numbers.

You can do a lot of cool things with the modulus operator. One example is figuring out if a number is even or odd. if you divide a number by 2 and the remainder is anything but 0, then you do not have an odd number.

That is all for the modulus operator. In the next video we are going to learn our first unary operator.

In the next blog we are going to be discussing all Unary Plus and Minus. Check it out!

The post C Programming Tutorial 32 – Modulus Operator appeared first on Caleb Curry.

]]>Did you read the last blog on Intro to Operators? Are you new here? Start at the beginning, Intro to C! This blog is going to discuss the arithmetic operators. Now, we’ve discussed these some already and I’m sure you are familiar with them from math classes you’ve taken so this blog is really not going to …

Continue reading "C Programming Tutorial 31 – Arithmetic Operators"

The post C Programming Tutorial 31 – Arithmetic Operators appeared first on Caleb Curry.

]]>Did you read the last blog on Intro to Operators? Are you new here? Start at the beginning, Intro to C!

This blog is going to discuss the arithmetic operators. Now, we’ve discussed these some already and I’m sure you are familiar with them from math classes you’ve taken so this blog is really not going to be that hard or confusing!

The **arithmetic operators** include the +, -, /, and *. These are all binary operators that need two operands.

The / is called a forward slash. Some people call \ a forward slash and they are wrong! The top of the slash is the reference point, not the bottom. The top is leaning forward, therefore / is called the forward slash.

The * is called an astersk. I like to call it capital 8, but I’m weird.

Now, when you have an expression like this:

First the multiplication and division happens from left to right, whichever comes first. This means that 3 * 4 happens first. The expression could be simplified to 2 + 12 / 3 – 2. The next thing to happen is 12 / 3. The expression could be simplified to 2 + 4 – 2. Next, the addition and subtraction happens, whichever comes first from left to right. So we get 6 – 2 which evaluates to 4.

You may find it more clear if we did this:

You can use as many parenthesis as you want and wherever you want, as long as you remember to close any parenthesis you open. They always get evaluated from the inside out. So 3 * 4 happens first, then the result of that gets divided by 3.

Now, you can also use parenthesis to force a certain part of the expression to happen first. For example, if we wanted the subtraction to happen first, we could have set it up like this:

Let’s output this and make sure we get the result we expect.

Some people choose to use parenthesis for nearly everything because they believe without them it is easier to make mistakes with arithmetic, but this is up to you.

Now, I’m going to introduce you to a new operator…

Check out this expression.

Now, I can tell you now that the value of y is going to be 1. Can you figure out what this operator does? We’ll find out in the next video.

In the next blog we are going to be discussing all of the Modulus Operator. Check it out!

The post C Programming Tutorial 31 – Arithmetic Operators appeared first on Caleb Curry.

]]>