Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.

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

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 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
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.
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",
 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.
 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.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
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.
GE 211 Programming in C Dr. Ahmed Telba Room :Ac -134
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CSE1301 Computer Programming: Lecture 6 Input/Output.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
© 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.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Lecture 3: Getting Started & Input / Output (I/O)
Chapter 9 - Formatted Input/Output
Lecture2.
C Formatted Input/Output
CSCE 206 Structured Programming in C
Formatted Input and Output
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
Chapter 9 C Formatted Input/Output
Formatted Input/Output
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Input/output.
TMF1414 Introduction to Programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Getting Started with C.
Formatted Input/Output
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.
Formatted Input/Output
Programming in C Input / Output.
CSI 121 Structured Programming Language Lecture 7: Input/Output
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Formatted Input/Output
A First Book of ANSI C Fourth Edition
Chapter 9 - Formatted Input/Output
Department of Computer and Information Science
Lecture3.
Programming in C Input / Output.
Formatted Input/Output
Formatted Input/Output
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Presentation transcript:

Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering

Topics Streams Streams printf function printf function scanf function scanf function

Data storage Data can be stored in two different ways: Data can be stored in two different ways: –by assignment to a variable –copying data from an input device into a variable using a function like scanf

Input operation Sometimes, it is need to copy data into a variable for a program to manipulate different data each time it executes Sometimes, it is need to copy data into a variable for a program to manipulate different data each time it executes –This data transfer from the outside world into memory is called an input operation

Output operation As the program executes, it performs computations and stores the results in memory As the program executes, it performs computations and stores the results in memory These program results can be displayed to the user by an output operation These program results can be displayed to the user by an output operation

Input/Output Program

Input/output functions All input/output operations in C are performed by special program units called input/output functions All input/output operations in C are performed by special program units called input/output functions You can gain access to most of the operations through the preprocessor directive You can gain access to most of the operations through the preprocessor directive –#include –#include

Input/output functions In C, a function call is used to call or activate a function In C, a function call is used to call or activate a function Calling a function is analogous to asking a friend to perform an urgent task Calling a function is analogous to asking a friend to perform an urgent task

Streams Text input or output is dealt with as a sequence of characters Text input or output is dealt with as a sequence of characters A stream serves as a channel to convey characters between I/O and programs A stream serves as a channel to convey characters between I/O and programs

Streams: Input -- Example _ \n int item; float cost; scanf(“%d %f”, &item, &cost); input buffer

Streams: Input -- Example (cont) _ \n int item; float cost; scanf(“%d %f”, &item, &cost); itemcost

Streams: Input – Example (cont) _ 25.5\n int item; float cost; scanf(“%d %f”, &item, &cost); item 135 cost

Streams: Input – Example (cont) _ \n int item; float cost; scanf(“%d %f”, &item, &cost); item 135 cost 25.5

Streams: Output -- Example Hello!\n printf(“Hello!\n”); output buffer

Hello!\n printf(“Hello!\n”); Streams: Output – Example (cont)

ello!\n H printf(“Hello!\n”); Streams: Output – Example (cont)

llo!\n He printf(“Hello!\n”); Streams: Output – Example (cont)

lo!\n Hel printf(“Hello!\n”); Streams: Output – Example (cont)

o!\n Hell printf(“Hello!\n”); Streams: Output – Example (cont)

!\n Hello printf(“Hello!\n”); Streams: Output – Example (cont)

\n Hello! printf(“Hello!\n”); Streams: Output – Example (cont)

Hello! _ printf(“Hello!\n”); Streams: Output – Example (cont)

Streams From the program's point of view, the characters are queued in a pipe From the program's point of view, the characters are queued in a pipe The sequence of characters is organized into lines The sequence of characters is organized into lines Each line: Each line: –can have zero or more characters –ends with the "newline" character '\n'

"Standard" Streams "Standard" Streams Standard streams : Standard streams : – stdin - standard input usually from keyboard usually from keyboard – stdout - standard output usually to screen usually to screen – stderr - standard error usually to screen usually to screen must have at the top of your program must have at the top of your program #include #include can be redirected can be redirected

stdin: Input Data is read in from stdin (into a variable) using the scanf() function Data is read in from stdin (into a variable) using the scanf() function

Example: ReadData Input name, age, gender, idNumber

#include #include

/*************************************\ /*************************************\ Read in important info about a lecturer Read in important info about a lecturer \**************************************/ \**************************************/

#include #include /*************************************\ /*************************************\ Read in important info about a lecturer Read in important info about a lecturer \**************************************/ \**************************************/ int main() int main() { return 0; }

#include #include /*************************************\ /*************************************\ Read in important info about a lecturer Read in important info about a lecturer \**************************************/ \**************************************/ int main() int main() { char name[100]; char name[100]; float age; float age; char gender; char gender; int idNumber; int idNumber; return 0; }

#include #include /*************************************\ /*************************************\ Read in important info about a lecturer Read in important info about a lecturer \**************************************/ \**************************************/ int main() int main() { char name[100]; char name[100]; float age; float age; char gender; char gender; int idNumber; int idNumber; scanf("%s %f %c %d", name, &age, &gender, &idNumber); scanf("%s %f %c %d", name, &age, &gender, &idNumber); return 0; }

