Console Input and Output

Similar documents
Unit 4. Input/Output Functions

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Stream Model of I/O. Basic I/O in C

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

Standard File Pointers

Fundamentals of Programming

Fundamental of Programming (C)

Lecture 4. Console input/output operations. 1. I/O functions for characters 2. I/O functions for strings 3. I/O operations with data formatting

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Outline. Computer Programming. Preprocessing in C. File inclusion. Preprocessing in C

C programming basics T3-1 -

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

Fundamentals of Programming Session 8

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Standard I/O in C and C++

Should you know scanf and printf?

Programming in C. Session 2. Seema Sirpal Delhi University Computer Centre

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Advanced C Programming Topics

Introduction to Computing Lecture 03: Basic input / output operations

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

Binghamton University. CS-220 Spring Includes & Streams

File I/O. Last updated 10/30/18

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

UNIT-I Input/ Output functions and other library functions

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

CC112 Structured Programming

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

CSC 1107: Structured Programming

Simple Output and Input. see chapter 7

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

CSC 1107: Structured Programming

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Reserved Words and Identifiers

Introduction to Programming

CSE 303 Lecture 8. Intro to C programming

printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); return 0;

File IO and command line input CSE 2451

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Text Output and Input; Redirection

Formatted Input/Output

11 Console Input/Output

Course organization. Course introduction ( Week 1)

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

Computer Programming CS F111

Fundamentals of Programming. Lecture 11: C Characters and Strings

Computers Programming Course 5. Iulian Năstac

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Computer Programming Unit v

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer of type FILE.

Intermediate Programming, Spring 2017*

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

BSM540 Basics of C Language

ENG120. Misc. Topics

Input / Output Functions

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer variable of type FILE.

Arithmetic Operators. Portability: Printing Numbers

Dept. of CSE, IIT KGP

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

BSM540 Basics of C Language

8. Characters, Strings and Files

SWEN-250 Personal SE. Introduction to C

Input/Output: Advanced Concepts

CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge

Introduction to C An overview of the programming language C, syntax, data types and input/output

Continued from previous lecture

UNIT 3 OPERATORS. [Marks- 12]

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Topic 6: A Quick Intro To C

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

C Basics And Concepts Input And Output

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Structured Programming. Dr. Mohamed Khedr Lecture 4

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Computer Programming: Skills & Concepts (CP) Files in C

THE FUNDAMENTAL DATA TYPES

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Work relative to other classes

Introduction to C. Systems Programming Concepts

n Group of statements that are executed repeatedly while some condition remains true

Today s Learning Objectives

As stated earlier, the declaration

This is CS50. Harvard University Fall Quiz 0 Answer Key

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Transcription:

Console Input and Output Hour 5 PObjectives <C I/O < Character I/O: getchar, putchar, and getch < printf format specifiers field width and precision justification and padding < scanf Copyright 1998-2002 Delroy A. Brinkerhoff. All Rights Reserved. Hour 5 C Slide 1 of 12 Introduction to C I/O Console and file I/O PI/O is the first topic where C++ diverges from C <All C I/O is available in C++ (and can be used in the same program) < C++ introduces many intelligent objects to handle I/O PI/O is implemented through library functions (not operators) PStandard I/O library is declared in <stdio.h> PConsole I/O is a special case of file I/O < In Unix, devices (screen, keyboard, etc.) are files < All C programs begin with three files pre-opened standard in (keyboard) standard out (screen) standard error (screen) PConsole I/O functions only perform tty I/O (i.e., no windows) Hour 5 C Slide 2 of 12

