CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester 1432 -1433 King Saud University College of Applied...

Post on 02-Jan-2016

224 views 2 download

Tags:

Transcript of CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester 1432 -1433 King Saud University College of Applied...

1

CHAPTER 2 PART #3INPUT - OUTPUT

1 st semester 1432 -1433

King Saud University College of Applied studies and Community ServiceCsc 1101

2

Outlines

Using the I/O operation Printf function Scanf function

Common Programming Errors

3

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)

4

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()

5

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.

6

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

Example

printf("That equals %fKilometers.\n",kms);

functionname

formatstring

placeholder newlineescape sequence

printlist

7

Printf with \n example

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

One printf can print several lines by using additional newline characters as in Fig. 2.4.

Each time the \n (newline) escape sequence is encountered, output continues at the beginning of the next line.

8

9

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

10

Escape SequenceEscape

SequenceEffect

\a Beep sound

\b Backspace

\f Formfeed (for printing)

\n New line

\r Carriage return

\t Tab

\v Vertical tab

\\ Backslash

\” “ sign

\o Octal decimal

\x Hexadecimal

\O NULL

11

Placeholder / Conversion Specifier

printf scanf

int %d %dfloat %f %fdouble %f %lfchar %c %cstring %s %s

12

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 95.5.

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.

13

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

14

Value Format Displayed Output

234 %4d 234

234 %5d 234

234 %1d 234

-234 %4d -234

-234 %2d -234

-234 %5d -234

Formatting Numbers in Program Output

15

Value Format Displayed Output

-99.42 %6.2f -99.42

.123 %6.2f 0.12

-9.536 %6.2f -9.54

-25.554 %6.2f -25.55

999.4 %6.2f 999.40

99.999 %6.2f 100.00

Formatting Numbers in Program Output

16

Value Format Displayed Output

3.14159 %5.2f 3.14

3.14159 %3.2f 3.14

.1234 %4.2f 0.12

-.006 %.3f -0.006

-3.14159 %.4f -3.1416

-.006 %4.2f -0.01

Formatting Numbers in Program Output

17

The scanf function

Read data from the standard input device (usually keyboard) and store it in a variable.

General format: scanf(“Format string”, &variable);

18

The scanf function

Example :int age;printf(“Enter your age: “);scanf(“%d”, &age);

Common Conversion Identifier used in printf and scanf functions.

printf scanf

int %d %dfloat %f %fdouble %f %lfchar %c %cstring %s %s

19

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);

20

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);

21

Simple C Program: Adding Two Integers

22

Simple C Program: Adding Two Integers

23

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

24

Common Programming Errors cont…

Run-time errorsAn 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

25

Common Programming Errors cont…

Logic Error/Design ErrorAn error caused by following an

incorrect algorithmVery 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