Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.

Similar presentations


Presentation on theme: "Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs."— Presentation transcript:

1 Chapter-4 Managing input and Output operation

2  Reading, processing and writing of data are three essential functions of a computer program.  Most programs take some data as input and display the processed data or result as output.  So, we need some methods that can read input and write output on a suitable input/output medium.

3  Bcoz unlike other high-level languages, C does not have any built- in input/output statements as part of its syntax.

4 Input/output functions  Thus far we have seen two methods for providing data to the program variables. 1.One is to assign values to variables through the assignment statements such as x = 5; and so on. 2. And another method is to use input function scanf which can read data from a terminal. And for outputting the results we have used the function printf which sends the results out to a terminal.

5  There are also some functions other than printf and scanf which can be used as input/output functions.  The simplest of all input/output operations is reading a character from the standard input and writing it to the standard output unit. Reading a character

6  Reading a single character can also be done by using the function getchar(). The getchar() function The getchar() function is used to read a single character from the standard input unit. It takes the following form: variable_name = getchar();

7  Variable_name is a valid C name that has been declared as char type.  When this statement is encountered, the computer waits until a key is pressed and then assigns this character as a value to the getchar function.

8 Example Since getchar is used on the right-hand side of an assignment statement, the character value of getchar is assigned to the variable name on the left side. For example: char name; name = getchar(); Will assign the character ‘A’ to the variable name when we press the key A on the keyboard.

9  Here, We Will assign the character ‘ A ’ to the variable name when we press the key A on the keyboard.

10  The getchar() function may be called successively to read the characters contained in a line of text.  The getchar() function returns any character keyed in. this include RETURN and TAB also. This means that it will not return, until u press ENTER.

11 Other functions  We can also determine whether the character typed from the keyboard is a letter or digit.  These tests can be done with the help of the following functions: isalpha(character) isdigit(character)

12  isalpha function assumes a value non- zero(TRUE) if the argument character contains an alphabet; otherwise it assumes 0(FALSE). Similar for isdigit function.

13  These all character functions are contained in the file ctype.h and therefore the statement #include Must be included in the program.  There are many other similar functions contained in this file.

14 1.isalnum(c) - Is c an alphanumeric character? 2.isalpha(c) – Is c an alphabetic character? 3. isdigit(c) – Is c a digit? 4. islower(c) – Is c a lower case letter? 5. isprint(c) –Is c a printable character? 6. isspace(c) –Is c a white space character? 7. isupper(c) – Is c an upper case letter?

15 Writing a character  Like getchar, there is an analogous function putchar for writing characters one at a time to the terminal.  It takes the following form: putchar(variable_name);

16  where variable_name is a type char variable containing a character. This statement displays the character contained in the variable_name at the terminal.

17 Example: char answer = ‘Y’; putchar(answer);  It will display the character Y on the screen.  The statement putchar(‘\n’); would cause the cursor on the screen to move to the beginning of the next line.

18 Formatted Input  Formatted input refers to an input data that has been arranged in a particular format.  For inputting the data, we use the scanf function. The general form of this scanf is :

19 Its.. scanf( “ controlString ”,arg1,arg2, … argn);  Conrol string specifies the field format in which the data is to be entered.  Arguments arg1, arg2, …..specify the address of locations where the data is stored.

20  The field (or format) specifications, consisting of the conversion character %, a data type character(or type specifier), and an optional number, specifying the field width.

21 Inputting Integer Numbers  The field specification for reading an integer number is: %wd  w is an integer number that specifies the field width of the number to be read and d, known as data type character.

22 Example:- scanf(“%2d%5d”,&num1,&num2); Input: 50 31426  Then, 5031426 num1num2

23 But if input : 31426 50 31426 num1num2 And the value 50 that is unread will be assigned to the first variable in the next scanf call.

24  On the other way if we have written : Scanf(“%d%d”,&num1,&num2); Will read the data 31426 50 Correctly in num1, num2.  Input data items must be separated by spaces,tabs or new lines.  When the scanf reads a particular value, reading of the value will terminate as soon as the number of characters specified by the field width is reached(if specified) or until a character that is not valid for the value being read is encountered.

25  An input field may be skipped by specifying * in the place of field width. For example, Scanf (%d%*d%d”,&a,&b); Input: 123 456 789 a skipped b 123456789 The data character d may be preceded by ‘ l ’ to read long integers. (%ld)

26  The field width of real numbers is not to be specified and therefore scanf uses simple specification %f for both the notations, decimal point and exponential notation.  For double type, %lf is used instead of %f.  A number may be skipped using %*f specification. Inputting Real Numbers

