0% found this document useful (0 votes)
37 views19 pages

I - O Statements

I/O statement in C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views19 pages

I - O Statements

I/O statement in C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Programming

Programming In in
C C
Part-5
(Input Output Statements)

Input-Output MS-PowerPoint

Statements
By:
Ganesh Kumar Sethi
Assistant Professor
MM Modi College
Patiala

Ganesh K Sethi
Input Output Statements
The main aim ofProgramming In C is to get some
writing any program
Part-5
desired output from it by giving some input to the
(Input Output Statements)
program.
 The program performs some processing on the data
and produces output
MS-PowerPoint
according to user’s
specifications.
By:
 All input/output operations are carried out through
Ganesh Kumar Sethi
Assistant Professor
C library functions, which can be linked to any
MM Modi College
Patiala

program. These functions are collectively known as


standard I/O library functions.
Input Output Statements
Programming
These functions are In C functions since
also called console
Part-5
mostly input data is given from a standard input
(Input Output Statements)
device i.e. keyboard and output data is displayed on a
standard output device i.e. monitor.
These functions are defined
MS-PowerPoint
in ‘stdio.h’ header file
which stands for standard input output header file.
By:
Ganesh Kumar Sethi
Assistant Professor
MM Modi College
Patiala

Ganesh K Sethi
Input Output Statements
Programming In C
Unformatted I/OPart-5 Formatted I/O
(Input Output Statements)
No particular format Particular format

MS-PowerPoint
Character I/O
String I/O
getchar() By: printf()
Ganesh Kumar Sethi
getche() gets()
Assistant Professor scanf()
MM Modi College
Patiala puts()
getch()

putchar()
Character Input -> getchar()
Programming
It is a part of standard C languageInI/OClibrary i.e.
<stdio.h>. Part-5
(Input Outputfrom
Statements)
It returns a single character a standard input
device i.e. a keyboard.
The typed character is MS-PowerPoint
echoed (visible) on the screen.
After typing the appropriate character, the user is
required toBy:press Enter key.
Ganesh Kumar Sethi
The generalAssistant
syntax of using getchar function is
Professor
MM Modi College
character variable = getchar( ) ;
Patiala

Ganesh K Sethi
Character Input -> getche()
This function alsoProgramming
returns a character In that
C has been
recently typed. Part-5
The character is(Input
also echoed
Output on the screen,
Statements)
But it is not required to press Enter key after typing
the character.
MS-PowerPoint
The syntax of using getche( ) is same as getchar( ).
The generalBy:syntax of using getchar function is
character variable = getche( ) ;
Ganesh Kumar Sethi
Assistant Professor
MM Modi College
Patiala

Ganesh K Sethi
Character Input -> getch()
This function alsoProgramming
returns a character In that
C has been
recently typed. Part-5
But here, user (Input Output Statements)
is not required to press the Enter key
and the character being typed is not echoed on the
screen. MS-PowerPoint
The syntax of using getch( ) function is same as
getchar( ) and
By: getche( ). This function is used to input
passwords Ganesh
for a system,
Kumar Sethi which have to be hidden for
Assistant Professor
security purposes.
MM Modi College
Patiala
The general syntax of using getchar function is
character variable = getch( ) ;
Character Output -> putchar()
Programming
This function is used In C constant or a
to print a character
character variable to thePart-5
standard output device which
(Input Output
is generally computer monitor. Statements)
To use this function, <stdio.h> header file is to be
included in the program.
MS-PowerPoint
Syntax is:
putchar(character
By: variable or constant)
Ganesh Kumar Sethi
Assistant Professor
MM Modi College
Patiala

Ganesh K Sethi
Character Input/Output -Example
#include<stdio.h> Programming In C
main ( ) Part-5
{ (Input Output Statements)
char ch ;
printf (“Enter any character :”)
MS-PowerPoint
ch = getchar( ) ;
printf (“\n Typed character is :”) ;
By:
putchar (ch) ; Kumar Sethi
Ganesh Note: We can also use
} Assistant Professor
MM Modi College
getch() or getche() in
Patiala
Output: Enter any character:B place of getchar().
Typed character is:B But working will be
different.
String Input -> gets()
This function reads Programming
a string from the Inkeyboard.
C
Part-5 of characters stored
A string is defined as a collection
(Input Output Statements)
in consecutive memory locations.
The length of the string accepted by this function is
limited by the declaration of the string variable.
MS-PowerPoint
The input string may consist of multiple words. The
input is completed
By: by pressing Enter key.
Ganesh Kumar Sethi
Syntax is: Assistant Professor
MM Modi College
gets(string variable)
Patiala

