LEC20080918 - Introduction to Computer Programming...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

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

Unformatted text preview: Introduction to Computer Programming September 18, 2008 CSC180 Fall 2008, University of Toronto Formatted input: scanf() Required readings: chapter 4. Experiment with all examples and exercises in this lecture. Standard Input/Output Library function scanf() s prototype: extern int scanf (__const char *__restrict __format, ...); Input analog of printf() scanf() reads input from standard input , interprets it according to conversion specifications in format string, and stores the results in format (pointer) arguments. In short, scanf() reads values from keyboard. CSC180 Fall 2008, University of Toronto 1 Standard input means keyboard by default. Standard input is actually a file pointed by stdin , which is opened automatically each the program starts up. scanf(%d, &n) is equivalent to fscanf(stdin, %d, &n) , where fscanf() has the following prototype: extern int fscanf (FILE *__restrict __stream, __const char *__restrict __format, ...); We can imagine scanf() as a pattern matching function that matches format string with keyboard input. Premature termination happens when scanf() reads a conflicting character or reaches end of file. In UNIX, standard input is actually a file. CSC180 Fall 2008, University of Toronto 2 scanf() returns number of successfully read values, or return EOF (a signal indicating end of file). Unread characters can be read by next input function. scanf() requires two matches between format string and format arguments, as printf() does. Major difference with printf() : scanf() requires addresses (or pointers) as its format arguments. Wrong (segmentation fault): int n; scanf(%d, n); /* WRONG */ CSC180 Fall 2008, University of Toronto 3 Correct: scanf(%d, &n); Why do we put & before n ? C passes arguments to functions by value. scanf() cannot change n s value if we pass n instead of &n . Ill talk about this in more detail later. For now, you can experiment with the following example. Example (reset n to 0): /* pass ns address */ /* pass ns value */ void reset(int *n) void reset2(int n) { { *n = 0; n = 0; } } CSC180 Fall 2008, University of Toronto 4 int n = 1; int n = 1; reset(&n); /* succeed */ reset2(n); /* fail */ printf(%d\n, n); printf(%d\n, n); CSC180 Fall 2008, University of Toronto 5 scanf() continued ... When the conversion specification is for reading a number, scanf() (1) Skips preceding whitespace characters, and (2) Matches as much as possible until it encounters a conflicting character; or it reaches end of file; or it has read enough characters as specified in minimum field width....
View Full Document

This note was uploaded on 01/10/2011 for the course CSC 180 taught by Professor Na during the Fall '01 term at University of Toronto- Toronto.

Page1 / 40

LEC20080918 - Introduction to Computer Programming...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online