Notes

The math blocks you've used in Scratch can all be recreated in C!

Numerical data in C is classified into several different types. From a variable’s type, you’ll be able to tell what kind of data it can store, how much data it can store, and which operations can be performed on this data. An int is a whole number. The appliance uses 32 bits to store an int .

A float is a number with a decimal point. The appliance uses 32 bits to store a float .

A double is a number with a decimal point, but with more space for precision after the decimal point. The appliance uses 64 bits to store a double .

A long long is a whole number that is twice as big in memory as an int . The appliance uses 64 bits to store a long long .

Here, we sum two variables of type int. Let's think very carefully about what's happening as these three lines of code execute: The first line declares an int named x . More precisely, it asks the operating system for enough memory to store an int and gives that chunk of memory the name x .

The second line initializes x by assigning it a value of 2. Remember that in C, a single equal sign is called an assignment operator. Whatever's to the right of the equal sign will be stored in the variable to the left of the equal sign.

The third line declares and initializes an int named y in a single line of code.

Let’s try some division next, and store the result of our division in a float because we know it will be a decimal value. %.2f tells the OS to print a floating-point number, but only to two decimal places. What will print? 0.00 Why? 1 and 10 are ints. In C, an int divided by an int is an int and any decimal places will be truncated off.

We can fix this! We can use this syntax (desired data type within parentheses) to cast the ints 1 and 10 to floats. This is called typecasting. A float divided by a float is a float, which preserves decimal places! What will print this time? 0.10

Another way to accomplish the same thing is to replace the ints 1 and 10 with floats 1.0 and 10.0. This method also results in the printing of 0.10.

Remember "order of operations" from elementary school? The same concept applies in programming and is called operator precedence. 1. 27 2. 22 3. 10

The modulo operator gives you the remainder of the division of the first number by the second. 1. 5 2. 3 3. 0 4. 1

What will be printed out? 2