Primitive Built-in Types. Variables and Basic Types. C++: Arithmetic Types. Signed and Unsigned Types

Variables and Basic Types Primitive Built-in Types command to submit homework 1 is $submit hachreka homework1 filename ˆ ˆ command to submit Proj...
0 downloads 0 Views 137KB Size
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