Input/Output Week 5:Lesson 16.1

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

C: How to Program. Week /Mar/05

Unit 4. Input/Output Functions

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

Chapter 2 - Introduction to C Programming

Fundamentals of Programming Session 4

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

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

Programming for Engineers Introduction to C

CC112 Structured Programming

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

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

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

Introduction to Computing Lecture 03: Basic input / output operations

Chapter 2, Part I Introduction to C Programming

Introduction to Programming

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

LESSON 4. The DATA TYPE char

Chapter 1 & 2 Introduction to C Language

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

BSM540 Basics of C Language

Fundamentals of C. Structure of a C Program

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.

Chapter 2 THE STRUCTURE OF C LANGUAGE

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

Should you know scanf and printf?

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

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

Fundamentals of Programming. Lecture 11: C Characters and Strings

Fundamentals of Programming

Fundamental of Programming (C)

Data Types. Data Types. Integer Types. Signed Integers

Formatted Input/Output

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

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

THE FUNDAMENTAL DATA TYPES

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

BSM540 Basics of C Language

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

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

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Basic Types and Formatted I/O

1.1 Introduction to C Language. Department of CSE

11 Console Input/Output

Introduction to the C Programming Language

Advanced C Programming Topics

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

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

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

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

ANSI C Programming Simple Programs

Chapter 1 Introduction to Computers and C++ Programming

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

printf( Please enter another number: ); scanf( %d, &num2);

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

CS1100 Introduction to Programming

Basic Elements of C. Staff Incharge: S.Sasirekha

Computers Programming Course 5. Iulian Năstac

C - Basic Introduction

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

C Language, Token, Keywords, Constant, variable

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

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

C-LANGUAGE CURRICULAM

A First Program - Greeting.cpp

Fundamentals of Programming Session 8

Chapter 8 - Characters and Strings

Data Types and Variables in C language

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Standard File Pointers

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

2 nd Week Lecture Notes

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Introduction to C# Applications

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Lecture 3. More About C

Preview from Notesale.co.uk Page 6 of 52

Multiple Choice Questions ( 1 mark)

CSC 1107: Structured Programming

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

Introduction to C Language

Computer Programming 3 th Week Variables, constant, and expressions

PROGRAMMAZIONE I A.A. 2018/2019

Lesson 7. Reading and Writing a.k.a. Input and Output

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

CS102: Variables and Expressions

ME 172. Sourav Saha. Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

UNIT 3 OPERATORS. [Marks- 12]

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

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

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

6.096 Introduction to C++ January (IAP) 2009

COMP1917: 09 Arrays and Strings

Transcription:

Input/Output Week 5:Lesson 16.1 Commands (On-Line) scanf/printf Principles of Programming-I / 131101 Prepared by: Dr. Bahjat Qazzaz -------------------------------------------------------------------------------------------- Introduction We have already practiced entering data from the keyboard and displaying results on the screen. The data can be introduced to the program, mainly, by three ways: 1. By initializing the variables during program typing. This is usually done to give an initial value or to assign a constant value to the variable. Consequently, when it is executed, the program gives the same results. For example, int sum=0; float PI = 3.14. 2. The data can be passed to the program from the keyboard using one of the functions dedicated to this purpose. The data, then, will be processed and displayed on the screen. This chapter will focus on this type of functions. 3. The data can be passed to the program using files or any of the computer ports. This type of data input will be introduced later in different chapter. Data Input and Output In this chapter, we will focus on six functions which are part of C library functions and used for data input and output. These functions include: printf, scanf, getchar, putchar, gets, and puts. These functions allow the data transfer between the standard input and output devices such as keyboard and screen respectively. The scanf Function This function is used to enter different data types including any combination of numerical values, single characters, and strings. Numerical values can be integer, float, short integer, long integer, unsigned integer, etc. The string includes any combination of