#include #include /*************************************\ /*************************************\ Read in important info about a lecturer Read in important info about a lecturer \**************************************/ \**************************************/ int main() int main() { char name[100]; char name[100]; float age; float age; char gender; char gender; int idNumber; int idNumber; scanf("%s %f %c %d", name, &age, &gender, &idNumber); scanf("%s %f %c %d", name, &age, &gender, &idNumber); return 0; }

#include #include /*************************************\ /*************************************\ Read in important info about a lecturer Read in important info about a lecturer \**************************************/ \**************************************/ int main() int main() { char name[100]; char name[100]; float age; float age; char gender; char gender; int idNumber; int idNumber; scanf("%s %f %c %d", name, &age, &gender, &idNumber); scanf("%s %f %c %d", name, &age, &gender, &idNumber); return 0; } Input: Ali 19.5 M 2085 Input: Ali 19.5 M 2085 Ali 19.5 M 2085

stdout: Output Data (e.g., from a variable) is written out to stdout using the printf() function. Data (e.g., from a variable) is written out to stdout using the printf() function.

Example: Write Data Example: Write Data Set name to “Ali” Set name to “Ali” Set age to 19.5 Set age to 19.5 Set gender to ‘M’ Set gender to ‘M’ Set idNumber to 2085 Set idNumber to 2085 Output name, age, gender, idNumber Output name, age, gender, idNumber

#include #include /*****************************************\ Write out important info about a lecturer Write out important info about a lecturer\*****************************************/ int main() { char *name = ”Ali" ; char *name = ”Ali" ; float age = 19.5; float age = 19.5; char gender = ’M'; char gender = ’M'; int idNumber = 2085 ; int idNumber = 2085 ; printf("%s\n%f\n%c\n%d\n", name, age, gender, idNumber); printf("%s\n%f\n%c\n%d\n", name, age, gender, idNumber); return 0; return 0;} Ali 19.5 M 2085 _

Formatted Input and Output General form: General form: printf( format-control-string, other-arguments ); scanf( format-control-string, other-arguments ); Examples: Examples: printf("%s %f %c %d", name, age, gender, idNumber); scanf("%s %f %c %d", name, &age, &gender, &idNumber);

Describes the format of the data for output Describes the format of the data for output Contains “conversion specifiers or place holders” and “literal characters” Contains “conversion specifiers or place holders” and “literal characters” Example : printf(“%s is %d years old.\n”, name, age); printf -- Format-Control-String

Describes the format of the data for output Describes the format of the data for output Contains “conversion specifiers or place holders” and “literal characters” Contains “conversion specifiers or place holders” and “literal characters” Example: printf(“%s is %d years old.\n”, name, age); conversion specifiers or placeholders printf -- Format-Control-String (cont)

printf(“%s is %d years old.\n”, name, age); literal characters Describes the format of the data for output Describes the format of the data for output Contains “conversion specifiers” and “literal characters” Contains “conversion specifiers” and “literal characters” Example : printf -- Format-Control-String (cont)

