Unit 4. Input/Output Functions

Similar documents
11 Console Input/Output

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

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

UNIT-I Input/ Output functions and other library functions

Fundamentals of Programming

Fundamental of Programming (C)

Should you know scanf and printf?

BSM540 Basics of C Language

UNIT 3 OPERATORS. [Marks- 12]

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

Introduction to Computing Lecture 03: Basic input / output operations

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

Input/Output Week 5:Lesson 16.1

Computers Programming Course 5. Iulian Năstac

Advanced C Programming Topics

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

CC112 Structured Programming

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

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

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

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

UNIT IV INTRODUCTION TO C

AWK - PRETTY PRINTING

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

Fundamentals of Programming Session 8

ANSI C Programming Simple Programs

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

THE FUNDAMENTAL DATA TYPES

Unit 4 Preprocessor Directives

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

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

Work relative to other classes

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

Fundamentals of Programming. Lecture 11: C Characters and Strings

Console Input and Output

Data Input and Output 187

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

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

CSC 1107: Structured Programming

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

CSC 1107: Structured Programming

C - Basic Introduction

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

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 OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

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

Number Systems, Scalar Types, and Input and Output

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

Formatted Output Pearson Education, Inc. All rights reserved.

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

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

Fundamentals of Programming Session 4

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Basic Elements of C. Staff Incharge: S.Sasirekha

Basics of Programming

CSC 1107: Structured Programming

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

بسم اهلل الرمحن الرحيم

Multiple Choice Questions ( 1 mark)

Introduction to Programming

Java Basic Datatypees

LESSON 4. The DATA TYPE char

Reserved Words and Identifiers

The Design of C: A Rational Reconstruction (cont.)

Standard File Pointers

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

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

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

C: How to Program. Week /Mar/05

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

Chapter 2 THE STRUCTURE OF C LANGUAGE

Input/Output: Advanced Concepts

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

Course organization. Course introduction ( Week 1)

Lecture 3. More About C

Formatted Input/Output

Chapter 2 - Introduction to C Programming

Computers Programming Course 6. Iulian Năstac

Programming for Engineers Introduction to C

Introduction to the C Programming Language

Data Types and Variables in C language

Fundamental of C programming. - Ompal Singh

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Fundamentals of C. Structure of a C Program

Basic Types and Formatted I/O

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Input / Output Functions

Data Types. Data Types. Integer Types. Signed Integers

Lecture 02 C FUNDAMENTALS

C Language, Token, Keywords, Constant, variable

Chapter 7. Basic Types

FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING UNIT IV INTRODUCTION TO C

Unit 7. Functions. Need of User Defined Functions

Programming in C++ 4. The lexical basis of C++

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Transcription:

Unit 4 Input/Output Functions Introduction to Input/Output Input refers to accepting data while output refers to presenting data. Normally the data is accepted from keyboard and is outputted onto the screen. C language has a series of standard input-output (I/O) functions. Such I/O functions together form a library named stdio.h. Irrespective of the version of C language, user will have access to all such library functions. These library functions are classified into three broad categories. a) Console I/O functions : Functions which accept input from keyboard and produce output on the screen. b) Disk I/O functions : Functions which perform I/O operations on secondary storage devices like floppy disks or hard disks. c) Port I/O functions : Functions which perform I/O operations on various ports like printer port, mouse port, etc. Console I/O Functions Console I/O refers to the operations that occur at the keyboard and the screen of your computer. Console I/O functions can be further classified as: Formatted Console I/O Unformatted Console I/O The basic difference between formatted and unformatted I/O functions is that the formatted I/O functions allow input and output to be formatted as per the requirements of the user.

