A variable is a container inside the computer that can contain a value such as
true
or
false
Four definitions:
variable1.C
:
define, declare, and initialize a variable
all in one statement.
Then output the contents of the variable.
cout << "The variable contains price.\n";
variable2.C
:
Assign a new value to the variable.
variable3.C
:
Old-fashioned (pre-2011) way to define a variable.
variable4.C
:
What’s the biggest number this type of variable can hold?
maxInt
is “camel case”,
EXIT_SUCCESS
and
numeric_limits
are “snake case”.
bc
,
the Linux “binary calculator”.
The keystroke
control-d
tells
bc
that you’re not planning to type in any more arithmetic problems for it.
jsmith@storm:~$ bc -l 2^31 2147483648 control-d jsmith@storm:~$See Why do the possible values for an int go from -2,147,483,648 to +2,147,483,647?
variable5.C
:
input a value from the outside world and store it into a variable.
Check that the input was successful.
jsmith@storm:~$ cd jsmith@storm:~$ pwd jsmith@storm:~$ ls -l jsmith@storm:~$ wget https://markmeretzky.com/fordham/1600/src/variable/variable5.C jsmith@storm:~$ c++ variable5.C jsmith@storm:~$ ls -l jsmith@storm:~$ ./a.out Please type the price and press RETURN. 13 The variable contains 13. jsmith@storm:~$ echo $? 0 (EXIT_SUCCESS stands for this number.) jsmith@storm:~$ ./a.out Please type the price and press RETURN. hello Sorry, that wasn't an acceptable number. jsmith@storm:~$ echo $? 1 (EXIT_FAILURE stands for this number.)Exercise. What happens if you try to input an out-of-bounds value such as
2147483648
or
-2147483649
into the variable?
What is the invisible exit status number?
variable5.C
)
from an input file
(thirteen.txt
),
instead of from the keyboard.
First create a very small file containing the two digits
13
and a newline,
a total of just three characters.
We could create this little file with
vi
,
but it’s easier to create it with
echo
.
jsmith@storm:~$ cd jsmith@storm:~$ pwd jsmith@storm:~$ ls -l jsmith@storm:~$ echo 13 (echo is the Linux "print" command.) 13 jsmith@storm:~$ echo 13 > thirteen.txt jsmith@storm:~$ ls -l (There should be a new file named "thirteen.txt".) jsmith@storm:~$ cat thirteen.txt 13 jsmith@storm:~$ ./a.out < thirteen.txt Please type the price and press RETURN. The variable contains 13.
jsmith@storm:~$ ./a.out < thirteen.txt > variable5.txt jsmith@storm:~$ ls -l jsmith@storm:~$ cat variable5.txt Please type the price and press RETURN. The variable contains 13.