Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento
front end book you'll ever need. Get your copy
today!

This program uses a function named printf. The f in printf stands for format. While you can use printf to print a string (as we have above), the real power of printf is in its ability to print strings and format them using values provided by variables.

If you compile and run this program, you’ll see output that looks like the following

Our variable contains the value 42

You’ll notice we passed printftwo arguments. The first was the string we wanted to print. The second was a variable. You’ll also notice our string included a funny looking %i character sequence. In the final printed string, this %i was swapped out for the value of the variable we passed in. This is what formatting means — it’s a very basic templating system for replacing variables in strings.

If you wanted to use two variables, you’d just include another %i and pass a third argument to the function.

Once again, C’s strict typing system rears its head. Rather than turn your 4.321 into an int, or rather than have some generic template replacement, C forces you to specify exactly what type you’ll be using. C does this because casting a variable has performance implications, and C is biased towards not making these sorts of decisions for you.

Mixing Types

One of the challenges you’ll face as a C programmer is understanding the consequences of using two different types in the same sort of operation. For example — consider the following program.

Here we declare two integers (x, y) and we divide the first by the second. Also, since 5 divided by 2 is 2.5, we declare a float variable named answer to hold the results. However, if you run the above program, you’ll get the following output.

Answer: 2.000000

What gives? Why does C think 5 divided by 2 is 2.0?

The problem is, we divided an integer by an integer. Since both parts of the division operation were integers, C used integer division to derive a result. Again, C errors on the side of doing the most efficient thing, even if it’s not the most obvious thing. We may have been smart enough to declare a float to hold our value, but we weren’t smart enough to remember that C can be weird when types mix. The solution is to ensure either the numerator or the divisor in our example is also a float.

While this works for our simple example, in a real program changing the type of a variable that’s used elsewhere may have unforeseen consequences. Fortunately C (like most programming languages) has a mechanism that will allow us to temporarily convert a variable of one type into other. This mechanism is known as casting. To cast the integer x as a float, we’d do this

This program is identical to our first example, with the exception of this

((float) x) / y;

Before we use x in our division operation, we put the (float) cast in front of it. This will treat xfor this operation only as a floating point number.

Lost Information

Casting from an int to a float is a relatively safe operation. However, what about casting a floating point number like 2.5 to an integer. What integer would that be? Two? Or three? This is the danger of casting — you often introduce ambiguities into your program. If we give this a try

Nine? How does that make any sense?! Well — it doesn’t until we consider the ASCII Text Encoding table. The character 9 is the 57th entry in this table. At some point a c systems developer decided that casting an int as a char would turn it into its corresponding text character.

This isn’t the only example of chars being somewhat type fluid. Consider the following example

Here we’re performing a mixed type addition with ints and chars. If we look at the output.

Answer Int: 117
Answer Char: u

we see that C treated our char as a number again. If you’re coming from a language like javascript, you might expect the + to do some sort of concatenation. That’s not going to work in C.

Wrap Up

Closing with javascript concatenation does (once again) raise the specter of strings. So far we’ve been focused on numbers and individual characters — but we’ve avoided how C deals with silly human things like words and text.

Next time we’ll start our trek towards strings in C, but before we can reach that summit we’ll need to stop at basecamp, and cover an even more important C concept: Arrays.