Chapter 4 Managing Input and Output Operations

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

1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
© 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.
1 CSE1301 Computer Programming: Lecture 9 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.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
 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.
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.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
C Tokens Identifiers Keywords Constants Operators Special symbols.
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.
Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp.
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.
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-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
CSE1301 Computer Programming: Lecture 6 Input/Output.
© 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.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Formatted Input and Output
Chapter 9 C Formatted Input/Output
Formatted Input/Output
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Input/output.
INTRODUCTION Every language has some features that provides interaction between the program and the user of the program. C language uses the reference.
TMF1414 Introduction to Programming
Revision Lecture
Unit-4, Chapter-2 Managing Input and Output Operations
Introduction to C CSE 2031 Fall /3/ :33 AM.
Formatted Input/Output
By: Syed Shahrukh Haider
OUTPUT STATEMENTS GC 201.
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.
Formatted Input/Output
CSI 121 Structured Programming Language Lecture 7: Input/Output
توابع ورودي-خروجي.
Formatted Input/Output
A First Book of ANSI C Fourth Edition
Chapter 5 Decision Making and Branching
Chapter 9 - Formatted Input/Output
Week 2 Variables, flow control and the Debugger
Formatted Input/Output
Formatted Input/Output
Introduction to C EECS May 2019.
Lexical Elements & Operators
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 Operations PROGRAMMING IN ANSI C

# include <stdio.h> # include "stdio.h" 2/22/2019 Chapter 4 C hasn’t any built-in input/output statements as part of its syntax. All of input/output operations are carried out through standard input/output functions. e.g. printf( ) scanf( ) getchar( ) gets( ) printf( ) putchar( ) puts( ) # include <stdio.h> # include "stdio.h" standard input and output

2/22/2019 getchar( ) & putchar( ) Form: variable = getchar(); putchar(character); #include <stdio.h> main() { char c ; c = getchar ( ); putchar ( c ); putchar ( getchar () ); printf ( "%c", getchar () ); putchar ('D'); } #include <stdio.h> main() { char c ; c = getchar ( ); putchar ( c ); putchar ( getchar () ); printf ( "%c", getchar () ); putchar ('D'); printf("%d", getchar()); } abc a b c D 10

printf( ) – Formatted Output 2/22/2019 printf( ) – Formatted Output Form: printf("control string", arg1, …, argn); e.g. printf("price=$%.2f\n, amount=%d.", pr*am, am); Control string The characters that will be outputted as they appear. e.g. price amount = $ , . Escape sequence characters. e.g. \n Format specifications. e.g. %f, %d

printf( ) – Formatted Output 2/22/2019 printf( ) – Formatted Output Form: printf("control string", arg1, …, argn); e.g. printf("price=$%.2f\n, amount=%d.", pr*am, am); The outputted expression list: (arg1, …, argn) There can be no any expression or more than one expression. They are separated by comma. In spite of what type these expressions are, they are always outputted in specified format. printf("%c, %d", 97, 'a'); Output: a, 97 printf("%d, %u", 32767+1, 32767+1); Output: -32768, 32768

printf( ) – Formatted Output 2/22/2019 printf( ) – Formatted Output printf ("%c, %c",65, 'A'); printf ("%s","Hello!"); printf ("%f",123.45); printf ("%%"); printf ("%g",123.450); printf ("%e",12.3); printf ("%o, %O",-3, 'A'); printf ("%u, %U",-3, 'A'); printf ("%x, %X",-3, 'A'); printf ("%d, %i",-3, 'A'); Format specifications Output: -3, 65 Output: % Output: 123.45 Output: fffd, 41 Output: 1.230000e+01 Output: 123.450000 Output: 177775, %O Output: A, A Output: Hello! Output: 65533, %U 1 %d, %i Signed decimal integer 2 %x, %X Unsigned hexadecimal integer (without leading 0x) 3 %o Unsigned octal integer (without leading 0) 4 %u Unsigned decimal integer 5 %c Single character 6 %s Sting 7 %f Real number in decimal notation 8 %e, %E Real number in exponential notation 9 %g, %G Real number either f-type or e-type depending on the length of the value without insignificant zero 10 %% %

printf( ) – Formatted Output 2/22/2019 printf( ) – Formatted Output Accessorial format specifications 1 w Specifies the minimum field width for the output. 2 .p To real number, specifies the number of digits after the decimal point (rounded). To string, instructs the first p characters to be outputted. To integer or single character, it is invalid. 3 l Precede d, i, o, x, u to outputs long type integer. Precede f, e, g to outputs double type real number. 4 - Left-justified, and remaining field will be blank. printf ("%4.2s", "abc"); printf ("%ld, %d", 65536, 65536); printf ("%2c, %-2c", 'A', 'A'); printf ("%.1f, %.1f", 1.25, 1.251); printf ("%3d", 12); printf ("%3f", 12.3); printf ("%.1f", 12.36); Output: 12.300000 Output: 12 Output: 12.4 Output:   ab Output: 65536, 0 Output: 1.2, 1.3 Output: A, A

scanf( ) – Formatted Input 2/22/2019 scanf( ) – Formatted Input Form: scanf ("control string", arg1, …, argn); e.g. scanf("%d,%c", &num, &ch); format specifications %d, %i, %o, %x, %u, %c, %s, %f, %e, %g the same as those in printf function.

