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

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

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

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

1

CHAPTER 2 PART #3INPUT - OUTPUT

1 st semester 1432 -1433

King Saud University College of Applied studies and Community ServiceCsc 1101

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

2

Outlines

Using the I/O operation Printf function Scanf function

Common Programming Errors

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

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)

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

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

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

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.

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

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

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

7

Printf with \n example

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

 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

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

9

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

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

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

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

11

Placeholder / Conversion Specifier

printf scanf

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

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

21

Simple C Program: Adding Two Integers

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

22

Simple C Program: Adding Two Integers

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

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

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

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

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

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