PPS Chapter 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Input and Output functions in C language

Input: The data items provided to computer.


Output: The data items given by computer to user.
How we use input and output devices in c language?
Ans: Computer languages uses concept of “stream” to connect input and output devices to
computer program.
Stream: flow of data.
The channel/path to move data from one place to other place.
Input stream: It is used to move data from input devices to computer program.
The channel to move data from input devices to computer program.
Input stream
Key board Computer program

Output stream:It is used to move data from computer program to output devices.
The channel to move data from computer program to output devices.

Computer program Monitor


Output stream

Note: Streams are unidirectional. Input stream is only used to accept/ read data, they are not used to
display/write data.

In C language streams are represented with functions like scanf(),printf(),getchar(),putchar() etc.


These functions are defined in one header file called stdio.h
stdio=> standard input output
Why every C program includes stdio.h header file?
Ans: Beacuse standard input and output function defination is defined in stdio.h file.

Classfication of I/O functions:


C I/O functions Formatted functions Formatted input scanf()
functions
Formatted output printf()
functions
Unformatted functions Unformatted input getchar(),gets()
functions
Unformatted output putchar(),puts()
functions

Unformatted functions: These functions are only used to read/write data in the form of character
(char) data type.
They do not require “control string”/”format specifier” to differentiate one data type from other.
Formatted functions: These functions are used to read/write data in all data types (int, long, short,
float, double, char)
They require conversion symbol which differentiate one data type from other, called “control
string”/”format specifier”. (eg. %d, %f, %c etc)

1. getchar(): This function is used to read only one charcter at a time from keyboard.
General syntax: character_variable=getchar();
eg. char ch;
ch=getchar();
2. putchar(): This function is used to write only one charcter at a time on monitor.
General syntax: putchar(character_variable);
eg. char ch;
ch=getchar();
putchar(ch);

3. gets(): This function is used to read one or more charcters (string) at a time from keyboard.
General syntax: gets(string_variable);
4. puts():This function is used to write one or more charcters at a time on monitor.
General syntax: puts(string_variable);

eg. #include<stdio.h>
void main()
{
char line[80]; //declaration of string
gets(line); // read and store string in line variable
puts(line); // write/display sring on monitor
}

5. printf(): The printf() function is used to print/display/write data (message/result of operation) on


output device i.e. monitor.
eg. printf(“Hi How are you”); =>printf is used to print simple message
eg. printf(“%d”,(2+3)); =>printf is used to print result of operation
eg. int a=10;
int b=20;
int c;
c=a+b;
printf(“%d”,c);

printf() function is used to display data in all data types (like int, float, char). To differentiate one
data type from another this function uses format specifier.
General syntax:
1. To print one value at a time
printf(“format_specifier”,argument);
2. To print more than one value at a time
printf(“format_specifier,format_specifier,format_specifier”,argument1,argument2,argument3);
3. To print simple message
printf(“your message”);

format specifiers/control strigs/conversion characters:


Charater meaning
%d Data item is a decimal integer (signed)
%ld Data item is a long integer (signed)
%f or %g Data item is a float
%lf Data item is a double
%c Data item is a character
%u Data item is a unsigned integer
%s Data item is a string
%o Data item is a octal integer
%x Data item is a hexadecimal integer

6. scanf(): The scanf() function is used to read/accept data from input device i.e. key board. scanf()
function is used to read data in all data types (like int, float, char). To differentiate one data type
from another this function uses format specifier.
General syntax:
1. To read one value at a time
scanf(“format_specifier”,&argument);
2. To read more than one value at a time
scanf(“format_specifier,format_specifier,format_specifier”,&argument1,&argument2,&argument3);

& => address operator. The scanf() function requires & operator. The address operator is used to
print address (memory location) of the variable.
In the scanf() statement the role of ‘&’ operator is to indicate memory location of the variable, so
that the value raed from key board is placed at that location.

You might also like