Console I/O Functions Formatted Functions Unformatted Functions Type Input Output Type Input Output char scanf( ) printf( ) int scanf( ) printf( ) char getch( ) getche( ) getchar( ) putch( ) putchar( ) float scanf( ) printf( ) string scanf( ) printf( ) int - - float - - string gets( ) puts( ) Formatted Console I/O Functions Formatted I/O functions accept or present the data in a particular format. The standard C library consists of two functions that perform output and input, viz., printf( ) and scanf( ). These functions can format the information under the user's directions. Formatted Output It is highly desirable that the outputs are presented in such a way that they are understandable and are in a form easy to use. The printf( ) statement provides certain features through which the screen output is effectively controlled. The general form of printf( ) function is: printf ("Control String", arg1, arg2... ); Control string may contain: Characters that are simply printed as they are. Conversion specifications that begin with a % sign. Escape sequences that begin with \ sign.

The control string indicates how many arguments follow and what their types are. The arguments arg1, arg2... are the variables whose values are formatted and printed according to specifications of the control string. The arguments must match in number, order, and type with the format specifications. main( ) { int arg = 346; float per = 69.2; printf ("Average = % d \n percentage = % f", arg, per); } output: Average = 346 Percentage = 69.2 Conversion Specifications The conversion specifications are used to provide the type and size of the data. Each conversion specification must begin with %. In the above example %d and %f are the conversion characters. The general form of conversion specifier is % fws fx where fws = field width specifier fx = format specifier The field width specifier tells printf( ) how many columns on the screen should be used while printing a value. %7d tells to print the variable as a decimal integer in the field of 7 columns. If we include a minus sign in conversion specification (e.g., % - 7d), this means left justification is desired and the value will be padded with blanks on the right. Given below is a list of conversion characters that can be used with the printf( ) function. Data Type Integer Real character String Conversion Character short signed %d or % i short unsigned % u Long signed % ld Long unsigned % lu unsigned hexadecimal % x unsigned octal % 0 float % f double % lf signed character % c unsigned character % c % s

Escape Sequences The backslash symbol (\) is considered as escape character because it causes an escape from the normal interpretation of a string, so that the next character is recognized as the one having special meaning. Escape sequence Purpose Escape sequence Purpose \n New line \t Tab \b Backspace \r Carriage return \f formfeed \a Alert \' single quote \" Double Quote Output of Integer Numbers \\ Backslash The format specification for printing an integer number is %wd where 'w' specifies minimum width for the output. The number is written right justified in the given field width. printf ("%d", 12345); 12345 printf ("%10d", 12345); 12345 printf ("% 010d", 12345); 0000012345 printf ("% -10d", 12345); 12345 Output of Real Numbers The real number is displayed in decimal notation with format specification % w.pf where'w' is the integer which represents the minimum number of positions that are to be used and 'p' indicates that in the total width, how many numbers will be placed after the decimal point. printf ("%7.4f", 98.7654); 98.7654 printf ("%7.2", 98.7654); 98.77 printf ("%f", 98.7654); 98.7654 Printing of Single Character A single character can be displayed in a desired position using format %wc. By default, the character will be displayed right justified in a field of 'w' columns. To make it left justified, place a minus sign in format specification. Formatted Input Formatted input refers to an input data that has been arranged in a particular format. For the formatted input we use the function scanf( ).

scanf( ) Function scanf( ) function, allows us to read formatted data and automatically convert numeric information into integers and float. The general from of scanf( ) is scanf ("control string", arg1, arg2,..... ); Control string specifies the field format in which data is to be entered and the arguments arg1, arg2 - - - - - specify the ADDRESS OF LOCATION where value is to be stored. Control string and arguments are separated by commas. Given below is a list of format specifier used to read the inputs: Code Meaning Code Meaning % c Read a single character % d Read a decimal integer % ld Read a long integer % i Read a decimal, or hexadecimal or octal integer % e Read a floating point number % f Read a floating point number % h Read a short integer % o Read an octal number % s Read a string % x Read a hexadecimal number % p Read a pointer % n Read an integer value equal to the number of characters read so far. Input of Integer Numbers The format specification for reading an integer number is % wd where (%) sign indicates conversion specification, 'w' is the integer number for field width specification and 'd' indicates that the number is to be read in integer mode. scanf ("% 2d % 5d", & n1, &n2); An input field may be skipped by specifying * in place of field width. scanf ("% 2d % * d % 6d", &n1, &n2);