Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of Cs standard input/output library. To access it: #include printf Function printf(The area is %d, and the perimeter is %d.\n, area, pm); The printf function is called in order to display program output. Inside the parenthesis are the functions arguments. The arguments of printf include a format string (given in double quotes) followed by a print list. If the variable area has a value 24 and the variable pm has a value 20, above function call displays the line: The area is 24, and the perimeter is 20. printf Function

format string The first argument to printf is a format string. It must be enclosed in quotes( ). Most of the format string is displayed literally on the output. The format string may contain placeholders. A placeholder begins with the symbol % and indicates that the value of the corresponding item in the print list should be substituted at that point. The character following the % tells the computer what kind of value to expect and how to display it. printf Function Placeholders in Format Strings Different conversion characters are used in placeholders for different variable types: %c %d %f

%lf %e %E char int double (used with printf) double (used with scanf) double, to print in scientific notation double, to print in scientific notation Format strings may contain multiple placeholders. There must be a corresponding value in the print list for each placeholder. printf(The area is %d, and twice the area is %d.\n, area, 2 * area); result: The area is 24, and twice the area is 48.

printf Function Displaying Prompts Whenever input data is needed in an interactive program, the program should first display a prompt. This informs the user what data to enter, along with other information the user needs. Usually a prompting message is simply text and requires no placeholders or print list. Examples: printf(Please enter an integer value for length: ); printf(Enter a code, a-c, or d to quit: ); Both of these will place the cursor immediately following the string displayed, waiting for the user to enter data. printf Function Using the newline character Whenever format string contains the newline character (\n), the cursor is advanced to the beginning of the next line. Often

the format string will end with the newline character so that subsequent output begins on the next line. However the newline may appear anywhere in the format string. Example: printf(Here are some\nlines of output,\n\nincluding a blank line.\n); result: 1 blank line here Here are some lines of output, including a blank line. printf Function Formatted Numeric Output Each placeholder (conversion specification) is introduced by the % and ends with a conversion character. Between them may be:

A digit string specifying minimum field width. The number will be printed in a field at least this wide, and longer if necessary. If longer than needed, number will be right justified and padded with blanks to the left. e.g. %5d For double values, may additionally or instead have a period and a second digit string specifying the number of digits to be printed to the right of the decimal point. e.g. %5.2f printf Function Formatted Numeric Output Value 200 200 -3

562 976.29 976.29 .342 99.99 -.002 Format %d %5d %5d %2d %10.3f %7.1f %5.2f %5.1f %.3f -3.14159 %.3f

97.86 %10.2E -0.0432 %.1e Printed Output 200 200 -3 562 (Note: Field too small) 976.290 976.3 0.34 100.0 -0.002 -3.142 9.79E+01 -4.3e-02 Note: In scientific notation, four places of field width

are taken for the exponent. scanf Function scanf(%d%d, &length, &width); The first argument to scanf is the format string (in double quotes), followed by an input list. Each int, double, or char variable in the input list is preceded by an ampersand (&). The & is the address-of operator. It tells C where to find the variable (in the memory) into which it will store values. The placeholders in the format string must correspond to the order of the variables in the input list. scanf Function The data required by scanf will come from the standard input device. Usually this is the keyboard. When scanf is executed, the program pauses until requested data is entered and the or key pressed. If too much data provided, the extra characters will be saved

for the next call to scanf. If not enough data provided, program will continue to pause until more data entered and or key again pressed. If wrong type of data (characters when numeric required) youll get an error. scanf Function Recognizing Data %c only one input character is used, even if it is a blank. Upper and lower case letters are different. %d program skips leading blanks, then read

digits until it finds non-digit. Puts corresponding number in the int variable. %lf program skips leading blanks, then looks for characters which can be converted to a double value. May have optional sign, a string of digits possibly with a decimal point, then an optional exponent field containing E or e followed by possibly signed integer. (conversion character e is a synonym for f). scanf Function Recognizing Data char a; int n, m;

double x, y; input line 25 62.91 36B scanf(%d%lf%d%c,&n,&x,&m,&a); n 25 x 62.91 m 36 a BB scanf(%lf%d%c%d%lf, &x,&n,&a,&m,&y); x 25.0 n 62 a B. m 91 y 36.0 Note: the character B is left for the next scanf request.

Recently Viewed Presentations

  • Sanitasi Makanan & Minuman

    Sanitasi Makanan & Minuman

    Upaya penyimpanan bahan makanan. Sangat dipengaruhi oleh faktor suhu dan kelembapan --->> kerusakan bahan makanan. Kerusakan bahan makanan: Kerusakan biologis dan mikrooranganisme -->> ulat, tikus, serangga, bakteri, dan jamur
  • Application of SPARROW Results for Developing TMDLs in

    Application of SPARROW Results for Developing TMDLs in

    Acknowledgments VT DEC - Tim Clear, Neil Kamman EPA New England - Eric Perkins, Matt Liebman EPA GIS Center - Tom Pearson, Deb Cohen USGS - Rich Moore, Keith Robinson NEIWPCC - Laura Blake Project Objectives Show use of SPARROW...
  • Fast Effective Clustering for Graphs and Document Collections

    Fast Effective Clustering for Graphs and Document Collections

    Fast Effective Clustering for Graphs and Documents William W. Cohen Machine Learning Dept. and Language Technologies Institute School of Computer Science Carnegie Mellon University Joint work with: Frank Lin and Ramnath Balasubramanyan
  • Cell Respiration - Morgan Park High School

    Cell Respiration - Morgan Park High School

    Cell respiration is used by all cells to produce ATP ... Summary Equations . Cell Respiration = glucose pyruvate . ... Some desert animals never have to drink water and eat only dry foods because cell respiration supplies almost all...
  • WELCOME CLASS of 2022!

    WELCOME CLASS of 2022!

    Romeo and Juliet--William Shakespeare. Fahrenheit 451--Ray Bradbury. The Pearl--John Steinbeck. The House on Mango Street--Sandra Cisneros. Advanced: Great Expectations--Charles Dickens; Jane Eyre--Charlotte Bronte. The reading project is another 4 books for your students.
  • video slide - blogs

    video slide - blogs

    Paramecium are oval-shaped protists covered with hair-like cilia all over their cell membrane. The cilia move together like oars to propel the paramecium. Paramecia ingest their food through an opening called the oral groove. When enough food has accumulated in...
  • School Identification in 2018-2019: CSI and ATS Informational

    School Identification in 2018-2019: CSI and ATS Informational

    Additional Targeted Support (ATS)--A school is identified for ATS if it has a subgroup that if on its own would be identified if the CSI criteria were applied. Note: Targeted Support and Improvement (TSI)--the bottom 5% of schools based on...
  • Chapter 4 Heredity Preview Section 1 Mendel and

    Chapter 4 Heredity Preview Section 1 Mendel and

    Gregor Mendel was born in 1822 in Heinzendorf, Austria. At age 21, Mendel entered a monastery. He performed many scientific experiments in the monastery garden. Mendel discovered the principles of heredity, the passing of traits from parents to offspring. Section...