Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. To perform these tasks user friendly.

Similar presentations


Presentation on theme: "Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. To perform these tasks user friendly."— Presentation transcript:

1 Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. To perform these tasks user friendly C has a number of input/Output functions. When a program needs data, it takes the data through the input functions and sends results obtained through the output functions. Thus the input/output functions are link between the user and the terminal. There are numbers of I/O function in C based on the data types. They are classified in to two types. Formatted Function Unformatted Function 1Basic of Computer & 'C' Programming

2 Formatted Functions :- The formatted input/output functions read and write all types of data values. They require conversion symbol to identify the data type. Hence, they can be used for both reading and writing of all data values. The formatted functions return the values after execution. The return value is equal to the number of variables successfully read/written. Using this value the user can find out the errors occurring during reading the data. 2Basic of Computer & 'C' Programming

3 Unformatted Functions :- The unformatted input/output functions only work with the character data types. They do not require conversion symbol for identification of data types because they work only with character data type. There is no need to convert the data. In case values of other data types are passed to these function, they are treated as the character data. The unformatted functions also return values, but return value of unformatted function is always the same. 3Basic of Computer & 'C' Programming

4 FormattedUnformatted Input Output scanf() printf()getch() putch() getchar() puts() gets() getche() putchar() 4Basic of Computer & 'C' Programming

5 The printf() statements:- The printf() function prints all types of data values to the console. It requires conversion symbol and variable names should be same in number. The scanf() statements:- The scanf() statements reads all types of data values. It is used for runtime assignments of variable. The scanf() statements also requires conversion symbol of the program. Syntax of scanf(“%d %f %c”,&a,&b,&c); 5Basic of Computer & 'C' Programming

6 6

7 7

8 Unformatted Function:- Character I/O String I/O Files I/O 1. Character I/O:- a) getchar():- This function reads character types data from the standard input. It reads one character at a time till the user presses the enter key. b) putchar():- This function prints one character on the screen at a time which is read by the standard input. c) getch() & getche():- These function read any alphanumeric characters from the standard input device. The character entered is not displayed by getch() function. d) putch():- This function prints any alphanumeric character taken by the standard input device. 8Basic of Computer & 'C' Programming

9 2.String I/O:- a) gets():- This function is used for accepting any string through stdin(keyboard) until enter key is pressed. The header file stdio.h is needed for implementing the above function. b) puts():- This function prints the strings or character array. c) cgets():- This function reads string from the console. Syntax: cgets (char *st); d) cputs():- This function display string on the console. Syntax: cputs (char *st); 9Basic of Computer & 'C' Programming

10 Commonly used Library Functions:- a) clrscr():- This function is used to clear the screen. It cleans the previous output from the screen and display the output of the current program from the first line of the screen. It is defined in conio.h header file. Syntax: clrscr(); b) exit():- This function terminates the program. It is defined in process.h header file. Syntax:- exit(); c) sleep():- This function pauses the execution of the program for a given number of seconds. The number of seconds is to be enclosed between the parenthesis. It is defined in dos.h header file. Syntax:- sleep (1); d) system():- This function is helpful in executing the different dos commands. It return 0 on success and -1 on failure. Syntax:- system (“dir”); This command should be enclosed within the double quotation mar ks. 10Basic of Computer & 'C' Programming

11 Programs: Q. WAP to add Two No. #include void main(); { int a,b,c; printf(“Enter Two No”); scanf(“%d %d”, &a,&b); c=a+b; printf(“Sum =%d”, c); } 11Basic of Computer & 'C' Programming

12 Q. WAP to Subtract Two No. #include void main(); { int a,b,c; printf(“Enter Two No”); scanf(“%d %d”, &a,&b); c=a-b; printf(“Subtraction =%d”, c); } Q. WAP to Multiply Two No. #include void main(); { int a,b,c; printf(“Enter Two No”); scanf(“%d %d”, &a,&b); c=a*b; printf(“Multiply =%d”, c); } 12Basic of Computer & 'C' Programming

13 Q. WAP to Divide Two No. #include void main(); { int a,b,c; printf(“Enter Two No”); scanf(“%d %d”, &a,&b); c=a/b; printf(“Divide =%d”, c); } Q. WAP to Print Square of a Given No. #include void main(); { int a,c; printf(“Enter Any No”); scanf(“%d”, &a); c=a*a; printf(“Square of Given No =%d”, c);} 13Basic of Computer & 'C' Programming

14 Q. WAP to swap the values of two variable without using third variable. #include void main(); { int a=10,b=20; printf(“A=%d B=%d”,a,b); a=a + b; b=a - b; a=a - b; printf(“Now A=%d B =%d”,a,b); } 14Basic of Computer & 'C' Programming

15 Q. WAP to calculate average of Three real Nos. #include void main() { float a,b,c,d; printf(“Enter Three Float Nos”); scanf(“%f%f%f”,&a,&b,&c); d=a+b+c; printf(“ Average of Given No =%f,d/3); } 15Basic of Computer & 'C' Programming

16 Thanks 16Basic of Computer & 'C' Programming


Download ppt "Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. To perform these tasks user friendly."

Similar presentations


Ads by Google