scanf( ) – Formatted Input 2/22/2019 scanf( ) – Formatted Input e.g. scanf("%d%*d%d", &a, &b); input: 1234567 result: 12a, 67b e.g. scanf("%3d%*2d%f", &i, &f); input: 123456.789 result: 123i, 6.789f Form: scanf ("control string", arg1, …, argn); e.g. scanf("%d,%c", &num, &ch); Accessorial format specifications e.g. scanf("%3d%f", &i, &f); input: 12345.6789 result: 123i, 45.6789f 1 h Precede d, i, o, u, x to read short type integer. 2 l Precede d, i, o, x, u to read long type integer. Precede f, e, g to read double type real number. 3 w Specify the field width of the data to be read. 4 * Specify the input field to be skipped.

scanf( ) – Formatted Input 2/22/2019 scanf( ) – Formatted Input e.g. scanf("%d%d%f", &n1, &n2, &f); input: 12345  67.89 result: 12n1, 345n2, 67.89 f Form: scanf ("control string", arg1, …, argn); e.g. scanf("%d,%c", &num, &ch); Separators When it read in the integers or real numbers, it defaults blank spaces, tabs or newlines as separators.

scanf( ) – Formatted Input 2/22/2019 scanf( ) – Formatted Input e.g. scanf("%d, %d", &a, &b); input: 12, 345  result: 12a, 345b input: 12345  result: 12a, nothingb e.g. scanf("a=%d, b=%d", &a, &b); input: a=12, b=345  result: 12a, 345b input: 12, 345  result: nothinga, nothingb Form: scanf ("control string", arg1, …, argn); e.g. scanf("%d,%c", &num, &ch); Separators It also can specify separators, that is those characters except format specifications. In this way, the input must accord with the specified format, otherwise the input most probably is read in by errors.

scanf( ) – Formatted Input 2/22/2019 scanf( ) – Formatted Input Form: scanf ("control string", arg1, …, argn); e.g. scanf("%d,%c", &num, &ch); Separators When it read in a character, the blank space, tab and newline character are no longer separators, but as a valid character. e.g. scanf("%d%c", &i, &ch); input: 12a result: 12i, '' ch

scanf( ) – Formatted Input 2/22/2019 scanf( ) – Formatted Input e.g. scanf("%d%d%f", &n1, &n2, &f); input: 12345  67.89 result: 12n1, 345n2, 67.89 f Form: scanf ("control string", arg1, …, argn); e.g. scanf("%d,%c", &num, &ch); How to judge a data is finished? If “scanf” read an integer of real number, when it encounters a blank space, or tab, or newline character, it considers the number is finished.

scanf( ) – Formatted Input 2/22/2019 scanf( ) – Formatted Input e.g. scanf("%3d%f", &i, &f); input: 12345.6789 result: 123i, 45.6789f e.g. scanf("%d%c%f", &a, &b, &c); input: 123a456o.78 result: 123a, 'a'b, 456.0c Form: scanf ("control string", arg1, …, argn); e.g. scanf("%d,%c", &num, &ch); How to judge a data is finished? When the input data achieves the specified field wide, “scanf” considers the data is finished. When “scanf” encounters an illegal character, it considers the data is finished.

Program 1 Read in 2 numbers, exchange them, then output them. 2/22/2019 Program 1 Read in 2 numbers, exchange them, then output them. Step1: Declare 3 float variables – x, y and temp. Step2: Read 2 real numbers into x and y. Step3: Exchange them: x -> temp (temp = x;) y -> x (x = y;) temp -> y (y = temp;) Step4: Output the value of x and y.

Program 1 Please input x and y: 4 5.6  2/22/2019 Program 1 Please input x and y: 4 5.6  Before exchange: x=4.00, y=5.60 After exchange: x=5.60, y=4.00 main() { float x, y, temp; printf ("Please input x and y:\n") ; scanf ( "%f%f", &x, &y ); printf ("Before exchange: x=%.2f, y=%.2f\n", x, y); temp = x ; x = y; y = temp; printf ("After exchange: x=%.2f, y=%.2f", x, y); }

2/22/2019 Program 2 Read in a lowercase letter, convert it into its uppercase equivalent, and then output the uppercase and its ASCII value. Step1: Declare a char type variable ch. Step2: Read in a lowercase letter into ch. Step3: Convert it into its uppercase equivalent. ch = ch – 32; or ch = ch – ('a' - 'A'); Step4: Output ch and its ASCII value.

Program 2 Please input a lowercase: m  2/22/2019 Program 2 Please input a lowercase: The lowercase is m, and its ASCII value is 109 The uppercase equivalent is M, and its ASCII value is 77 m  #include <stdio.h> main() { char ch; printf("Please input a lowercase:") ; ch = getchar(); printf("The lowercase is %c, and its ASCII value is %d\n", ch, ch); ch = ch - 32; printf("The uppercase equivalent is:%c, and its ASCII value is %d\n", ch, ch); }

2/22/2019 Homework Review Questions P106 4.1, 4.2, 4.4, 4.5 (Write down in your exercise book) Programming Exercises