Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit-4, Chapter-2 Managing Input and Output Operations

Similar presentations


Presentation on theme: "Unit-4, Chapter-2 Managing Input and Output Operations"— Presentation transcript:

1 Unit-4, Chapter-2 Managing Input and Output Operations

2 Agenda Reading a Character Writing a Character Formatted Input
Formatted Output

3 Reading a Character To read a single character from the standard input device, getchar function can be used. Var_name= getchar(); Example: char name; name=getchar();

4 Functions related to characters are
getchar() can be used inside a loop with proper terminating condition, to accept multiple characters. Functions related to characters are isalnum(c) - Is ‘c’ an alphanumeric character? isalpha(c) - alphabet isdigit(c) - digit islower(c) - lower case letter isprint(c) - printable character

5 ctype.h header file should be mentioned in the program
ispunct(c) - punctuation mark isspace(c) - white space isupper(c) - upper case letter ctype.h header file should be mentioned in the program

6 Writing a Character To print a single character on the standard output device, putchar function is used. putchar(var_name); name=‘Y’; putchar(name); To print a new line character putchar(‘\n’);

7 scanf(“control string”,arg1,arg2,arg3,…argn);
Formatted Input We go for formatted input when we have to input multiple values of different types Example: John scan formatted (scanf) is the formatted input statement. Syntax: scanf(“control string”,arg1,arg2,arg3,…argn);

8 Inputting Integer Numbers
Control string specifies the format of the data and arg1, arg2…argn specifies the address or the location. Control string may contain numbers specifying the numbers of characters to be inputted. Inputting Integer Numbers Field specification will contain % w d w- integer number specifying the field width d- integer number will be saved

9 scanf (“%2d %5d”, &n1,&n2); If input is , then 50 is assigned to n1 and is assigned to n2. If input is , then 31 is assigned to n1 and 426 is assigned to n2. 50 is kept in the buffer, and used in the next scanf statement. If scanf statement encounters incorrect input then it stops taking further inputs.

10 scanf (“%d %*d %d”, &a, &b);
Values can be skipped using * symbol. scanf (“%d %*d %d”, &a, &b); If the input line is , then 123 is assigned to a, 456 is skipped and 789 is assigned to b. “ld” indicates that number is long integer and “h” indicates that number is short integer.

11 Do not use unnecessary symbols in scanf statement.
Example: scanf (“%d-%d”,&n1,&n2); For the above scanf statement the inputs will be successfully read only if the inputs are given as 4-2 or 8-6, i.e First value followed by ‘-’ symbol and followed by second values.

12 scanf (“%f %f %f”, &x,&y,&z);
Inputting Real Numbers Field width is not specified in case of floating point values. scanf (“%f %f %f”, &x,&y,&z); To read double values, use %lf instead of %f To skip values use ‘ * ’ symbol.

13 Inputting Character Strings
%ws or %wc is used to input specified number of characters. Some versions of scanf also support following formats %[characters] - accepts these characters %[^characters] - Do not accept these characters

14 X=scanf(“%d %c %f %s”, &n1,&ch,&n2,name);
Reading Mixed Data Types To achieve this the order and the type should be specified correctly scanf(“%d %c %f %s”, &n1,&ch,&n2,name); scanf returns the number of values successfully read. This can be used for Error detection in inputs. X=scanf(“%d %c %f %s”, &n1,&ch,&n2,name); If all the inputs are taken successfully then x will have the values 4.

15 printf(“control string”,arg1,arg2,…argn);
Formatted Output The printf statement is used to display the results in the required alignment and format Syntax: printf(“control string”,arg1,arg2,…argn); Control string can have characters, Format specifications and Escape sequence characters

16 w- an integer values specifying total width
% w.p type-specifier w- an integer values specifying total width p- integer value that specifies the number of digits after decimal point Output of Integer values % w d w is the width that has to printed, if the original values is more than w, then the whole values is printed.

17 Default is right justified, to left justify it we use ‘-’ symbol.
printf(“%d”, 9876); printf(“%6d”, 9876); printf(“%2d”,9876); printf(“%-6d”, 9876); printf(“%06d”,9876); Default is right justified, to left justify it we use ‘-’ symbol. To add preceding 0s we use 0 in the control string 9 8 7 6 9 8 7 6 9 8 7 6 9 8 7 6 9 8 7 6

18 Output of Real Numbers % w.p f
w- min number of positions to be displayed p- number of digits after decimal point If y= printf(“%7.4f”,y); printf(“%7.2f”,y); printf(“%-7.2f”,y); printf(“%e”,y); 9 8 . 7 6 5 4 9 8 . 7 7 9 8 . 7 7 9 . 8 7 6 5 4 e + 1

19 Printing a Single Character
printf (“*.*f”, w,p,y); User can define the field size at run time. Printing a Single Character To print a single character, we use %w c Default is right justified, ‘ – ‘ is used to make it left justified Printing of Strings Format is like printing real numbers % w.ps

20 (Please refer text book for example)
w- specifies the field width p- indicates the number of characters to be printed Default is right justified, ‘ - ’ is used to make it left justified. (Please refer text book for example)

21 Mixed Data Output printf is used to print mixed data values as shown below printf (“%d %f %s %c”, a,b,c,d); printf returns the number of characters printed.

22


Download ppt "Unit-4, Chapter-2 Managing Input and Output Operations"

Similar presentations


Ads by Google