Basic Types, Variables, Literals, Constants

Basic Types, Variables, Literals, Constants

Basic Types, Variables, Literals, Constants What is in a Word? A byte is the basic addressable unit of memory in RAM Typically it is 8 bits (octet) But some machines had 7, or 9, or ... A word is the basic unit of operation by the CPU Most registers are this size Largest unit of transfer between RAM and CPU in single instruction What is a Type? A type is a qualifier that is

used by the compiler Machine languages do not have types The type of a variable or constant tells the compiler: How much space the object occupies What operations on the object mean What is a Type? Given an address in RAM, what does it mean? Could be anything! Types tell the compiler Which instruction to apply

Integer addition, floating pt addition How to increment pointers Array references, fields some Arithmetic Types in C++ Type Meaning Minimum Size bool Boolean

NA char character 8 bits wchar_t wide character 16 bits short short integer

16 bits int integer 16 bits long long integer 32 bits long long very long integer

64 bits float 1-prec. floating point 6 (7) sig. digits double double-prec. FP 10 (16) sig. digits Which Type to Use? General Usage int most integer arithmetic

double for floating point computation char only for chars bool only for Booleans Unsigned Used to save space who cares? Do not mix signed and unsigned! Can cause headaches easily - avoid Type Conversion Casting, or conversion Needed when different type expected Compiler handles automatically Bool LHS = {false if 0, true if non-0}

RHS = {0 if false, 1 if true} Type Conversion Integer Floating point number Truncate FPN when int on LHS Fractional part 0 when int on RHS Can lose precision Out-of-Range LHS unsigned, then residue mod size LHS signed, then undefined (bad) What is a Literal? A literal is a fixed, explicit value that is known at compile time Can be used to initialize

variables Can be used to initialize constants Can be used in expressions Generally bad programming style It may be int, char, bool, etc. Special Characters Some characters are not printable Some characters have special meaning to the language For these, we need escape sequences All start with backslash \ Some predefined: \n newline

Special Characters newline \n horizontal tab \t alert (bell) \a vertical tab \v backspace \b double quote \ backslash

\\ carriage return \r question mark \? single quote form feed \f Can use as single character: std::cout << '\n'; std::cout << \tHello!\n; Generalized escape sequence: \12 = \014 = x0c = newline in decimal, octal, hex Note: only the first 3 octal digits are accepted Note: hex uses all the following digits (!) \' Special Literals Boolean true false Pointer nullptr preferred literal 0 NULL (must #include cstdlib) Never any other Variables

A variable is a logically named, typed, structured piece of storage Name allows us to refer to the stored structure Type allows us to know structure Variables can be assigned new values Variables Definition: allocates space for storage Declaration: specifies name and type So variable can be referenced here and defined elsewhere Type var_name, var_name, ;

All vars have type given at start Good practice: one per line of Variable Definition int sum = 0, value, total = 0; // all type int /* sum and total initialized to 0 */ Sales_item item; /* type Sales_item initialized to default value */ std::string name(Dr. Newman); /* string is a type from std library

variable length character sequence * Initialization Good idea: ALWAYS INITIALIZE!!!! Initialization object gets value at time of definition (when created) May be any expression that can be evaluated at time of creation Name becomes visible immediately Hence can be used in subsequent initializations in same line! Variable Initialization int i = 0, j = 2*i; /* j init uses value of i immediately */

int k = sizeof(double); /* value is a function that can be evaluated when k is defined */ List Initialization int i = 0; int i(0); int i = {0}; int i{0}; /* i initialized with literal value */ /* here also */ /* i initialized with literal value, but restricted */

/* same here */ double pi = 3.14; int a{pi}, b = {pi}; int c(pi), d = pi; /* floating pt */ /* fail - requires narrowing */ /* OK, but value truncated */ Declaration vs. Definition Definition allocate space

Declaration state type and name Name can be used in current file Makes promise it will be defined later extern int i; /* declares but doesn't Only define in define ONE file i*/ int j; // declares and defines j Identifiers Identifier = name for variable, function, constant, class, type, etc. Cannot be a keyword in C++ Identifiers may be composed of

letters, digits, and underscore char Must begin with _ or letter Identifiers are case-sensitive C++ Keywords alignas alignof asm auto bool break case catch char char16_t char32_t class

const constexpr const_cast continue decltype default delete do double dynamic_cast else enum explicit export extern false

float for friend goto if inline int long mutable namespace new C++ Keywords noexcept nullptr operator private protected public

register reinterpret-cast return short signed sizeof static static_assert static_cast struct switch template this thread_local throw true try

typedef typeid typename union unsigned using virtual void volatile wchar_t while C++ Alternative Operator Names and and_eq bitand

bitor compl not not_eq or or_eq xor xor_eq Identifier Conventions Identifier should hint toward purpose Constants are all upper case Variables are lower case Classes start with upper case Multi-word identifiers should

distinguish each word using a capital or underscore Sales_item, booksSold, totalRbis Scoping When is a name visible/usable? Most scopes delimited by {} blocks Names can be reused across scopes Global scope defined outside a fcn Block scope accessible from point defined onward within block

Recently Viewed Presentations

  • XML, XSL, and SOAP

    XML, XSL, and SOAP

    Arial Verdana Wingdings Bold Stripes XML, XSL, and SOAP New Web Technologies XML, the Extensible Markup Language Components of XML Example XML Document Example DTD A More Complex DTD XSL - Extensible Stylesheet Language Thinking of XSL A Simple XSL...
  • Ancient Rome -

    Ancient Rome -

    Geography of Italy. Present-day Italy is a peninsula, often called a "boot" because of its shape, that juts out into the Mediterranean Sea. The city of Rome is located on the west coast of Italy in the middle of the...
  • Perspective on Parallel Programming

    Perspective on Parallel Programming

    Hit, invalidate x1/x2 in P2 False miss; x1 irrelevant to P2 False miss; x1 irrelevant to P2 True miss Hit, invalidate x1/x2 in P2 * * MP Performance 4 Processor Commercial Workload: OLTP, Decision Support (Database), Search Engine True and...
  • How to win the 3 minute thesis

    How to win the 3 minute thesis

    The competition asks you to present a oration about what you are doing, how you are doing it and why in a way that engages the audience, without reducing research to purely entertainment value. You do not have to explicitly...
  • Anatomy of the neck

    Anatomy of the neck

    Anatomy of the neck By Dr. Rasha Sabry Muscular triangle Outlines : Infront : midline of the neck Behind and above :superior belly of omohyoid muscle Behind and below : the sternomastoid muscle Contents of the Muscular triangle It contains...
  • Political Parties 27 October, 2008 The 2008 US

    Political Parties 27 October, 2008 The 2008 US

    Political Parties 27 October, 2008 0 The 2008 US Presidential Election: A Roundtable Event Sponsored by the Department of Politics & the Centre for Elections, Media and Political
  • IP Multicast - UCY

    IP Multicast - UCY

    IP Multicast Protocols. Definitions. A multicast group is a set of receivers with a common interest. A source is an end user that originates a data stream. ... Links cannot be shut down in routed WANs. Routing protocols need some...

    Unit 1 Warm-Ups & Wrap-Ups. Day 1 Warm-Up: Cell Phone Problems. List at least 5 things you do not like about your cell phone. ... Objective: Use Geometry terms to communicate with precision. Final Presentation Requirements. Day 4 Activity #2....