Ganesh K Sethi
String Output -> puts()
Programming
This function is used In Cconstant or a
to output a string
Part-5output device.
string variable to the standard
(Input Output Statements)
Syntax is:
puts(string variable or constant)
MS-PowerPoint

By:
Ganesh Kumar Sethi
Assistant Professor
MM Modi College
Patiala

Ganesh K Sethi
String Input/Output -Example
# include <stdio.h> Programming In C
main ( ) Part-5
{ (Input Output Statements)
char str [51] ;
puts (“\n Enter any string of length not more than 50 :”) ;
MS-PowerPoint
gets (str) ;
puts (“\n Entered string is :”) ;
By:
puts (str) ;Ganesh Kumar Sethi
Assistant Professor
} MM Modi College
Enter any stringPatiala
of length not more than 50: MM Modi College
Entered string is: MM Modi College
Formatted Input –scanf()
Input data can be enteredProgramming
into a computerIn C keyboard by
from
Part-5
means of a C library function scanf( ).
This function can be(Input
used toOutput
enter any combination of numerical
Statements)
values like int or float, characters or strings.

MS-PowerPoint

By:
Ganesh Kumar Sethi
Assistant Professor
MM Modi College
Patiala

Ganesh K Sethi
Formatted Input –scanf()
Its syntax is Programming In C
Part-5
scanf (“format specifier”, &arg1, &arg2, …….,&argn) ;
(Input Output Statements)
where format specifier is a character string also known as
control string which contains conversion specifications (%d, %f,
%c etc.) describing the characteristics
MS-PowerPoint of the input data.

Here, arg1, arg2, ------ argn are the variables in which the
values are stored.
By: Every variable is preceded by an address
Ganesh Kumar Sethi
operator (&) except in Professor
Assistant case of string.
MM Modi College
For example if rollno is int
Patiala and percent is float then scanf() is
written like:
scanf(“%d %f”, &rollno, &percent);
Formatted Input –scanf()
Programming In C
Part-5
(Input Output Statements)

MS-PowerPoint

By:
Ganesh Kumar Sethi
Assistant Professor
MM Modi College
Patiala
Formatted Output –printf()
Programming In C
This function can be used to output any combination of
Part-5
numerical values, single
(Inputcharacters and strings.
Output Statements)
It is similar to the input function scanf( ), except that its
purpose is to display data on computer screen.
MS-PowerPoint

By:
Ganesh Kumar Sethi
Assistant Professor
MM Modi College
Patiala

Ganesh K Sethi
Formatted Output –printf()
Its syntax is Programming In C
printf(“format specifiers”, Part-5arg1, arg2, ……..,argn);
where format (Input Output
specifier Statements)
also known as control string
refers to a string that contains formatting information. Format
specifier tells how many arguments follow and what their types
are. The arguments arg1, arg2, arg3, ----argn are the variables
MS-PowerPoint
whose values are formatted and printed according to the
specifications of the control string.
By:
Note that arguments in printf
Ganesh Kumar Sethi are not preceded by ‘&’ operator.
Assistant Professor
All the conversion characters
MM Modi College used within the format specifier of
Patiala
scanf( ) function are also valid for printf( ) function.
Formatted Input/Output -Example
main ( ) Programming In C
{ Part-5
int a ; (Input Output Statements)
float b ;
printf (“Enter values of a & b :”)
scanf (“%d % f “, &a, MS-PowerPoint
&b) ;
printf(“\n Value of a = %d \n Value of b = %f “, a, b) ;
} By:
Ganesh Kumar Sethi
Assistant Professor
MM Modi College
Enter Patiala
values of a & b: 10 20.4
Value of a = 10
Value of b = 20.4
Character Input/Output -Example
main ( ) Programming In C
{ Part-5
int a ; (Input Output Statements)
float b ;
printf (“Enter values of a & b :”)
scanf (“%d % f “,MS-PowerPoint
&a, &b) ;
printf(“\n Value of a = %d \n Value of b = %f “, a,
b) ; By:

} Youtube.com/c/GaneshSethi
Ganesh Kumar Sethi
Assistant Professor
MM Modi College
EnterPatiala
values of a & b: 10 20.4
Value of a = 10
Value of b = 20.4

You might also like