characters. Although we will give examples on how to read and write strings, the strings will be explained with details in a later chapter when we talk about arrays. The letter f in the scanf means formatted scanf. And the format of the scanf is as follows: scanf( control string, argument1, argument2,, argumentn); The control string consists of the character % followed by a character which indicates the type of the corresponding argument. For example, %d is used for integer, %f is used for float, %c is used for character, %s is used for string, etc. The argument is an identifier (e.g. variable and arrays) with a specific data type. The control string must match the data type of the argument. int age; float salary; char option; char name[20]; // this is a string or an array of characters scanf( %d %f %c %s, &age, &salary, &option, name); The previous statement prompts the user to enter four kinds of data: integer, float, character, and string. The control string can be written with and without white space. (e.g. %d%f%c%s ). Each variable (or argument) must be preceded by an ampersand character &. In fact, this variable is called an address (or pointer) that indicates where the data will be stored in the computer s memory. Notice that the variable name is not preceded by the & since it is defined as an array. Array identifiers should not begin with the ampersand. The printf function The printf function is used for outputting data on the screen. We have already seen example that used the printf function for displaying different data types on the screen. The printf function can be of different formats as follow: printf( string ); printf( Welcome to An-najah University ); printf( control string, argumnet1, argument2,, argumentn);

printf( %s %d %f, name, age, salary); Notice that, the variables in the printf function are not preceded by an &. printf( combinations of string and control string, argument1, argument2, argumnetn ); printf( My name is %s, my age is %d and my salary is %f, name, age, salary); This example is similar to example 6 of the previous chapter. However, the two numbers are entered from the keyboard by the user. The results of the execution vary each time the user enters different values. int main() int value1; /* first number to be input by the user */ int value2; /* second number to be input by the user */ int sum; /* variable in which sum will be stored */ //value1 = 20; printf( Please enter value1: ); scanf( %d, &value1); //value2 = 30; printf( Please enter value2: ); scanf( %d, &value2); sum = value1 + value2; printf( Value1 = %d\n, value1); printf( Value2 = %d\n, value2); printf( The sum of %d and %d is %d\nor\n,value1, value2, sum); printf( %d + %d = %d\n, value1, value2, sum); return 0;

Q4) Write a C program to calculate the total price of buying orange, apple, and tomato. The user must input the number of kilos and the price of each item, then the program calculates and displays on the screen the total price. Escape Sequences Certain characters (e.g. the double quote, the backslash \, the apostrophe, and the question mark?) and nonprinting characters (e.g. line feed or newline, bell, null) can t be displayed on the screen or used with printf function directly. Such characters must be preceded by a backslash. For example, to display a backslash on the screen you must type printf( \\ ); The same is applied to the rest of the characters (e.g. \n for new line, \ for double quote, \? for the question mark, \t for horizontal tab, \a for bell (alert), and \0 for null). The null is a special character added to mark the end of a string. printf( Ahmad\ s House \ AH\ \a\a\a is big\n ); The following program is to define variables and initialize them to calculate the area of a room given the length and width. int length; int width; length = 10; width = 20; area = length * width; Since the values of the length and width are known (constant), the program can be written in an easier way as follows:

area = 20 * 10; The program can be written using a single statement as follows: printf( Area = %d\n, 20 * 10); Keyboard and scanf statement The above three solutions share the same problem which is that, when you run these programs they always give the same result (Area = 200). This is true since the value of the length and width are assigned constant values which are 20 and 10 respectively. To overcome this problem, we will change the program so that the program prompts the user to input (enter) the values of the length and width during the run time. Thus, each time the user runs the program, he will be prompted to enter new values. The following program code shows how the scanf works: int length; int width; scanf( %d, &length); /* The user inputs the length value from the keyboard */ scanf( %d, &width20); /* The user inputs the width value from the keyboard */ area = length * width;

Interactive programming Interactive programming allows interfacing between the computer and user. That is, the computer can ask the user to do some actions and directs him to do what he is supposed to do. To understand what do we mean by that, we have added printf statement before each of the scanf statements. Compile and run the following program to see the difference: int main( ) /* Calculate the area of a room if the length is 20 and */ /* the width is 10 */ int length; int width; printf( Please enter the length of the room: ); scanf( %d, &length); printf( Please enter the widh of the room: ); scanf( %d, &width20); area = length * width; return 0;