Variables and Basic Types
Primitive Built-in Types
command to submit homework 1 is $submit hachreka homework1 filename
command to submit Project 1 is $submit hachreka project1 filename
suppose you wrote a gaming application tic-tac-toe as Project 3 implementation. Store all of your code in a folder say "tictactoe". Zip it (say tictactoe.zip). command to submit Project 3 is $ submit hachreka project3 tictactoe.zip
C++ defines a set of arithmetic types, e.g., integers, floating-point numbers, and individual characters and boolean values Special type: void has no associated values size of the arithmetic types varies across machines the standard guarantees a minimum size for each of the arithmetic types, but it does not prevent compilers from using larger sizes
1
C++: Arithmetic Types Type Meaning bool boolean char wchar_t short int long float double long double
2
Signed and Unsigned Types Minimum Size NA
character
8 bits
wide character
16 bits
short integer
16 bits
integer long integer
16 bits 32 bits
single-precision floating-point
6 significant digits
double-precision floating-point
10 significant digits
integral types, except the boolean type, may be either signed or unsigned a signed type can represent both negative and positive numbers (including zero) an unsigned type represents only values greater than or equal to zero
e.g., unsigned long, unsigned char (0-255)
extended-precision floating-point 10 significant digits 3
4
1
Floating-Point Types
Literal Constants
types float, double, and long double represent floating-point single-, double-, and extendedprecision values floats are represented in one word (32 bits) doubles in two words (64 bits) long double in either three or four words (96 or 128 bits) float is guaranteed to offer only 6 significant digits
a value, such as 42, is known as a literal constant literal because we can speak of it only in terms of its value constant because its value cannot be changed every literal has an associated type
for example, 0 is an int and 3.14159 is a double
literals exist only for the built-in types. There are no literals of class types true and false are literals of type bool:
bool test = false;
5
Escape Sequences for Nonprintable Characters
provides us with named storage that our programs can manipulate each variable in C++ has a specific type, which determines
e.g., backspace or a control character
Other characters have special meaning in the language,
6
Variables
Some characters are nonprintable
20 // decimal 024 // octal 0x14 // hexadecimal
e.g., single and double quotation marks, and the backslash.
the size and layout of the variable's memory
the range of values that can be stored within that memory
the set of operations that can be applied to the variable
the name of a variable, its identifier, can be composed of letters, digits, and the underscore character. name must begin with either a letter or an underscore. Identifiers in C++ are case-sensitive.
7
8
2
Declarations and Definitions
Scope of a Name
definition of a variable allocates storage for the variable and may also specify an initial value for the variable we can declare a name without defining it by using the extern keyword an extern declaration is not a definition and does not allocate storage it claims that a definition of the variable exists elsewhere in the program an extern that is initialized is treated as a definition
names can be used more than once in a program a name can be reused as long as it is used in different contexts scope: the context used to distinguish the meanings of names a scope is a region of the program. A name can refer to different entities in different scopes. most scopes in C++ are delimited by curly braces
extern double pi = 3.1416; // definition 9
Scope of a Name
10
Scope in C++ Nest
#include int main() { int sum = 0; // sum values from 1 up to 10 inclusive for (int val = 1; val