Character Input Reading one character at a time Pint getchar(void) < Returns a single character from the keyboard (stdin) < Character not read until the enter key is pressed (implementation defined but almost always based on Unix line-buffered input) <Returns EOF (< 0, usually -1) on error or if control-d (Unix) or control-z (DOS, Borland and Win95) is entered <Should be assigned to an int variable (EOF is often -1) PCommon idiom: int c;.. while ((c = getchar())!= EOF) /* process character c */ Hour 5 C Slide 3 of 12 The getchar Problem Illustrated A potential pitfall #include <stdio.h> int main(void) { int c; while ((c = getchar( ))!= EOF) /* end with a ^D (Unix) or ^Z (DOS) */ { getchar( ); /* discard new-line from enter key */ } switch(c) { case 'a' : puts("a\n"); break; case 'b' : puts("b\n"); break; case 'c' : puts("c\n"); break; case '\n' : puts("new line\n"); break; } return 0; } Hour 5 C Slide 4 of 12

Character Output Writing one character at a time Pint putchar(int c) <Writes a single character to the screen (stdout) <Only the low-order byte is printed <Return value: c on success; EOF on failure < Less overhead than printf <Return EOF on error, c if otherwise PExample: char* message = hello world ; int i; for (i = 0; i < 11; i++) putchar(message[i]); Hour 5 C Slide 5 of 12 Single-Character Input Functions Non-standard (i.e., not ANSI) functions PReads the character but does not echo the character < int getch(void); Borland / Unix < int _getch(void); Microsoft PReads the character and echos it to the screen < int getche(void); Borland / Unix < int _getche(void); Microsoft PDoes not require a new line (pressing the enter key) PPrototype in <conio.h> (Borland and Microsoft) PPart of the Unix curses library (<curses.h> and -lcurses) PSee getch.c for a simple Unix implementation Hour 5 C Slide 6 of 12

printf Formatted output PPrototype in <stdio.h> Pint printf("controlstring", arg1, arg2, arg3...); < The controlstring is required; other arguments are optional < The return value (the number of characters printed or a negative value on error) is almost never checked PExcess arguments (no corresponding format specifier) are ignored; format specifiers without corresponding arguments produce undefined results PArguments may be any legal expression printf( The answer is: %d and %d\n, ans1, ans2); Hour 5 C Slide 7 of 12 The Control String Output specification PContains < Characters that are printed without modification < Format specifiers: minimally a percent sign (%) & a conversion specifier (see page 79 or a list of conversion specifiers) PFormat specifiers <% position flag fieldwidth precision length conversion <Position integer $ position in the argument list of the next arguments to be converted < Flag (dash, hyphen, or minus sign) left justify output (used w/ field width) 0 (zero) pad with 0 s (default is pad with blanks) + precede numbers with + or - (blank or space) precede numbers with space or - Hour 5 C Slide 8 of 12

The Control String Continued PfieldWidth < integer > 0 minimum field width, excess space filled w/ pad char < * takes the next argument for width specifier < printf("%4d", counter); Pprecision <. integer number of digits following the decimal point <.* takes the next argument for precision specifier < printf("%6.2f", pay); /* 3 digits before the decimal, two after */ Plength < l (lower case L) used with d, i, o, u, x, and X; specifies a long value (effect is implementation dependent) < printf("%lf", doublevalue); Hour 5 C Slide 9 of 12 Simple printf Examples Common usage printf( %d %7d %10d, 10, counter, 10+counter); printf( i*pi = %f sin(i*pi) = %15.10f\n, i*pi, sin(i*pi)); printf( A = %d in ASCII\n, A ); printf( Can t open file \ %s\ \n, filename); printf( Test Score: %f6.2%%\n, tscore); printf( The Earth weighs %e tons, 6.65e24); Hour 5 C Slide 10 of 12

Data Input with scanf Standard library input function PUsed to read data from the keyboard P#include <stdio.h> (prototype, constants, etc.) PExcess arguments (no corresponding control character) are ignored. Control characters without corresponding arguments produce undefined results PArguments must be pointers (i.e., addresses); these are usually found with the address of operator (&) see Hour 11 scanf( %d, &ans1); Hour 5 C Slide 11 of 12 scanf Examples Basic Input with scanf int float double char size; balance; debt; answer; scanf( %d, &size); scanf( %f, &balance); scanf( %lf, &debt);/* must use l for double */ scanf( %c, &answer); Hour 5 C Slide 12 of 12