Chapter 4 Variables in C
So far we have dealt with printing text to the screen that was given (and stored) in our program’s C-code. We will now delve into printing numbers and results of calculations to the screen. Next we will learn about the concept of a variable. You can think of a variable as a storage location that can hold hold numbers or characters for example and to which you can refer by a name of your choosing. Variables allow us to write programs that behave differently, depending on the value stored in this variable.
We’ll learn how to capture and store in a variable input from the user. We’ll also discuss common errors when working with variables as well as the use of variables in loops.
Since we may want to store data of a variety of types (such as numbers, text, etc.) we’ll learn about different data types, in particular integers (
int
), characters (char
) and floating point numbers (float
).We’ll finish this chapter by discussion the way in which results of a division might vary depending on the data type being used.