Stream 3 - which is even more likely to work, with the...

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

View Full Document Right Arrow Icon
Stream I/O Suppose that you wish to output three values and you use some C-style output to do so: printf("%d %d %d\n", a, b); What is wrong here? Well, the output specification calls for three integer values to be output, but only two were specified. You can probably "get away" with this usage without your program crashing, with the printf() routine picking up a garbage value from the stack. But many cases of this usage will crash the program. A similar case would be: printf("%d %d %d\n", a, b, c, d);
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: which is even more likely to work, with the extra argument ignored. This problem is intrinsic to printf() and related functions. Using stream I/O as illustrated above eliminates this particular problem completely: cout << a << " " << b << " " << c << "\n"; as well as the related problem illustrated by: int a; printf("%s\n", a); where the argument is of the wrong type. Stream I/O is fundamentally safer than C-style I/O; stream I/O is said to be "type safe"....
View Full Document

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University.

Ask a homework question - tutors are online