Introduction to Computing Lecture 03: Basic input / output operations

Similar documents
Should you know scanf and printf?

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

Unit 4. Input/Output Functions

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

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

Fundamentals of Programming

Fundamental of Programming (C)

CC112 Structured Programming

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

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

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

Fundamentals of Programming Session 4

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

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

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

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

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

Programming for Engineers Introduction to C

Chapter 2, Part I Introduction to C Programming

Input/Output Week 5:Lesson 16.1

Formatted Input/Output

C: How to Program. Week /Mar/05

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

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

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

Basic Types and Formatted I/O

Chapter 2 - Introduction to C Programming

Fundamentals of Programming. Lecture 11: C Characters and Strings

Fundamental of Programming (C)

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

BSM540 Basics of C Language

Formatted Output Pearson Education, Inc. All rights reserved.

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Advanced C Programming Topics

Computer Programming Lecture 12 Pointers

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

EC 413 Computer Organization

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) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

AWK - PRETTY PRINTING

C Programming

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

BSM540 Basics of C Language

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

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

11 Console Input/Output

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

Chapter 2: Overview of C. Problem Solving & Program Design in C

The C language. Introductory course #1

Standard I/O in C and C++

211: Computer Architecture Summer 2016

UNIT-I Input/ Output functions and other library functions

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

Work relative to other classes

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

Reserved Words and Identifiers

Fundamentals of C. Structure of a C Program

BİL200 TUTORIAL-EXERCISES Objective:

EE458 - Embedded Systems Lecture 4 Embedded Devel.

Input / Output Functions

Number Systems, Scalar Types, and Input and Output

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

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

Chapter 2 THE STRUCTURE OF C LANGUAGE

ANSI C Programming Simple Programs

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

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

LESSON 4. The DATA TYPE char

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

Standard C Library Functions

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

Chapter 9 Strings. With this array declaration: char s[10];

Differentiate Between Keywords and Identifiers

Lecture 3. More About C

Introduction to the C Programming Language

Full file at C How to Program, 6/e Multiple Choice Test Bank

4. Inputting data or messages to a function is called passing data to the function.

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Binghamton University. CS-220 Spring Includes & Streams

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

A Fast Review of C Essentials Part I

CSC 1107: Structured Programming

UNIT IV-2. The I/O library functions can be classified into two broad categories:

C - Basic Introduction

Operators and Control Flow. CS449 Fall 2017

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

CSE 12 Spring 2016 Week One, Lecture Two

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Standard File Pointers

Worksheet 4 Basic Input functions and Mathematical Operators

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Introduction to Programming

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

3. Types of Algorithmic and Program Instructions

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

Transcription:

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

Topics Streams printf function scanf function

Data storage 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 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 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 You can gain access to most of the operations through the preprocessor directive #include <stdio.h>

Input/output functions 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

Streams 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

Streams: Input -- Example int item; float cost; scanf( %d %f, &item, &cost); 135 25.5 _ 1 3 5 2 5. 5 \n input buffer

Streams: Input -- Example (cont) int item; float cost; scanf( %d %f, &item, &cost); 135 25.5 _ 1 3 5 2 5. 5 \n item cost

Streams: Input Example (cont) int item; float cost; scanf( %d %f, &item, &cost); 135 25.5 _ 2 5. 5 \n item 135 cost

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

Streams: Output -- Example printf( Hello!\n ); H e l l o! \n output buffer

Streams: Output Example (cont) printf( Hello!\n ); H e l l o! \n

Streams: Output Example (cont) printf( Hello!\n ); e l l o! \n H

Streams: Output Example (cont) printf( Hello!\n ); l l o! \n He

Streams: Output Example (cont) printf( Hello!\n ); l o! \n Hel

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

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

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

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

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

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

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

Example: ReadData Input name, age, gender, idnumber

#include <stdio.h>

#include <stdio.h> /*************************************\ Read in important info about a lecturer \**************************************/

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

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

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

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

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

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

Example: Write Data Set name to Ali Set age to 19.5 Set gender to M Set idnumber to 2085 Output name, age, gender, idnumber

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

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

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

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

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

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

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

Common Conversion Specifiers for Numerical Information decimal integer: %d printf( What is %d plus %d?\n, x, y); scanf( %d, &sum); float: %f printf( %f squared is...?, x); scanf( %f, &ans); 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); string: %s printf( Name: %s\n, name); scanf( %s, name);

printf: Conversion Specifiers i or d: display a signed decimal integer f: display a floating point value (default precision is 6) 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 L: placed before any float conversion specifier to indicate that a long double is displayed

scanf: Conversion Specifiers d: read an optionally signed decimal 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) h or l: placed before any integer conversion specifiers to indicate that a short or long integer is to be input long int 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

Skipping Characters in Input Stream Skipping blank spaces scanf("%d %d %d", &day, &month, &year); An alternative Enter data as dd-mm-yyyy: 16-3-1999 Store each number in date variables 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

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

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

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

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

Formatting output with \n \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 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( \nand this is the second.\n ); Here is the first line and this is the second.

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

Formatting output with \t \t is another escape sequence in C for horizontal tab Example: printf( age id\n ); printf( --- ------\n ); printf( %d %d\n,age1,id1); printf( %d %d\n,age2,id2); Output: age id --- ------ 12 1222 3 1423

Formatting output with \t 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: age id --- ------ 12 1222 3 1423

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

Summary 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 Streams carry characters divided into lines with \n character To use the C library functions, you must include the stdio.h header file Input and output can be formatted