cs31 lecture 3

12 not ok not integer 6 be careful x what are

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: / 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 Be extra cautious: analyze the input to ensure the range of Be variables! variables! 7 float x float: an arbitrary number, with decimals float: x Range: from –infty to +infty Precisely: range is roughly [-1038, 1038] for float x Two types of floating point numbers in C++: float double (double precision, use twice more bits) x Rounding happens Why? Finite set of numbers holds “infinite” set of numbers x IEEE 754 implementation can lead to weird corner cases 8 float: examples float f = 0; // ok float f = 0.0; // ok float f = 0.0f; // ok float f = -0; // ok float f = 42; // ok float f = 12312434123.0f; // ok double f = -42; // ok double 9 char x An arbitrary character, from the ASCII table x Standard ASCII: 128 characters x Extended ASCII: 256 characters x Range: [-128,127] x Possible qualifier: unsigned x Use single quote to set a value using a character Examples: char x = ‘a’; char char x = 97; char 10 A program in C++ x A program is made of functions x There must be a main function, which is called when the There program starts program x This function has a specific name and prototype int main() { some code goes here… some } 11 Syntax of a function <return type> <name> ([arguments]) { } x<return type> is the type of the data returned by the <return function function Examples: int, float, void x<name> is the name of the function 12 A typical simple main function int main() { /// list of instructions: / return 0; return } xThe fun...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online