27 Inputting Character Strings  Single character can be read from the terminal using getchar function. The same can be achieved using the scanf function also.  In addition, a scanf can input strings containing more than one character.

28  The specifications for reading character strings are: %ws or %wc  %wc used to read a single character while %ws terminates the reading at the encounter of blank space.

29  All function arguments must be a pointers to variables.  Format specifications contained in the control string should match the argument order.  Input data items must be separated by spaces and must match the variables receiving the input in the same order.

30  The reading will be terminated, when the scan encounters an ‘ invalid mismatch ’ of data or a character that is not valid for the value being read.  When searching for a value, scanf ignores line boundaries and simply looks for the next appropriate character.  Any unread data items in a line will be considered as a part of the data input line to the next scanf call.

31  When the field width specifier w is used, it should be large enough to contain input data size.

32  It is highly desirable that the outputs are produced in such a way that they are understandable and are in an easy-to-use form.  It is therefore necessary for the programmer to give careful consideration to the appearance and clarity of the output produced by program.

33  printf statement provides certain features that can be effectively control the alignment and spacing of print- outs on the terminals.

34 printf( “ control string ”,arg1,arg2....argn); Control string consists of three types of items: 1.Characters that will be printed on the screen as they appear. 2.Format specifications that will define the output format for display of each item.

35 3.Escape sequence characters such as \n, \t, and \b.

36  The format specification for printing an integer is %wd. Where w specifies the minimum field width for the output. Format Output  Printf(“%d”,9876)  Printf(“%6d”,9876)  Printf(“%2d”,9876) 9876 9876 9876

37  Printf(“%-6d”,9876)  Printf(“%06d”,9876) 9876 009876

38  The output of real number may be displayed in decimal notation using the format %w.pf If y = 98.7654 Format Output Printf(“%7.4f”,y) Printf(“%7.2f”,y) Printf(“%-7.2f”,y) Printf(“%f”,y) 98.7654 98.77 98.77 98.7654

39 Printing of a single character  A single character can be displayed in a desired position using the format %wc.  The character will be displayed right- justified in the field of w columns.  We can make the display left-justified by placing a minus sign before w.  The default value for w is 1.

40 Printing of strings  The format specification for outputting string is of the form %w.ps.  To print string “ NEW DELHI 110001 ”, containing 16 characters. %20s 1102020

41 To print string “ NEW DELHI 110001 ”, containing 16 characters. %20s %20.10s NEWDELHI110001 NEWDELHI

42 Difference between %[a-z] %[^a-z]

43

44

45 sizeof operator  when used with operand, it returns the no. of bytes the operand occupies… i.e. sizeof(int) is returns 2 sizeof(char)returns 1..on 16bit processor. Q.float x;printf(“%d”,sizeof(x)); what will be the output?Why?

46 Precedence of arithmetic operator High priority * / % Low priority+ - The basic evaluation includes left to right passes….. During first pass higher priority opr is applied as they encounter….during second pass from left to right the low priority operators are applied as they are encountered.

47 example i.e x = 9-12/3+3*2-1; pass 1:x = 9 – 4 + 3*2 –1; x = 9 – 4 + 6 –1; Pass 2: x= 5 +6 –1; x=11-1; x=10;

48 Use of parenthesis to override normal precedence X= 9 – 12 / (3+3) *(2-1) Pass 0:x=9 – 12 / 6 * (2-1) x=9 – 12 / 6 * 1; Pass 1:x=9 – 2 * 1; x=9 – 2; Pass 2:x=7 Thus, when parenthesis are used, the expressions within parenthesis assume highest priority.

49 Associativity The operator having same precedence are evaluated either from left to right or right to left…..depending of the level ….known as associativity.

50  There are two ways for type conversion. 1) Implicit Type Conversion 2) Explicit Type Conversion

51  Automatic conversion. e.g. a=3 b=2 int i; float a,b; i=a/b; here, value of i will be 1. instead of 1.5

52  (type-name) expression Where, type-name is one of the standard C data types. Expression may be a constant, variable or an expression.

53  i=( int ) 7.5 Means, 7.5 is converted to integer by truncation.  i=( int )21.3/ ( int )4.5 Means, Evaluated as 21/4 and result would be 5.

54  Casting can be used to round-off a given value. HOW??? x = (int) (y+0.5); if y = 27.6 then, y+0.5=28.1 and on casting the result becomes 28.

55


Download ppt "Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs."

Similar presentations


Ads by Google