Input And Output

3,910 views

Published on

Published in: Technology
6 Comments
9 Likes
Statistics
Notes
No Downloads
Views
Total views
3,910
On SlideShare
0
From Embeds
0
Number of Embeds
325
Actions
Shares
0
Downloads
0
Comments
6
Likes
9
Embeds 0
No embeds

No notes for slide

Input And Output

  1. 1. Input and Output A Lecture by Abdul ghaffar Khan Chapter #. 6
  2. 2. Input and Output <ul><li>Input: </li></ul><ul><li>Input is a process of entering the data ito the computer’s memory </li></ul><ul><li>Output: </li></ul><ul><li>Output is a process of translating data that are in machine-readable form into a form understandable by humans or readable by other machines. </li></ul>
  3. 3. I/O Functions <ul><li>Character I/O </li></ul><ul><ul><li>getchar() & putchar() </li></ul></ul><ul><li>Formatted I/O </li></ul><ul><ul><li>printf() and scanf() </li></ul></ul><ul><li>String I/O </li></ul><ul><ul><li>gets() and puts() </li></ul></ul><ul><ul><li>Note : To use any of these function first include the appropriate header file in your program. </li></ul></ul>
  4. 4. Character I/O <ul><li>getchar() function </li></ul><ul><li>getchar() function is used to enter a single character through the standard input device generally keyboard . The syntax of the function is </li></ul><ul><li>variable = getchar() </li></ul>
  5. 5. Character I/O <ul><li>putchar() function </li></ul><ul><ul><li>putchar() function displays a character on the screen. This function takes a character as an argument.The syntax of the function is </li></ul></ul><ul><ul><li>putchar( variable ) </li></ul></ul>
  6. 6. Character I/O <ul><li>Example 6.1 </li></ul><ul><ul><li>#include <stdio.h> </li></ul></ul><ul><ul><li>#include <ctype.h> </li></ul></ul><ul><ul><li>void main ( void ) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>char grade; </li></ul></ul><ul><ul><li>printf(“Type a character? “); </li></ul></ul><ul><ul><li>grade = getchar(); </li></ul></ul><ul><ul><li>grade=toupper(grade); </li></ul></ul><ul><ul><li>printf(“Your grade is “); </li></ul></ul><ul><ul><li>putchar(grade); </li></ul></ul><ul><ul><li>} </li></ul></ul>
  7. 7. Formatted I/O <ul><li>Formatted I/O means to control the type, width, precision and other attribute of the data during input and output process. Two functions are mostly used in C language for this purpose are </li></ul><ul><ul><li>printf() </li></ul></ul><ul><ul><li>scanf() </li></ul></ul>
  8. 8. Formatted I/O <ul><li>Printf Function: </li></ul><ul><ul><li>printf is function used to output any type of data either characters, strings, numbers etc.. The syntax of printf function is </li></ul></ul><ul><ul><li>printf(format_string, var1, var2,….varn); </li></ul></ul>
  9. 9. Printf Function <ul><li>Format string </li></ul><ul><ul><li>The format string is a character string that may contain two types of elements </li></ul></ul><ul><ul><li>Plain text </li></ul></ul><ul><ul><ul><li>Any plain text included in the format string displayed on the screen as it is except Escape Sequence Characters and Format Specifiers. </li></ul></ul></ul><ul><ul><li>Format Specifiers. </li></ul></ul><ul><ul><ul><li>Format Specifiers are instructions to the function to display a data in a specific format. (continue….) </li></ul></ul></ul>
  10. 10. Printf Function <ul><li>The syntax of one format specifier is as follows </li></ul><ul><li>% [flag] [width] [.prec] conversion_char </li></ul><ul><li>in a format string there may be any number of format specifiers. </li></ul><ul><ul><li>Flags (optional) </li></ul></ul><ul><ul><ul><li>- , left justifies the result </li></ul></ul></ul><ul><ul><ul><li>+ , always puts a sign before a number depending on the number </li></ul></ul></ul><ul><ul><ul><li>blank , positive value will have a space before it and negative number will have a minus sign before the number. </li></ul></ul></ul><ul><ul><li>Width (optional) </li></ul></ul><ul><ul><li> This is an integer value which specifies the minimum width of the data. </li></ul></ul><ul><ul><li>(continue..) </li></ul></ul>
  11. 11. Printf Function <ul><ul><li>Precision (optional) </li></ul></ul><ul><ul><ul><li>Is an integer number to specify the number of decimal places to be displayed in the output for a floating point number. </li></ul></ul></ul><ul><ul><li>Conversion character (format specifier) </li></ul></ul><ul><ul><ul><li>There are as many conversion characters as the standard data types are in the ‘C’ language some of those are listed below </li></ul></ul></ul><ul><ul><ul><li>d sign decimal integer </li></ul></ul></ul><ul><ul><ul><li>u unsigned decimal integer </li></ul></ul></ul><ul><ul><ul><li>id long </li></ul></ul></ul><ul><ul><ul><li>f floating point </li></ul></ul></ul><ul><ul><ul><li>e floating with exponent </li></ul></ul></ul><ul><ul><ul><li>o octal integer </li></ul></ul></ul><ul><ul><ul><li>x hexadecimal </li></ul></ul></ul><ul><ul><ul><li>c single character </li></ul></ul></ul><ul><ul><ul><li>s string (multiple characters) (continue…) </li></ul></ul></ul>
  12. 12. Printf Function <ul><li>Variable list </li></ul><ul><ul><ul><li>The second argument of the function is a list of variables. Here variables , values and expressions can be used to supply the value to the function too display. </li></ul></ul></ul><ul><li>Example 6.2: </li></ul><ul><ul><ul><li>int x=34; </li></ul></ul></ul><ul><ul><ul><li>float y=45.34; </li></ul></ul></ul><ul><ul><ul><li>char c =‘F’; </li></ul></ul></ul><ul><ul><ul><li>char s[] = “this is a string”; </li></ul></ul></ul><ul><ul><ul><li>Printf(“ Output is %d %c %f %s ”,x,c,y,s); </li></ul></ul></ul><ul><ul><ul><li>Output is 34 F 45.340000 this is a string </li></ul></ul></ul>
  13. 13. Example 6.3 <ul><li>#include <stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main (void) </li></ul><ul><li>{ </li></ul><ul><li>clrscr(); </li></ul><ul><li>int roll_no = 450, marks=760; </li></ul><ul><li>float per,total= 900.0; </li></ul><ul><li>char section = 'B'; </li></ul><ul><li>per = (marks / total) *100.0; </li></ul><ul><li>printf(&quot;Rol1 Number %d &quot;,roll_no); </li></ul><ul><li>printf(&quot;Section %c &quot;,section); </li></ul><ul><li>printf(&quot;You Secure %d marks out of %f marks &quot;, marks, total); </li></ul><ul><li>printf (&quot;Percentage %f &quot;,per); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>Output: Rol1 Number 450 Section B You Secure 760 marks out of 900 marks Percentage 84.444443
  14. 14. Example 6.4 <ul><li>#include <stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>int sum = 29766; </li></ul><ul><li>float radius = 3.7654; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot;Sum=%2d &quot;,sum ); </li></ul><ul><li>printf(&quot;Sum=%d &quot; ,sum ); </li></ul><ul><li>printf(&quot;Sum=%7d &quot;,sum); </li></ul><ul><li>printf(&quot;Sum=%10d &quot;,sum); </li></ul><ul><li>printf(&quot;Radius=%f &quot;, radius); </li></ul><ul><li>printf(&quot;Radius=%10f &quot;, radius); </li></ul><ul><li>printf(&quot;Radius=%15f&quot;, radius); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><ul><li>Sum=29766 </li></ul></ul><ul><ul><li>Sum=29766 </li></ul></ul><ul><ul><li>Sum= 29766 </li></ul></ul><ul><ul><li>Sum= 29766 </li></ul></ul><ul><ul><li>Radius=3.765400 </li></ul></ul><ul><ul><li>Radius= 3.765400 </li></ul></ul><ul><ul><li>Radius= 3.765400 </li></ul></ul>
  15. 15. Example 6.5 <ul><li>#include <stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main (void) </li></ul><ul><li>{ </li></ul><ul><li>clrscr(); </li></ul><ul><li>int a=2438; </li></ul><ul><li>int b=1590; </li></ul><ul><li>int c = 2107; </li></ul><ul><li>printf(&quot;%10d%10d%10d &quot;,a,b,c); </li></ul><ul><li>printf(&quot;%-10d %-10d %-10d &quot;,5*a,b*3,c*7); </li></ul><ul><li>printf(&quot;%-10d %-10d %-10d &quot;,a,b,c); </li></ul><ul><li>printf(&quot;%+d %+d %+d &quot;,5*a,-b*3,c*7); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><ul><li>2438 1590 2107 </li></ul></ul><ul><ul><li>12190 4770 14749 </li></ul></ul><ul><ul><li>2438 1590 2107 </li></ul></ul><ul><ul><li>+12190 -4770 +14749 </li></ul></ul>
  16. 16. Example 6.6 <ul><li>#include <stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main (void) </li></ul><ul><li>{ </li></ul><ul><li>clrscr(); </li></ul><ul><li>float tot= 36.87; </li></ul><ul><li>float per= 76.586588788; </li></ul><ul><li>printf(&quot;Total Marks %f &quot;,tot); </li></ul><ul><li>printf(&quot;Percentage %f &quot;,per); </li></ul><ul><li>printf(&quot;Total Marks %.1f &quot;,tot); </li></ul><ul><li>printf(&quot;Percentage %.1f &quot;,per); </li></ul><ul><li>printf(&quot;Total Marks %.8f &quot;,tot); </li></ul><ul><li>printf(&quot;Percentage %.8f &quot;,per); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><ul><li>Total Marks 36.869999 </li></ul></ul><ul><ul><li>Percentage 76.586586 </li></ul></ul><ul><ul><li>Total Marks 36.9 </li></ul></ul><ul><ul><li>Percentage 76.6 </li></ul></ul><ul><ul><li>Total Marks 36.86999893 </li></ul></ul><ul><ul><li>Percentage 76.58658600 </li></ul></ul>
  17. 17. Scanf Function <ul><li>scanf function is used to enter the data into computers memory through standard input device generally keyboard. It can be used to enter multiple data items by a single statement. The syntax is as follows, </li></ul><ul><li>scanf(format_string,&var1,&var2,….,&varn) </li></ul><ul><li>e.g. scanf(“%d”,&x); </li></ul>
  18. 18. Scanf Function <ul><li>Format string </li></ul><ul><ul><li>Format string is a character string which contains three types of items. </li></ul></ul><ul><ul><ul><ul><li>White space characters </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Non-white space characters </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Format specifiers </li></ul></ul></ul></ul>
  19. 19. Scanf Function <ul><li>White space characters </li></ul><ul><ul><li>white space characters are those characters that are not visible on the screen. These are space, tab ( ) or new line ( ) characters. If a white space character is included in the format string it reads all consecutive white space character from the input but does not store it in a variable. Until it finds the next non white space character. </li></ul></ul>
  20. 20. Scanf Function <ul><li>Non-White space characters </li></ul><ul><ul><li>All ASCII characters are none-white space characters, except %. If such a character is included in a format string, scanf will read the matching character from the input, but not store. </li></ul></ul>
  21. 21. Scanf Function <ul><li>Format Specifiers </li></ul><ul><ul><li>The format specifiers are characters that direct the scanf function to read and convert characters from the input into the specific type of values. Then store them in the location given by the address of variable. A format specifier begins with a percentage sign (%) and followed a width specifier, which is followed by a conversion character, same as in printf function. The syntax of format specifier is as follows.. </li></ul></ul><ul><ul><li>% [width] conversion_character </li></ul></ul>
  22. 22. Example 6.7 <ul><ul><ul><li>#include <stdio.h> </li></ul></ul></ul><ul><ul><ul><li>#include <conio.h> </li></ul></ul></ul><ul><ul><ul><li>void main (void) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>clrscr(); </li></ul></ul></ul><ul><ul><ul><li>int rollno,tot; </li></ul></ul></ul><ul><ul><ul><li>float per,marks; </li></ul></ul></ul><ul><ul><ul><li>printf(&quot;Enter roll number &quot;); </li></ul></ul></ul><ul><ul><ul><li>scanf(&quot;%d&quot;,&rollno); </li></ul></ul></ul><ul><ul><ul><li>printf(&quot;Enter marks &quot;); </li></ul></ul></ul><ul><ul><ul><li>scanf(&quot;%f&quot;,&marks); </li></ul></ul></ul><ul><ul><ul><li>printf(&quot;Enter total marks &quot;); </li></ul></ul></ul><ul><ul><ul><li>scanf(&quot;%d&quot;,&tot); </li></ul></ul></ul><ul><ul><ul><li>printf(&quot; Your percentage is %.2f&quot;,(100.0 * marks/tot)); </li></ul></ul></ul><ul><ul><ul><li>getch(); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  23. 23. Example 6.8 <ul><ul><ul><li>#include <stdio.h> </li></ul></ul></ul><ul><ul><ul><li>#include <conio.h> </li></ul></ul></ul><ul><ul><ul><li>void main (void) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>clrscr(); </li></ul></ul></ul><ul><ul><ul><li>int hr,mn,sec; </li></ul></ul></ul><ul><ul><ul><li>scanf(&quot;%d:%d:%d&quot;,&hr,&mn,&sec); </li></ul></ul></ul><ul><ul><ul><li>printf(&quot; Hours %d&quot;,hr); </li></ul></ul></ul><ul><ul><ul><li>printf(&quot; Minutes %d&quot;,mn); </li></ul></ul></ul><ul><ul><ul><li>printf(&quot; Seconds %d&quot;,sec); </li></ul></ul></ul><ul><ul><ul><li>getch(); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  24. 24. Example 6.9 <ul><ul><ul><li>#include <stdio.h> </li></ul></ul></ul><ul><ul><ul><li>#include <conio.h> </li></ul></ul></ul><ul><ul><ul><li>void main (void) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>clrscr(); </li></ul></ul></ul><ul><ul><ul><li>int x,y,a,b; </li></ul></ul></ul><ul><ul><ul><li>scanf(&quot;%2d%3d&quot;,&x,&y); </li></ul></ul></ul><ul><ul><ul><li>scanf(&quot;%d%d&quot;,&a,&b); </li></ul></ul></ul><ul><ul><ul><li>printf(&quot; x = %d y = %d a = %d b = %d &quot;,x,y,a,b); </li></ul></ul></ul><ul><ul><ul><li>getch(); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>

×