Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester 1432 -1433 King Saud University College of Applied studies and Community Service Csc 1101 1."— Presentation transcript:

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

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

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

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

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

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 6 Example printf("That equals %fKilometers.\n",kms); function name format string placeholder newline escape sequence print list

7 Printf with \n example 7

8 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.) 9

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

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

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

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 13

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

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 15

16 Value Format Displayed Output 3.14159 %5.2f3.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 16

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

18 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

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

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

21 Simple C Program: Adding Two Integers 21

22 Simple C Program: Adding Two Integers 22

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 23

24 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

25 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


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

Similar presentations


Ads by Google