Variables, Memory, and Operators

Variables are an important concept in all computer programming languages, but are specially so in C because of its heavily-typed nature. The reader needs to understand how memory space is allocated for variables in C and how the heavy typing imposed by C

  • PDF / 316,884 Bytes
  • 20 Pages / 504.567 x 720 pts Page_size
  • 60 Downloads / 219 Views

DOWNLOAD

REPORT


Variables, Memory, and Operators

Now that we are past the nearly trivial first chapter, we’re going to crank up the level of “interestingness” a bit more. In this chapter, we will work on defining and declaring variables and performing some simple operations on them. In the process, you will gain some additional experience with the output function printf().

1

Variables – A Brief Introduction

In its most basic terms, a computer program instructs the computer in which it resides to acquire, manipulate and output data in some form or another. Therefore, data manipulation can be said to be an essential part of computing. Computers cannot sing or dance, but they can manipulate data to display a singer or a dancer, and even control a dancing robot. To do that, data must be placed somewhere while they are being manipulated and then stored somewhere after they have been processed. Variables are the most basic element with which to do this in a computer program. They are designed to store and retrieve data easily. The concept of a variable in algebra is that a mathematical term (a symbol) holds a computable value that can be changed, hence its name variable. The symbol that represents the variable is itself non-computable. For example, one cannot add the characters a and b, as such an operation in mathematics is in and of itself meaningless. However, if the a and b symbols (variables) were to somehow stand for numerical values, the operation could replace the symbols a and b by their numerical values, which would be added, and carry out the operation successfully. In algebra, a variable can be assigned a specific value, or it can be left unbound (i.e., it holds no value ... yet). In computer programming, however, variables are not the highly abstract concepts that they are in algebra. Instead, they are actual locations in the computer’s memory where a value can be stored, retrieved and then later re-stored after being processed (if that is what the program calls for). This could happen many times during the course of a program’s execution. Memory in a computer is a veeeeeeeeeery looooooooong string of binary elements called bits, each of which can hold either a 0 or a 1. Fortunately, a computer’s memory is equipped with an indexing system (beyond the scope of this book) that allows any location in this long string to be found rather efficiently. Thus, locations in memory have addresses where a particular segment of memory can be found, just like a street address (house number, street name, city, state and zip code) can uniquely pinpoint the location of a small house in a big country. Unfortunately, addresses in computer memory © Springer Nature Switzerland AG 2020 A. J. Gonzalez, Computer Programming in C for Beginners, https://doi.org/10.1007/978-3-030-50750-3_2

15

2  Variables, Memory, and Operators

16

are quite cryptic, making it difficult for a programmer to keep track of what address in memory holds a particular value of interest. Fortunately, high-level programming languages such as C make it easy on the pr