Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
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.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (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.
Data types and variables
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
© 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.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
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.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
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.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
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.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
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.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Programming Fundamentals
Input/output.
TMF1414 Introduction to Programming
Revision Lecture
Unit-4, Chapter-2 Managing Input and Output Operations
Introduction to C CSE 2031 Fall /3/ :33 AM.
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.
Formatted Input/Output
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
A First Book of ANSI C Fourth Edition
Chapter-3 Operators.
Chapter 4 Managing Input and Output Operations
Formatted Input/Output
Formatted Input/Output
Introduction to C EECS May 2019.
Introduction to C Programming
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Presentation transcript:

Chapter-4 Managing input and Output operation

 Reading, processing and writing of data are three essential functions of a computer program.  Most programs take some data as input and display the processed data or result as output.  So, we need some methods that can read input and write output on a suitable input/output medium.

 Bcoz unlike other high-level languages, C does not have any built- in input/output statements as part of its syntax.

Input/output functions  Thus far we have seen two methods for providing data to the program variables. 1.One is to assign values to variables through the assignment statements such as x = 5; and so on. 2. And another method is to use input function scanf which can read data from a terminal. And for outputting the results we have used the function printf which sends the results out to a terminal.

 There are also some functions other than printf and scanf which can be used as input/output functions.  The simplest of all input/output operations is reading a character from the standard input and writing it to the standard output unit. Reading a character

 Reading a single character can also be done by using the function getchar(). The getchar() function The getchar() function is used to read a single character from the standard input unit. It takes the following form: variable_name = getchar();

 Variable_name is a valid C name that has been declared as char type.  When this statement is encountered, the computer waits until a key is pressed and then assigns this character as a value to the getchar function.

Example Since getchar is used on the right-hand side of an assignment statement, the character value of getchar is assigned to the variable name on the left side. For example: char name; name = getchar(); Will assign the character ‘A’ to the variable name when we press the key A on the keyboard.

 Here, We Will assign the character ‘ A ’ to the variable name when we press the key A on the keyboard.

 The getchar() function may be called successively to read the characters contained in a line of text.  The getchar() function returns any character keyed in. this include RETURN and TAB also. This means that it will not return, until u press ENTER.

Other functions  We can also determine whether the character typed from the keyboard is a letter or digit.  These tests can be done with the help of the following functions: isalpha(character) isdigit(character)

 isalpha function assumes a value non- zero(TRUE) if the argument character contains an alphabet; otherwise it assumes 0(FALSE). Similar for isdigit function.

 These all character functions are contained in the file ctype.h and therefore the statement #include Must be included in the program.  There are many other similar functions contained in this file.

1.isalnum(c) - Is c an alphanumeric character? 2.isalpha(c) – Is c an alphabetic character? 3. isdigit(c) – Is c a digit? 4. islower(c) – Is c a lower case letter? 5. isprint(c) –Is c a printable character? 6. isspace(c) –Is c a white space character? 7. isupper(c) – Is c an upper case letter?

Writing a character  Like getchar, there is an analogous function putchar for writing characters one at a time to the terminal.  It takes the following form: putchar(variable_name);

 where variable_name is a type char variable containing a character. This statement displays the character contained in the variable_name at the terminal.

Example: char answer = ‘Y’; putchar(answer);  It will display the character Y on the screen.  The statement putchar(‘\n’); would cause the cursor on the screen to move to the beginning of the next line.

Formatted Input  Formatted input refers to an input data that has been arranged in a particular format.  For inputting the data, we use the scanf function. The general form of this scanf is :

Its.. scanf( “ controlString ”,arg1,arg2, … argn);  Conrol string specifies the field format in which the data is to be entered.  Arguments arg1, arg2, …..specify the address of locations where the data is stored.

 The field (or format) specifications, consisting of the conversion character %, a data type character(or type specifier), and an optional number, specifying the field width.

Inputting Integer Numbers  The field specification for reading an integer number is: %wd  w is an integer number that specifies the field width of the number to be read and d, known as data type character.

Example:- scanf(“%2d%5d”,&num1,&num2); Input:  Then, num1num2

But if input : num1num2 And the value 50 that is unread will be assigned to the first variable in the next scanf call.

 On the other way if we have written : Scanf(“%d%d”,&num1,&num2); Will read the data Correctly in num1, num2.  Input data items must be separated by spaces,tabs or new lines.  When the scanf reads a particular value, reading of the value will terminate as soon as the number of characters specified by the field width is reached(if specified) or until a character that is not valid for the value being read is encountered.

 An input field may be skipped by specifying * in the place of field width. For example, Scanf (%d%*d%d”,&a,&b); Input: a skipped b The data character d may be preceded by ‘ l ’ to read long integers. (%ld)

 The field width of real numbers is not to be specified and therefore scanf uses simple specification %f for both the notations, decimal point and exponential notation.  For double type, %lf is used instead of %f.  A number may be skipped using %*f specification. Inputting Real Numbers

Inputting Character Strings  Single character can be read from the terminal using getchar function. The same can be achieved using the scanf function also.  In addition, a scanf can input strings containing more than one character.

 The specifications for reading character strings are: %ws or %wc  %wc used to read a single character while %ws terminates the reading at the encounter of blank space.

 All function arguments must be a pointers to variables.  Format specifications contained in the control string should match the argument order.  Input data items must be separated by spaces and must match the variables receiving the input in the same order.

 The reading will be terminated, when the scan encounters an ‘ invalid mismatch ’ of data or a character that is not valid for the value being read.  When searching for a value, scanf ignores line boundaries and simply looks for the next appropriate character.  Any unread data items in a line will be considered as a part of the data input line to the next scanf call.

 When the field width specifier w is used, it should be large enough to contain input data size.

 It is highly desirable that the outputs are produced in such a way that they are understandable and are in an easy-to-use form.  It is therefore necessary for the programmer to give careful consideration to the appearance and clarity of the output produced by program.

 printf statement provides certain features that can be effectively control the alignment and spacing of print- outs on the terminals.

printf( “ control string ”,arg1,arg2....argn); Control string consists of three types of items: 1.Characters that will be printed on the screen as they appear. 2.Format specifications that will define the output format for display of each item.

3.Escape sequence characters such as \n, \t, and \b.

 The format specification for printing an integer is %wd. Where w specifies the minimum field width for the output. Format Output  Printf(“%d”,9876)  Printf(“%6d”,9876)  Printf(“%2d”,9876)

 Printf(“%-6d”,9876)  Printf(“%06d”,9876)

 The output of real number may be displayed in decimal notation using the format %w.pf If y = Format Output Printf(“%7.4f”,y) Printf(“%7.2f”,y) Printf(“%-7.2f”,y) Printf(“%f”,y)

Printing of a single character  A single character can be displayed in a desired position using the format %wc.  The character will be displayed right- justified in the field of w columns.  We can make the display left-justified by placing a minus sign before w.  The default value for w is 1.

Printing of strings  The format specification for outputting string is of the form %w.ps.  To print string “ NEW DELHI ”, containing 16 characters. %20s

To print string “ NEW DELHI ”, containing 16 characters. %20s %20.10s NEWDELHI NEWDELHI

Difference between %[a-z] %[^a-z]

sizeof operator  when used with operand, it returns the no. of bytes the operand occupies… i.e. sizeof(int) is returns 2 sizeof(char)returns 1..on 16bit processor. Q.float x;printf(“%d”,sizeof(x)); what will be the output?Why?

Precedence of arithmetic operator High priority * / % Low priority+ - The basic evaluation includes left to right passes….. During first pass higher priority opr is applied as they encounter….during second pass from left to right the low priority operators are applied as they are encountered.

example i.e x = 9-12/3+3*2-1; pass 1:x = 9 – 4 + 3*2 –1; x = 9 – –1; Pass 2: x= 5 +6 –1; x=11-1; x=10;

Use of parenthesis to override normal precedence X= 9 – 12 / (3+3) *(2-1) Pass 0:x=9 – 12 / 6 * (2-1) x=9 – 12 / 6 * 1; Pass 1:x=9 – 2 * 1; x=9 – 2; Pass 2:x=7 Thus, when parenthesis are used, the expressions within parenthesis assume highest priority.

Associativity The operator having same precedence are evaluated either from left to right or right to left…..depending of the level ….known as associativity.

 There are two ways for type conversion. 1) Implicit Type Conversion 2) Explicit Type Conversion

 Automatic conversion. e.g. a=3 b=2 int i; float a,b; i=a/b; here, value of i will be 1. instead of 1.5

 (type-name) expression Where, type-name is one of the standard C data types. Expression may be a constant, variable or an expression.

 i=( int ) 7.5 Means, 7.5 is converted to integer by truncation.  i=( int )21.3/ ( int )4.5 Means, Evaluated as 21/4 and result would be 5.

 Casting can be used to round-off a given value. HOW??? x = (int) (y+0.5); if y = 27.6 then, y+0.5=28.1 and on casting the result becomes 28.