Docsity

Computer Programming Managing Input and Output Operation, Exams of Computer Engineering and Programming

Partial preview of the text

Download Computer Programming Managing Input and Output Operation and more Exams Computer Engineering and Programming in PDF only on Docsity!

CMT 110: COMPUTER PROGRAMMING METHODOLOGY CHAPTER SIX C Programming:- Managing Input and Output Operation Chapter Objectives By the end of this chapter the learner should be able to; « Use the C program input and Output functions appropriately v Link the appropriate header file Use the scanf function to format inputs Use the printf function to format output Be able to detect errors in in puts ad v i ¥ Enhance the readability of the Out put 6.1. Introduction Reading, processing and writing of data are the three essential functions of a computer program. « Most programs take data as input and display the processed data, often known as information or output on a suitable medium. The methods of providing data to a programs variable are; « Assignment statements example x = 60; Input function scanf () which reads data from a keyboard. (Scanf stands for scan formatted) Unlike other high-level languages, C does not have any built-in input/output statements as part of the syntax. All input/output operations are carried out through function calls such as printf and scanf. These standard input/out put functions are contained in the library file. To able to call these standard input/output functions the library/header file (stdio.h = standard input-output header file). CMT 110: Computer Programming Methodology notes for Regular and ODEL Students CMT 110: COMPUTER PROGRAMMING METHODOLOGY 6.2. Reading a Character. The simplest of all input/output operations is reading a character from the “standard input" unit (Usually keyboard) and writing it to the ,standard output" unit (usually the screen). In C program, reading a character can be done by using the getchar() function. The getchar function takes the following format; variable_name = getchar(); variable_name is a valid C name that has been defined as char type. When the statement is reads, the program waits until a key is pressed and then assign the character as a value to the getchar() function. Example; char name; name = getchar(): Example 6.1. Program Example: use of getchar() function to read a character /* program example on use of getchar function */ #include #include main() { char answer; printf(,Would you like to know my name?\n); printf(“Type Y or YES and N or NO:”); answer = getchar(); if (answer == ,,Y“ || answer == ,y") printf(“An\n My name if BUSY BEE\n”); else printf(“\n\n You are Good for nothing\n”); getchar(); } CMT 110: Computer Programming Methodology notes for Regular and ODEL Students CMT 110: COMPUTER PROGRAMMING METHODOLOGY printf(“The character is not alphanumeric’): getch(); } 6.3. Writing a character. Like getchar there is an analogous function putchar for writing characters one at a time to the terminal. It takes the form; putchar (variable_name); the variable name is a type char variable containing a character. Example 6.3 Program to show use of putchar function and to convert case of a character #include #include #include>ctype.h> main() { char alphabet; printf (“Enter any alphabet\n’); putchar (‘\n’); /* move to the next line*/ alphabet = getchar(); if (islower(alphabet)) putchar (toupper (alphabet)); /* Reverse the case and display */ else putchar(tolower(alphabet)); getch(), } CMT 110: Computer Programming Methodology notes for Regular and ODEL Students CMT 110: COMPUTER PROGRAMMING METHODOLOGY 6.4. Formatted Input Formatted input refers to an input data that has been arranged in a particular format. C program uses the scanf function to format in the inputs. The scanf function takes the following format; scanf(“control string”, arg1, arg2, ....); The control string specifies the field format in which the data is to be entered and the arguments arg1, arg2 specify the address of locations where the data is stored. Control string and the arguments are separated by commas. Control string (also called format string) contains field specifications which direct the interpretation of input data and includes; « Field (or format) specification, consisting of the conversion character %, a data type character (or type specifier), and an optional number, specifying the field width e Blank, tabs and newlines Blanks, tabs and newlines are ignored. The data type character indicates the type of data that is to be assigned to the variable associated with the corresponding argument. The field width specifier is optional. 6.4.1. Inputting Integer Numbers The field specification for reading an integer number is % w sd % - specifies a conversion specification follows. w is an integer number that specified the field width of the number to be read and d is the data type character and indicates the number to be read is a integer. Consider the following example; scanf(“%2d %5d”, &num1, &num2); If number entered are 50, 31426, 50 will be assigned to num1 and 31426 will be assigned to num2. The output will be 50, 31425. If number entered are 31426, 50, the num/1 variable will be assigned 31 (because of the %2d) and numz2 variable will be assigned the remaining part - 426 (unread part of 31426). The output will thus be 31 426 Input data must be separated by spaces, tabs or new lines, punctuation marks do not count as separators. CMT 110: Computer Programming Methodology notes for Regular and ODEL Students CMT 110: COMPUTER PROGRAMMING METHODOLOGY Example scanf (%f %f %f”, &x, &y, &z); with the input 475.90 43.25 689.0, 475.90 will be assigned to x, 43.25 will assigned to y and 689.0 will assigned to z. Program example 6.5. Reading float numbers using scanf function #include #include #include>ctype.h> main() { float a, b, c, x, y printf(“Enter Values for A and B\n’); scanf("%f %f”, &a, &b); printf(“x = %f\n y = %f\n\n”, x, y printf(“Enter values for X and Y \n”); scanf(“%1f %1f, &x, &y); printf(‘An\n x = %.12fin y = %12e”. x y); getch(), i Output Enter values or A and B 199.75 12.567 a= 199.95 b= 12.567 Enter values for X and Y 4.123458967 18.56768974363 x = 4.123458967 y = 18.56768974363c001 6.5. Inputting Character Strings The scanf function format for inputting character strings are; CMT 110: Computer Programming Methodology notes for Regular and ODEL Students CMT 110: COMPUTER PROGRAMMING METHODOLOGY scanf( %ws) or scanf(%wc); When we use %wc for reading a string the program waits until the wth character is keyed in. %s terminates reading at the encounter of a blank space. 6.6. Reading mixed Data types C allows inputting of strings of mixed data types as long as the data items match the control specifications in order and type. When an attempt is made to read an item that does not match the type expected, the scanf function does not read any further and immediately returns the values read. Example; scanf(“%d %c %f %s”, &count, &code, &ratio, name); will read the data 15 p 1.575 coffee correctly and assign the values to the variables in the order in which they appear. 6.7. Detecting Errors in Input When a scanf function completes reading its list, it returns the value of number of items that are successfully read. When scanf() encounters an error example in data type, it returns the number of the last correctly read value. Example scanf(“%d %f %s”, &a, &b, name); will return 1 when 20, motor, 15.25 data is input. Program Example 6.6. Program to test correctness of the data input #include #include #include>ctype.h> main() f inta float f; char c; printf(“Enter values of a, b, c\n”); if scanf(“%d %f %c”, &a, &b, &c) ==3) printf(“a = %d, b = %f c = %c\n”, a, b, c); CMT 110: Computer Programming Methodology notes for Regular and ODEL Students CMT 110: COMPUTER PROGRAMMING METHODOLOGY e Never end the format string with whitespaces. It is a fatal error. « The scanf reads until « Awhite space character is found in a numeric specification or « The maximum number of characters have been read or « The end of file is reached. 6.9. Formatted Out put The printf function is used to format the C program out puts. The printf statement provides certain features that can be effectively exploited to control the alignment and spacing of print-outs on the terminal. The printf function format is printf(“ control string”, arg1, arg2,arg3, .....); The control string consists of; « Characters that will be printed on the screen as they appear « Format specification that defines the output format for display of ach item. e Escape sequence characters such as \n (for new line), \t (for tab) and \b. The control string indicates how many arguments follow and what their types are. The arguments arg1, arg2, are the variables whose values are formatted and printed according to the specifications of the control string. The argument should match in number, order and type with the format specifications. A simple format specification has the following form; Format ( %w.p type-specifier) w = integer number that specifies the total number of columns for the output value, p is another integer number that specifies the number of digits to the right of the decimal point or the number of characters to be printed from a string. Both w and p are optional. Printf function never supplies a newline automatically and therefore multiple printf statements may be used to build one line of output. 6.9.1. Formating Integer and Real numbers output The format specification for printing an integer number is;- %wd w specifies the minimum field width for the output, however if the number is greater than the specified field width, it will print in full overriding the minimum specification. The format specification for printing a real number is; % w.p f CMT 110: Computer Programming Methodology notes for Regular and ODEL Students CMT 110: COMPUTER PROGRAMMING METHODOLOGY w specifies the minimum field width for the output, p an integer indicates the number of digits to be displayed after the decimal point (precision). The value when printed out is rounded to p decimal places and printed right-justified. Examples of integer and real numbers _ formatting Format Out put printf(“%6d", 9867); __ 9876 prinff{“%-6d" 9867 ); 9876__ printt(““%c06d”, 9876); 0098676 printi(“%7.4f", 98.7654) | 98.7654 printf(“%7.2f", 98.7654) 98.77 printf(“%-7.2f", 98.7654) | 98.77 printi(“%f”, 98.7654) 98.7654 Program Example 6.7. Formatted output for Real numbers #include #include #include>ctype.h> main() { float y = 98.7654; printf(“%7.4f\n” , y); printf(“%f \n”, y); printf(“%-7.2f\n” , y); printf(“%-O7f \n”, y); getch(); } CMT 110: Computer Programming Methodology notes for Regular and ODEL Students