Module-E- Libraries1/49 Module E- Standard Libraries Input and Validation Formatted Output Library Functions.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Input, Output, and Processing
Functions Why we use functions C library functions Creating our own functions.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CS 1704 Introduction to Data Structures and Software Engineering.
Sudeshna Sarkar, IIT Kharagpur 1 I/O in C + Misc Lecture –
Files A collection of related data treated as a unit. Two types Text
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Topics Designing a Program Input, Processing, and Output
Chapter 9 C Formatted Input/Output
C Characters and Strings
Input/output.
TMF1414 Introduction to Programming
Introduction to C CSE 2031 Fall /3/ :33 AM.
I/O in C + Misc Lecture Sudeshna Sarkar, IIT Kharagpur.
Programming in C Input / Output.
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
A First Book of ANSI C Fourth Edition
Chapter 9 - Formatted Input/Output
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
A First Book of ANSI C Fourth Edition
Differences between Java and C
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to C EECS May 2019.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Presentation transcript:

Module-E- Libraries1/49 Module E- Standard Libraries Input and Validation Formatted Output Library Functions

Module-E- Libraries2/49 Input and Validation Types of Input getchar scanf Validation In-Class Practice

Module-E- Libraries3/49 Types of Input Input to a program may be unbuffered or buffered Interactive program uses unbuffered input. The program can respond to each and every keystroke directly. Buffered input enables data editing before submission to a program. That is, the program accepts one complete input record at a time rather than one keystroke at a time. A buffer is a region of memory that collects and holds data temporarily.

Module-E- Libraries4/49 Buffered Input To transfer the contents of a buffer to a program the user must press the '\n' character. Two C functions provide buffered input facilities on the standard input stream: –getchar –scanf

Module-E- Libraries5/49 getchar getchar retrieves a single character from the standard input stream buffer without translating the input. int getchar ( void ); getchar returns either the character code for the retrieved character or EOF. (EOF=-1, ctrl+z in Windows, ctrl+d in Unix)

Module-E- Libraries6/49 Clearing the buffer /* clear empties input buffer */ void clear (void) { while ( getchar() != '\n' ) ;/* null statement intentional */ }

Module-E- Libraries7/49 Formatted input: scanf (1) int scanf(const char *format, arg-list) The prototype for scanf( ) is in. The scanf( ) function is a general-purpose input routine that reads the stream stdin. It can read all the built-in data types and automatically convert them into the proper internal format. It is much like the reverse of printf( ). The control string pointed to by format consists of three types of characters: - Format specifiers - White-space characters - Non-white-space characters The format specifiers are preceded by a percent sign and tell scanf( ) what type of data is to be read next. For example, %s reads a string while %d reads an integer.

Module-E- Libraries8/49 scanf (2) The format string is read left to right, and the format codes are matched, in order, with the arguments that make up the argument list. A white-space character in the control string causes scanf( ) to skip over one or more white-space characters in the input stream. A white-space character is either a space, a tab, or a newline. In essence, one white-space character in the control string causes scanf( ) to read, but not store, any number (including zero) of white-space characters up to the first non-white-space character. A non-white-space character causes scanf( ) to read and discard a matching character. For example, "%d,%d" causes scanf( ) to read an integer, read and discard a comma, and then read another integer. If the specified character is not found, scanf( ) terminates.

Module-E- Libraries9/49 scanf (3) All the variables used to receive values through scanf( ) must be passed by their addresses. This means that all arguments must be pointers to the variables used as arguments. This is C’s way of creating a call by reference, and it allows a function to alter the contents of an argument. For example, to read an integer into the variable count, you would use the following scanf( ) call: scanf("%d", &count); Strings are read into character arrays, and the array name, without any index, is the address of the first element of the array. So, to read a string into the character array address, use scanf("%s", address); In this case, address is already a pointer and need not be preceded by the & operator.

Module-E- Libraries10/49 scanf (4) Conversion Specifiers

