Here I'll show you how you can store and retrieve data in variables, as well as how to use constant values that won't change but will help with code readability and reduce human error.

Transcript

Now that you know the diverse types of data, let's go through how to store it. The code I'll be using is available on the workshop page, if you want to follow along with me.

To store data, we use variables and to create a variable we need a type and a name. So, for instance here;

I've got the type of "int" and the name "A" and what this does is it reserves a space in memory to store what we want.

For "B", I've done the same thing the type of "int" and the name of "B", but I've also set its first value to be 0. This is an innovative idea because if you don't set the value, it will use whatever binary data happened to be left in that space at the time and convert it to its value. So, you'll see here that the default value of "A" is just over 4,000,000. If you want to change an existing variable, we don't need to use the type again we just use its name, so for instance "B" = 2.

For "C", I used a constant value, that I created at the top using #define the constant name and the value. The constant name is generally all caps and anything from the end of the name to the end of the line becomes the value. You'll notice that I didn't use a semicolon because this would become part of the value and would break our code.

For "D", I'm simply putting a decimal value in a double type.

Whereas in "E", I'm using scientific notation, which can be very handy if you have very large or very small numbers.

For "F", we're putting a negative number into a signed value.

With "G", we're putting the same negative value into an unsigned variable. This has the effect of going below its minimum value and rolling background and starting at its maximum value and working backwards. As you can see over here it's gone to 65,000.

For "H", I'm showing you how to use hexadecimal notation, which is a zero and X and then the value in base 16.

For "I" on the other hand, I'm using octal notation or base 8 and that is simply just a 0 at the beginning of your number.

Now floating-point types behave slightly differently to integer types, where if you exceed the minimum or maximum value of an integer, it wraps around to the other end. In a floating-point type, you get an error of negative or positive infinity, as you can see here with "J".

Finally, if you would like to use the return value of a function like I have here with the square root function from the math library. All you need to do is use that function in place of the value you would normally put in a variable.

Like I said this code is available on the webshop page. So, if you follow along and have any questions feel free to put them on the forum and I'll answer them for you.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.