For printf : variables containing data for output For printf : variables containing data for output Example: (“ printf(“%s is %d years old.\n”, name, age); printf -- Other-Arguments In case of multiple placeholders, number of place holders must match number of variables In case of multiple placeholders, number of place holders must match number of variables C matches variables with placeholders in left- to-right order C matches variables with placeholders in left- to-right order

Describes the format of the data given as input Describes the format of the data given as input Contains “conversion specifiers or place holders” Contains “conversion specifiers or place holders” scanf -- Format-Control-String Example: scanf("%s %f %c %d", name, &age, &gender,&id); conversion specifiers

For scanf: “pointers” to variables in which the input will be stored For scanf: “pointers” to variables in which the input will be stored scanf -- Other-Arguments scanf("%s %f %c %d", name, &age, &gender,&id); Example:

‘&’ is for scanf only! ‘&’ is for scanf only! scanf("%s %f %c %d", name, &age, &gender,&id); scanf -- Other-Arguments (cont) For scanf : “pointers” to variables in which the input will be stored For scanf : “pointers” to variables in which the input will be stored Example: Variables of type int, float or char need ‘&’ Do NOT use ‘&’ with strings!

Common Conversion Specifiers for Numerical Information decimal integer: %d printf(“What is %d plus %d?\n”, x, y); scanf(“%d”, &sum); decimal integer: %d printf(“What is %d plus %d?\n”, x, y); scanf(“%d”, &sum); float: %f float: %f printf(“%f squared is...? ”, x); scanf(“%f”, &ans); double : double : printf(“%f squared is...? ”, x); scanf(“%lf”, &ans);

Conversion Specifiers for Alphanumeric Information char : %c printf(“What letter follows %c?\n”,ch); scanf(“%c”, &nextchar); char : %c printf(“What letter follows %c?\n”,ch); scanf(“%c”, &nextchar); string : %s printf(“Name: %s\n”, name); scanf(“%s”, name); string : %s printf(“Name: %s\n”, name); scanf(“%s”, name);

i or d : display a signed decimal integer i or d : display a signed decimal integer f: display a floating point value (default precision is 6) f: display a floating point value (default precision is 6) e or E: display a floating point value in exponential notation e or E: display a floating point value in exponential notation g or G: display a floating point value in either f form or e form; whichever is more compact for the given value and precision g or G: display a floating point value in either f form or e form; whichever is more compact for the given value and precision L: placed before any float conversion specifier to indicate that a long double is displayed L: placed before any float conversion specifier to indicate that a long double is displayed printf : Conversion Specifiers

scanf : Conversion Specifiers scanf : Conversion Specifiers d : read an optionally signed decimal integer d : read an optionally signed decimal integer i : read an optionally signed decimal, octal, or hexadecimal integer i : read an optionally signed decimal, octal, or hexadecimal integer i and d : the argument is a “pointer” to an integer int idNumber; scanf("%d", &idNumber);

scanf : Conversion Specifiers (cont) scanf : Conversion Specifiers (cont) h or l : placed before any integer conversion specifiers to indicate that a short or long integer is to be input h or l : placed before any integer conversion specifiers to indicate that a short or long integer is to be input long int idNumber; long int idNumber; scanf("%ld", &idNumber); scanf("%ld", &idNumber); l or L : placed before any float conversion specifiers to indicate that a double or long double is to be input l or L : placed before any float conversion specifiers to indicate that a double or long double is to be input

Skipping Characters in Input Stream Skipping Characters in Input Stream Skipping blank spaces Skipping blank spaces scanf("%d %d %d", &day, &month, &year); scanf("%d %d %d", &day, &month, &year); An alternative An alternative –Enter data as dd-mm-yyyy: –Store each number in date variables scanf("%d-%d-%d", &day, &month, &year); scanf("%d-%d-%d", &day, &month, &year);

Displaying Prompts When input data are needed in an interactive program, it is better to use the printf function to display a prompting message or prompt, that tells the user what data to enter and if necessary, the format of the input When input data are needed in an interactive program, it is better to use the printf function to display a prompting message or prompt, that tells the user what data to enter and if necessary, the format of the input

Displaying Prompts - Examples printf(“Enter the distance in miles> “); printf(“Enter the distance in miles> “); scanf(“%lf”, &miles); printf(“Enter date as dd-mm-yyyy”); printf(“Enter date as dd-mm-yyyy”); scanf("%d-%d-%d", &day, &month, &year); scanf("%d-%d-%d", &day, &month, &year);

Formatting output Using justification for decimal numbers Using justification for decimal numbers –int a=15, b=116; printf(“%d\n”,a);printf(“%d”,b);Output:15116

Formatting output Using justification for decimal numbers Using justification for decimal numbers –int a=15, b=116; printf(“%3d\n”,a);printf(“%3d”,b);Output:

Formatting output Floating point numbers Floating point numbers –double fp = ; printf( "%f %.3f %.2f %e %E\n", fp, fp, fp, fp,fp ); Output: e E+002

Formatting output with \n \n is an escape sequence in C \n is an escape sequence in C The cursor is a moving place market that indicates the next position on the screen where information will be displayed The cursor is a moving place market that indicates the next position on the screen where information will be displayed When executing a printf function, the cursor is advanced to the start of the next line on the screen if \n is encountered in the format string When executing a printf function, the cursor is advanced to the start of the next line on the screen if \n is encountered in the format string

\n Examples printf(“Here is the first line\n“); printf(“Here is the first line\n“); printf(“\nand this is the second.\n“); Here is the first line Here is the first line and this is the second.

\n Examples printf(“This sentence appears \non two lines.“); printf(“This sentence appears \non two lines.“); This sentence appears This sentence appears on two lines.

Formatting output with \t \t is another escape sequence in C for horizontal tab \t is another escape sequence in C for horizontal tab Example: Example: – printf(“age id\n”); printf(“ \n”); printf(“%d %d\n”,age1,id1); printf(“%d %d\n”,age2,id2); –Output: age id

Formatting output with \t Example with (\t and justification): Example with (\t and justification): – printf(“age\tid\n”); printf(“---\t------\n”); printf(“%3d\t%4d\n”,age1,id1); printf(“%3d\t%4d\n”,age2,id2); –Output: ageid

Some Other Escape sequences \a: Alert (bell) \a: Alert (bell) \b: Backspace \b: Backspace \’: Single quotation mark \’: Single quotation mark \”: Double quotation mark \”: Double quotation mark \\: Backslash \\: Backslash

Summary Input from keyboard is via the stdin stream using the scanf() function Input from keyboard is via the stdin stream using the scanf() function Output to the screen is via the stdout stream using the printf() function Output to the screen is via the stdout stream using the printf() function Streams carry characters Streams carry characters –divided into lines with ‘\n’ character To use the C library functions, you must include the stdio.h header file To use the C library functions, you must include the stdio.h header file Input and output can be formatted Input and output can be formatted