Module-E- Libraries11/49 scanf (5) - Conversion Specifiers Code Meaning %c Read a single character %d Read a decimal integer %D Read a long integer (C++ Builder specific) %i Read a decimal integer %I Read a long integer (C++ Builder specific) %e Read a floating-point number %E Read a floating-point number %f Read a floating-point number %g Read a floating-point number %G Read a floating-point number %o Read an octal number %O Read an long octal number (C++ Builder specific) %s Read a string %x Read a hexadecimal number %X Read a hexadecimal number %p Read a pointer %n Receives an integer value equal to the number of characters read so far %u Read an unsigned integer %U Read an unsigned long integer (C++ Builder specific) %[ ] Scan for a set of characters % Read a % sign

Module-E- Libraries12/49 scanf (6) scanf treats the whitespace between the input values as a separator int items; float price; printf("Enter items, price : "); scanf("%d%f", &items, &price); Enter items, price: 3 5.2

Module-E- Libraries13/49 scanf (7)

Module-E- Libraries14/49 scanf (8) Return Values –scanf returns the number of addresses successfully filled or EOF. A return value of 0 indicates that scanf did not fill any address, 1 indicates that scanf filled the first address successfully, 2 indicates that scanf filled the first and second addresses successfully,... EOF indicates that scanf did not fill any address AND encountered an end of data character. The return code from scanf does not reflect success of %* conversions

Module-E- Libraries15/49 Validation We cannot predict how the user will input the data values: whether the user will enter them as requested or not. One user may make a mistake. Another user may simply try to break the program. We write the program so that it traps all erroneous input, which includes: –invalid characters –trailing characters –out-of-range input –incorrect number of input fields

Module-E- Libraries16/49 Validation example

Module-E- Libraries17/49 In-Class Practice Design and code a function named getDouble that receives two double values - a lower limit and an upper limit - and returns user input that lies between the limiting values. Your function rejects any input that includes trailing characters or lies outside the specified limits.

Module-E- Libraries18/49 Summary Input and Validation Types of Input getchar scanf Validation In-Class Practice Q&A

Module-E- Libraries19/49 Output putchar printf

Module-E- Libraries20/49 putchar putchar writes the character received to the standard output stream buffer and returns the character written or EOF if an error occurred. Prototype: int putchar ( int ); For example: putchar('a');

Module-E- Libraries21/49 Formatted output: printf printf sends data under format control to the standard output stream buffer and returns the number of characters sent. Syntax printf ( format string, value,..., value ) The format string is a literal string that consists of characters interspersed with conversion specifiers. Conversion specifier begins with a % and ends with a conversion character

Module-E- Libraries22/49 Format string Between the % and the conversion character, there may be % flags width. precision size conversion_character flags - prescribes left justification of the converted value in its field 0 pads the field width with leading zeros size identifies the size of data type of the value passed.

Module-E- Libraries23/49 Conversion Specifiers

Module-E- Libraries24/49

Module-E- Libraries25/49 Summary Formatted Output putchar printf Q&A

Module-E- Libraries26/49 Library Functions The standard C libraries include functions to perform mathematical calculations, character analysis and character manipulation.

Module-E- Libraries27/49 Session Objectives The standard C libraries Standard Time In-Class Practice Math Character

Module-E- Libraries28/49 Standard (1) The prototypes for the more popular standard functions are in. Integer Absolute Value int abs ( int ); long labs ( long );

Module-E- Libraries29/49 Standard (2) Random Number rand returns a pseudo-random integer in the range 0 to RAND_MAX. RAND_MAX is implementation dependent but at least int rand ( void ); For example, the following program outputs the same results every run:

Module-E- Libraries30/49 Standard (3) The following program prints 10 pseudo- random integers between 6 and 100 inclusive:

Module-E- Libraries31/49 Standard (4) The following program prints 10 pseudo- random floating-point numbers between 3.0 and inclusive:

Module-E- Libraries32/49 Standard (5) Random Number Seed –srand sets the seed for the random number generator. int srand ( unsigned int seed ); –We call srand before the first call to rand, typically at the start of our program. We use time(NULL) to generate a unique time-based seed for each new run.

Module-E- Libraries33/49 Standard-5 The following program outputs a different set of random numbers with every run: The prototype for time is in

