Reading the data from the input devices and displaying the results on the screen are the two main...

16
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 1 Basic of Computer & 'C' Programming

Transcript of Reading the data from the input devices and displaying the results on the screen are the two main...

Page 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.

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

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

Formatted Unformatted

Input Output Input Output

scanf() printf() getch() putch()

getchar() puts()

gets()

getche() putchar()

4Basic of Computer & 'C' Programming

Page 5: 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.

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

Page 6: 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.

6Basic of Computer & 'C' Programming

Page 7: 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.

7Basic of Computer & 'C' Programming

Page 8: 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.

Unformatted Function:-Character I/OString I/OFiles 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

Page 9: 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.

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

Page 10: 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.

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 marks.

10Basic of Computer & 'C' Programming

Page 11: 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.

Programs: Q. WAP to add Two No.

#include<stdio.h>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

Page 12: 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.

Q. WAP to Subtract Two No. #include<stdio.h>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<stdio.h>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

Page 13: 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.

Q. WAP to Divide Two No.#include<stdio.h>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<stdio.h>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

Page 14: 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.

Q. WAP to swap the values of two variable without using third variable.

#include<stdio.h>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

Page 15: 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.

Q. WAP to calculate average of Three real Nos.

#include<stdio.h>

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

Page 16: 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.

Thanks

16Basic of Computer & 'C' Programming