|
Data Types
Page history
last edited
by Lucas Wojciechowski 15 years, 7 months ago
Data Types
-
A variable's data type determines what operations are allowed with the variable
-
It also determines the range and type of values that can be stored
-
In C++ all variables must be declared ahead of time
-
The variable types allow code to be more efficient, working with numbers the same way as the processor
Data Type
|
Meaning
|
Range
|
char
|
Character (Letter)
|
-127 to 127
|
wchar_t
|
Wide Character
|
0 to 65535
|
int
|
Integer
|
-32767 to 32767
|
float
|
Floating Point Value
|
1e-37 to 1e37 with 6 sigfigs
|
double
|
Double Floating Point Value
|
1e-37 to 1e37 with 10 sigfigs
|
bool
|
Boolean (True or False)
|
0 or 1
|
void
|
Valueless
|
--
|
long int
|
Extra Long Integer
|
-2147483647 to 2147483647
|
Modifiers
Signed Variables
-
Signed refers to weather or not the variable stores information about if the value is + or -
-
The first bit of the data is interpreted as a 0 or 1 which corresponds to a + or -
-
"unsigned x" is the same as "unsigned int x"
Integers
-
Can't hold fractions or decimals
-
Used for controlling loops and conditional statements and counting.
-
If not otherwise specified, they are assumed to be signed.
Floating-Point Variables
-
Allow for decimals unlike integers
-
Allow for very large or small numbers
-
Basically store the value in scientific notation
-
Two types: Double and Float
-
Double can hold 10 times the value of float
-
Double is the most common variable type because most libraries are written assuming variables are the double type
Code: Double Data Type
Boolean
Code: Prime Numbers
Void
Literals
Numerical Literals
-
Must be expressed as a decimal
-
Can be in scientific notation
-
Compiler can automatically chose the most efficient data type for the value you provide.
-
Data type can also be manually chosen with a suffix on the value
-
Values can be stored in different number systems
String Literals
-
A set of characters enclosed in double quotes
-
There is no string data type, instead strings are stored as an array of characters
Character Escape Sequences
b
|
backspace
|
f
|
form feed
|
n
|
new line
|
r
|
carriage return
|
t
|
horizontal tab
|
"
|
double quote
|
'
|
single quote
|
|
backslash
|
v
|
vertical tab
|
a
|
alert
|
?
|
question mark (?)
|
N
|
octal constant
|
xN
|
hexadecimal constant
|
Declaring Variables
-
General form is "type varName;"
-
Variables can never change data type
-
You can also declare a value when declaring the variable
-
Variables can be initialized at any time during the program
- Called Dynamic Initialization
Type Conversion
- Bits can be gained and lost if different types are incorrectly converted to eachother
- Need to use a "cast" to change data type without data loss
(type) expression
(float) x/2
Data Types
|
Tip: To turn text into a link, highlight the text, then click on a page or file from the list above.
|
|
|
Comments (0)
You don't have permission to comment on this page.