Module-E- Libraries34/49 Time (1) The prototypes for the time library functions are in. time returns the current calendar time. The prototype is time_t time ( time_t *tptr ); –time_t is a type that it is sufficiently large to hold time values (for example, unsigned long). –time also assigns the current calendar time to *tptr, if tptr is not NULL. –If an error occurs, time returns the value (time_t)(-1).

Module-E- Libraries35/49 time (2) difftime difftime returns the difference in seconds between two calendar time arguments. double difftime ( time_t, time_t ); –time_t is a type that it is sufficiently large to hold time values (for example, unsigned long).

Module-E- Libraries36/49 time (3)

Module-E- Libraries37/49 clock (1) clock returns the approximate process time. The prototype is clock_t clock ( void ); clock_t is a type that holds time values (for example, unsigned long). We divide the time value by CLOCKS_PER_SEC to obtain the process time in seconds.

Module-E- Libraries38/49 clock (2)

Module-E- Libraries39/49 In-Class Practice Design and code a program named coinToss.cpp that –prompts the user for the number of times to toss a coin, –accepts a positive value as the number of times, –simulates the coin tosses for the requested number of times, and –reports the percentage of head results and the percentge of tail results for the coin tosses.

Module-E- Libraries40/49 Math (1) The prototypes for the math functions are in. Floating-Point Absolute Value fabs, fabsf returns the absolute value of the floating-point value received. Their prototypes are double fabs ( double ); float fabsf ( float );

Module-E- Libraries41/49 Math (2) floor floor, floorf return the largest integer value not greater than the value received. Their prototypes are double floor ( double ); float floorf ( float ); For example, floor(16.3) has a value of 16.0.

Module-E- Libraries42/49 Math (3) Ceiling ceil, ceilf return the smallest integer value not less than the value received. Their prototypes are double ceil ( double ); float ceilf ( float ); For example, ceil(16.3) has a value of 17.0.

Module-E- Libraries43/49 Math (4) Rounding round, roundf return the integer value closest to the value received. Their prototypes are double round ( double ); float roundf ( float ); For example, round(16.3) has a value of 16.0, while round(-16.3) has a value of

Module-E- Libraries44/49 Math (5) Truncating trunc, truncf, truncl return the integer component of the value received. Their prototypes are double trunc ( double ); float truncf ( float ); long double truncl ( long double ); For example, trunc(16.7) has a value of 16.0, while round(-16.7) has a value of

Module-E- Libraries45/49 Math (6) Square Root sqrt, sqrtf, sqrtl return the square root of the floating-point value received. Their prototypes are double sqrt ( double ); float sqrtf ( float ); long double sqrtl ( long double ); For example, sqrt(16.0) has a value of 4.0.

Module-E- Libraries46/49 Math (7) Powers pow, powf, powl return the result of the first floating-point value received raised to the power of the second floating-point value. Their prototypes are double pow ( double base, double exponent ); float powf ( float base, float exponent ); long double powl ( long double base, long double exponent );

Module-E- Libraries47/49 Math (8) Logarithms log, logf, logl return the natural logarithm of the floating-point value received. Their prototypes are double log ( double ); float logf ( float ); long double logl ( long double ); For example, log( ) has a value of 1.0.

Module-E- Libraries48/49 Math (9) Powers of e exp, expf, expl return the natural anti- logarithm of the floating-point value received. Their prototypes are double exp ( double ); float expf ( float ); long double expl ( long double ); For example, exp(1.0) has a value of

Module-E- Libraries49/49 Character (1) The prototypes for character analysis and character manipulation functions are in. In checking for a true value, we check that the value is not false: that is, value != 0, rather than assume that true is represented by unity

Module-E- Libraries50/49 Character (2) int islower ( int ); int isupper ( int ); int tolower ( int ); int toupper ( int ); int isalpha ( int ); int isdigit ( int ); int isspace ( int ); (Whitespace characters are ' ', '\t', '\n', '\v', and '\f'. ) int isblank ( int ); (space or tab )

Module-E- Libraries51/49 Summary The standard C libraries Standard Time Math Character Q&A

Module-E- Libraries52/49 Practice