| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

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

  • Some compilers support greater ranges of values, but these are the minimum values.

 

Modifiers

  • "Modifies" the properties of the data type in order to make it better fit a particular situation

    • Modifiers for int: signed, unsigned, short, long

    • Modifiers for char: signed, unsigned

    • Modifiers for double: long

Signed Variables

  • Signed refers to weather or not the variable stores information about if the value is + or -

    • Think absolute value

  • 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

  • Stores only 1 or 0 which correspond to "true" or "false"

  • Any non-zero value is assumed to mean "true"

Code: Prime Numbers

 

Void

  • Valueless expression

 

Literals

  • Essentially constants

  • Can be numbers or strings

Numerical Literals

  • Must be expressed as a decimal

    • No fractions

  • 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

    • Octal

    • Hexadecimal

 

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

  • A way of typing special characters

  • Also called backslash character constants

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;"

    • type is the data type

    • varName is the variable's name

  • Variables can never change data type

  • You can also declare a value when declaring the variable

    • type varName = "value";

  • 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


 

 

Comments (0)

You don't have permission to comment on this page.