cs31 lecture 17

42 nt 4 basic types in c x how to determine the data

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: , can assign a value at declaration x Examples: int foo; iint bar = 42; nt 4 Basic types in C++ x How to determine the data type for a variable? x Simple: look for the basic type in C++, and find a match x Basic types: iint: a number which is an integer nt: • ex: 12345, 0, -42 float: an arbitrary number (integer or floating point)
 • ex: -1.33, -42.0, 42.0, 0.0 ex: char: a single character • ex: ‘a’, ‘b’ ex: 5 iint: examples nt: int x = 42; // ok int int x = 0; // ok int x = 421234234535; // error (out of range)
 int x = -1; // ok unsigned x = -1; // error (-1 is signed)
 unsigned int x = -42; // ok int int x = 42.12; // not ok (not integer)
 6 Be careful! x What are semantics errors may not be reported as errors What by the compiler! by x Reason: the C++ standard defines what to do for those Reason: cases cases Example: unsigned int x = -1 is defined, will hold maxint! x Be extra cautious: it is the duty of the programmer to Be determine the range of variables x B...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online