19_pdfsam_lecture2 - More on printf Placeholders can also specify widths and precisions e.g d add spaces to take up at least 10 characters 0d add

Info iconThis preview shows pages 1–6. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: More on printf Placeholders can also specify widths and precisions, e.g. %10d : add spaces to take up at least 10 characters %010d : add zeros to take up at least 10 characters %.2f : print only 2 digits after decimal point %5.2f : print 1 decimal digit, add spaces to take up 5 chars int i = 90; float f = 3.0; printf(%5d roads\n, 42); printf(i = %06d, f = %5.2f\n, i, f); sss42 roads i = 000090, f = 3.00 Printf has many other features! Check API online. Warning about printf printf is powerful, but potentially dangerous What does this code do? int i = 90; float f = 3.0; printf(i = %d, f = %f\n, i); printf(%d roads\n, 42, f); printf(i = %d, f = %f\n, f, i); i = 90, f = (junk) 42 roads i = 1074266112, f = 0.000000 Reading input from keyboard scanf is the opposite of printf Syntax: scanf(format_string, val1, val2, . . . ); Tries to parse input according to format_string Like printf, format_string includes placeholders that species how values should be parsed int I; printf(enter an integer: scanf(%d, &I); ); Note the & before the variable name. This is required! Passes a pointer to the variable I, instead of the value of I. We'll talk much more about this later. More scanf examples Read a oat from the keyboard float F; printf(enter a float: scanf(%f, &F); ); Parse a date into month, day, year int month, day, year; printf(enter a date: ); scanf(%d/%d/%d, &month, &day, &year); scanf editorial scanf is powerful, but awkward and dangerous. Error handling is dicult What does this code do? int I; printf(enter an integer: scanf(%d, I); ); Use it for now. We'll see better ways of handling input later. Variables Variables have a name and a type Restrictions on variable names Must begin with a letter Can contain letters, digits, and underscores ( _ ) Can't be a reserved word (if, else, void, etc.) Only the rst 31 characters matter C has 4 basic built-in types char, int, oat, double ...
View Full Document

This note was uploaded on 01/10/2010 for the course CS 2022 at Cornell University (Engineering School).

Page1 / 6

19_pdfsam_lecture2 - More on printf Placeholders can also specify widths and precisions e.g d add spaces to take up at least 10 characters 0d add

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

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