CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester 1432 -1433 King Saud University College of Applied studies and Community Service Csc 1101 1.

Slides:



Advertisements
Similar presentations
Fundamental of C Programming Language and Basic Input / Output Function Lecture 2.
Advertisements

Principles of Programming Chapter 3 Fundamental of C Programming Language and Basic Input/Output Function 1 NI S1 2009/10.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
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.
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
Principles of Programming - NI Chapter 3 Fundamental of C Programming Language and Basic Input/Output Function.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
CSEB114: Principle of Programming
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
1 Software John Sum Institute of Technology Management National Chung Hsing University.
Chapter 2 Getting Started in C Programming
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
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.
Yu Yuanming CSCI2100B Data Structures Tutorial 3
Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. To perform these tasks user friendly.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CSEB114: Principle of Programming Chapter 3: Fundamental of C Programming Language and Basic Input / Output Function.
1 Program Planning and Design Important stages before actual program is written.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Structured Programming (4 Credits) HNDIT Week 2 – Learning Outcomes Design an algorithmic solution for simple problem such as computation of a factorial,
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Lecture2.
C Formatted Input/Output
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
CSC201: Computer Programming
Chapter 2 - Introduction to C Programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Chapter 2 part #3 C++ 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 input/output.
CSI 121 Structured Programming Language Lecture 7: Input/Output
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lecture3.
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 part #3 C++ Input / Output
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc

Outlines  Using the I/O operation  Printf function  Scanf function  Common Programming Errors 2

Input/Output Operations  Input operation  an instruction that copies data from an input device into memory  Output operation  an instruction that displays information stored in memory to the output devices (such as the monitor screen) 3

Input/Output Functions  A C function that performs an input or output operation  A few functions that are pre-defined in the header file stdio.h such as :  printf()  scanf()  getchar() & putchar() 4

The printf function  Used to send data to the standard output (usually the monitor) to be printed according to specific format.  General format:  printf(“string literal”); A sequence of any number of characters surrounded by double quotation marks.  printf(“format string”, variables); Format string is a combination of text, conversion specifier and escape sequence. 5

The printf function  Example:  printf(“Thank you”);  printf (“Total sum is: %d\n”, sum); %d is a placeholder (conversion specifier) marks the display position for a type integer variable \n is an escape sequence moves the cursor to the new line 6 Example printf("That equals %fKilometers.\n",kms); function name format string placeholder newline escape sequence print list

Printf with \n example 7

A Simple C Program: Printing a Line of Text (Cont.)  One printf can print several lines by using additional newline characters as in Fig  Each time the \n (newline) escape sequence is encountered, output continues at the beginning of the next line. 8

A Simple C Program: Printing a Line of Text (Cont.) 9

Escape Sequence Effect \aBeep sound \bBackspace \fFormfeed (for printing) \nNew line \rCarriage return \tTab \vVertical tab \\Backslash \”“ sign \oOctal decimal \xHexadecimal \ONULL 10

Placeholder / Conversion Specifier printfscanf int%d float%f double%f%lf char%c string%s 11

Examples printf("Please enter the student's grades:"); Please enter the student's grades: printf("The class average is %f ",average); The class average is printf("The total area is %f and the total cost is %d S.R.",tarea,tcost); The total area is 60.2 and the total cost is 4530 S.R. printf("The student received an %c grade in the course.",grade); The student received an A grade in the course. 12

Examples (Continue) printf("The grade is %c%c", grade, gradesymb); The grade is A+ printf("I am the first line\n"); printf("\n I am the second line\n"); I am the first line I am the second line 13

Value Format Displayed Output 234 %4d %5d %1d %4d %2d %5d-234 Formatting Numbers in Program Output 14

Value Format Displayed Output %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f Formatting Numbers in Program Output 15

Value Format Displayed Output %5.2f %3.2f %4.2f %.3f %.4f %4.2f Formatting Numbers in Program Output 16

The scanf function  Read data from the standard input device (usually keyboard) and store it in a variable.  General format:  scanf(“Format string”, &variable); 17

The scanf function  Example : int age; printf(“Enter your age: “); scanf(“%d”, &age);  Common Conversion Identifier used in printf and scanf functions. printfscanf int%d float%f double%f%lf char%c string%s 18

The scanf function  If you want the user to enter more than one value, you serialise the inputs.  Example: float height, weight; printf(“Please enter your height and weight:”); scanf(“%f%f”, &height, &weight); 19

Examples scanf("%i",&workhours); scanf("%c",&letter); scanf("%i",&student_ID); scanf("%f",&tot_score); scanf("%f",&temperature); scanf("%f",&working_hours); scanf("%i%c",&population,&firstinitial); 20

Simple C Program: Adding Two Integers 21

Simple C Program: Adding Two Integers 22

Common Programming Errors  Debugging  Process removing errors from a program  Three (3) kinds of errors :  Syntax Error a violation of the C grammar rules, detected during program translation (compilation). statement cannot be translated and program cannot be executed 23

Common Programming Errors cont…  Run-time errors An attempt to perform an invalid operation, detected during program execution. Occurs when the program directs the computer to perform an illegal operation, such as dividing a number by zero. The computer will stop executing the program, and displays a diagnostic message indicates the line where the error was detected 24

Common Programming Errors cont…  Logic Error/Design Error An error caused by following an incorrect algorithm Very difficult to detect - it does not cause run-time error and does not display message errors. The only sign of logic error – incorrect program output Can be detected by testing the program thoroughly, comparing its output to calculated results To prevent – carefully desk checking the algorithm and written program before you actually type it 25