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