INPUT OUTPUT
FUNCTIONS IN C
PROGRAMMING
COMPUTER
PROGRAMMING AND
UTILIZATION
PREPARED BY:
Introduction
 Reading input data, processing it and
displaying the results are the three tasks of
any program.
 There are two ways to accept the data.
 In one method, a data value is assigned to the
variable with an assignment statement.
 Another way of accepting the data is with
functions.
Formatted function
• With the formatted functions, the input or
output is formatted as per our
requirement.
• All the I/O function are defined as
stdio.hstdio.h header file.
• Header file should be included in the
program at the beginning.
Input and Output Functions
Formatted Functions Unformatted Functions
printf()
scanf()
getch() putch()
getche() putchar()
getchar() puts()
gets()
Formatted Functions
It read and write all
types of data values.
Require format string
to produce
formatted result
Returns value after
execution
Unformatted Functions
Works only with
character data type
Do not require format
conversion for
formatting data type
printf( ) function
This function displays output with specified
format
It requires format conversion symbol or format
string and variables names to the print the data
The list of variables are specified in the printf()
statement
The values of the variables are printed as the
sequence mentioned in printf()
The format string symbol and variable name
should be the same in number and type
printf( ) function
Syntax
printf(“control string”, varialbe1,
variable2,..., variableN);
 The control string specifies the field format
such as %d, %s, %g, %f and variables as
taken by the programmer
void main()
{
int NumInt = 2;
float NumFloat=2.2;
char LetterCh = ‘C’;
printf(“%d %f %c”, NumInt, NumFloat, LetterCh);
}
Output :
2 2.2000 C
void main()
{
int NumInt = 65;
clrscr();
printf(“%c %d”, NumInt, NumInt);
}
Output :
A 65
void main()
{
int NumInt = 7;
clrscr();
printf(“%f”, NumInt);
return 0;
}
Output :
Error Message : “Floating points formats not linked”
void main()
{
int NumInt = 7;
clrscr();
printf(“%f”, NumInt);
return 0;
}
Output :
Error Message : “Floating points formats not linked”
 All the format specification starts with
% and a format specification letter
after this symbol.
 It indicates the type of data and its
format.
 If the format string does not match
with the corresponding variable, the
result will not be correct.
 Along with format specification use
 Flags
 Width
 Precision
 Flag
 It is used for output justification,
numeric signs, decimal points, trailing
zeros.
 The flag (-) justifies the result. If it is
not given the default result is right
justification.
 Width
 It sets the minimum field width for an
output value.
 Width can be specified through a
decimal point or using an asterisk ‘*’.
void main()
{
clrscr();
printf(“n%.2s”,”abcdef”);
printf(“n%.3s”,”abcdef”);
printf(“n%.4s”,”abcdef”);
}
OUTPUT
ab
abc
abcd
void main()
{
int x=55, y=33;
clrscr();
printf(“n %3d”, x – y);
printf(“n %6d”, x – y);
}
OUTPUT
22
22
void main()
{
int x=55, y=33;
clrscr();
printf(“n %*d”, 15, x – y);
printf(“n %*d”, 5,x – y);
}
OUTPUT
22
22
void main()
{
float g=123.456789;
clrscr();
printf(“n %.1f”, g);
printf(“n %.2f”, g);
printf(“n %.3f”, g);
printf(“n %.4f”, g);
}
OUTPUT
123.5
123.46
123.457
123.4568
scanf() function
 scanf() function reads all the types of data
values.
 It is used for runtime assignment of
variables.
 The scanf() statement also requires
conversion symbol to identify the data to be
read during the execution of the program.
 The scanf() stops functioning when some
input entered does not match format string.
scanf() function
Syntax :
scanf(“%d %f %c”, &a, &b, &c);
 Scanf statement requires ‘&’ operator called
address operator
 The address operator prints the memory
location of the variable
 scanf() statement the role of ‘&’ operator is to
indicate the memory location of the variable,
so that the value read would be placed at that
location.
scanf() function
 The scanf() function statement also
return values. The return value is
exactly equal to the number of values
correctly read.
 If the read value is convertible to the
given format, conversion is made.
void main()
{
int a;
clrscr();
printf(“Enter value of ‘A’ : “);
scanf(“%c”, &a);
printf(“A : %c”,a);
}
OUTPUT
Enter value of ‘A’ : 8
A : 8
void main()
{
char a;
clrscr();
printf(“Enter value of ‘A’ : “);
scanf(“%d”, &a);
printf(“A : %d”,a);
}
OUTPUT
Enter value of ‘A’ : 255
A : 255
Enter value of ‘A’ : 256
A : 256
Data Type Format string
Integer Short Integer %d or %i
Short unsigned %u
Long signed %ld
Long unsigned %lu
Unsigned hexadecimal %u
Unsigned octal %o
Real Floating %f or %g
Double Floating %lf
Character Signed Character %c
Unsigned Character %c
String %s
Octal number %o
Displays Hexa
decimal number in
lowercase
%hx
Displays Hexa
decimal number in
lowercase
%p
void main()
{
int a = 1, b = a + 1, c = b + 1, d = c + 1;
clrscr();
printf(“t A = %dnB = %d ’C = %d’”,a,b,c);
printf(“nb***D = %d**”,d);
printf(“n*************”);
printf(“rA = %d B = %d”, a, b);
}
OUTPUT
A = 1
B = 2 ‘C = 3’
***D=4**
A = 1 B = 2******
Unformatted Functions
C has three types of I/O functions
 Character I/O
 String I/O
 File I/O
 Character I/O
getchar ( )
 This function reads a character type
data from standard input.
 It reads one character at a time till
the user presses the enter key.
 Syntax
VariableName = getchar();
 Example
char c;
c = getchar();
putchar( )
 This function prints one character on the
screen at a time, read by the standard
input.
 Syntax
 puncher(variableName)
 Example
char c = ‘C’;
putchar(c);
getch() and getche()
 These functions read any
alphanumeric character from the
standard input device.
 The character entered is not displayed
by the getch() function.
 The character entered is displayed by
the getche() function.
 Exampe
 ch = getch();
 ch = getche();
gets()
 This function is used for accepting any string through stdin keyword until
enter key is pressed.
 The header file stdio.h is needed for implementing the above function.
 Syntax
char str[length of string in number];
gets(str);
void main()
{
char ch[30];
clrscr();
printf(“Enter the string : “);
gets();
printf(“n Entered string : %s”, ch);
}
puts()
 This function prints the string or character array.
 It is opposite to gets()
char str[length of string in number];
gets(str);
puts(str);

CPU INPUT OUTPUT

  • 1.
    INPUT OUTPUT FUNCTIONS INC PROGRAMMING COMPUTER PROGRAMMING AND UTILIZATION
  • 2.
  • 3.
    Introduction  Reading inputdata, processing it and displaying the results are the three tasks of any program.  There are two ways to accept the data.  In one method, a data value is assigned to the variable with an assignment statement.  Another way of accepting the data is with functions.
  • 4.
    Formatted function • Withthe formatted functions, the input or output is formatted as per our requirement. • All the I/O function are defined as stdio.hstdio.h header file. • Header file should be included in the program at the beginning.
  • 5.
    Input and OutputFunctions Formatted Functions Unformatted Functions printf() scanf() getch() putch() getche() putchar() getchar() puts() gets()
  • 6.
    Formatted Functions It readand write all types of data values. Require format string to produce formatted result Returns value after execution Unformatted Functions Works only with character data type Do not require format conversion for formatting data type
  • 7.
    printf( ) function Thisfunction displays output with specified format It requires format conversion symbol or format string and variables names to the print the data The list of variables are specified in the printf() statement The values of the variables are printed as the sequence mentioned in printf() The format string symbol and variable name should be the same in number and type
  • 8.
    printf( ) function Syntax printf(“controlstring”, varialbe1, variable2,..., variableN);  The control string specifies the field format such as %d, %s, %g, %f and variables as taken by the programmer
  • 9.
    void main() { int NumInt= 2; float NumFloat=2.2; char LetterCh = ‘C’; printf(“%d %f %c”, NumInt, NumFloat, LetterCh); } Output : 2 2.2000 C
  • 10.
    void main() { int NumInt= 65; clrscr(); printf(“%c %d”, NumInt, NumInt); } Output : A 65
  • 11.
    void main() { int NumInt= 7; clrscr(); printf(“%f”, NumInt); return 0; } Output : Error Message : “Floating points formats not linked”
  • 12.
    void main() { int NumInt= 7; clrscr(); printf(“%f”, NumInt); return 0; } Output : Error Message : “Floating points formats not linked”
  • 13.
     All theformat specification starts with % and a format specification letter after this symbol.  It indicates the type of data and its format.  If the format string does not match with the corresponding variable, the result will not be correct.  Along with format specification use  Flags  Width  Precision
  • 14.
     Flag  Itis used for output justification, numeric signs, decimal points, trailing zeros.  The flag (-) justifies the result. If it is not given the default result is right justification.  Width  It sets the minimum field width for an output value.  Width can be specified through a decimal point or using an asterisk ‘*’.
  • 15.
  • 16.
    void main() { int x=55,y=33; clrscr(); printf(“n %3d”, x – y); printf(“n %6d”, x – y); } OUTPUT 22 22
  • 17.
    void main() { int x=55,y=33; clrscr(); printf(“n %*d”, 15, x – y); printf(“n %*d”, 5,x – y); } OUTPUT 22 22
  • 18.
    void main() { float g=123.456789; clrscr(); printf(“n%.1f”, g); printf(“n %.2f”, g); printf(“n %.3f”, g); printf(“n %.4f”, g); } OUTPUT 123.5 123.46 123.457 123.4568
  • 19.
    scanf() function  scanf()function reads all the types of data values.  It is used for runtime assignment of variables.  The scanf() statement also requires conversion symbol to identify the data to be read during the execution of the program.  The scanf() stops functioning when some input entered does not match format string.
  • 20.
    scanf() function Syntax : scanf(“%d%f %c”, &a, &b, &c);  Scanf statement requires ‘&’ operator called address operator  The address operator prints the memory location of the variable  scanf() statement the role of ‘&’ operator is to indicate the memory location of the variable, so that the value read would be placed at that location.
  • 21.
    scanf() function  Thescanf() function statement also return values. The return value is exactly equal to the number of values correctly read.  If the read value is convertible to the given format, conversion is made.
  • 22.
    void main() { int a; clrscr(); printf(“Entervalue of ‘A’ : “); scanf(“%c”, &a); printf(“A : %c”,a); } OUTPUT Enter value of ‘A’ : 8 A : 8
  • 23.
    void main() { char a; clrscr(); printf(“Entervalue of ‘A’ : “); scanf(“%d”, &a); printf(“A : %d”,a); } OUTPUT Enter value of ‘A’ : 255 A : 255 Enter value of ‘A’ : 256 A : 256
  • 24.
    Data Type Formatstring Integer Short Integer %d or %i Short unsigned %u Long signed %ld Long unsigned %lu Unsigned hexadecimal %u Unsigned octal %o Real Floating %f or %g Double Floating %lf Character Signed Character %c Unsigned Character %c String %s Octal number %o Displays Hexa decimal number in lowercase %hx Displays Hexa decimal number in lowercase %p
  • 25.
    void main() { int a= 1, b = a + 1, c = b + 1, d = c + 1; clrscr(); printf(“t A = %dnB = %d ’C = %d’”,a,b,c); printf(“nb***D = %d**”,d); printf(“n*************”); printf(“rA = %d B = %d”, a, b); } OUTPUT A = 1 B = 2 ‘C = 3’ ***D=4** A = 1 B = 2******
  • 26.
    Unformatted Functions C hasthree types of I/O functions  Character I/O  String I/O  File I/O  Character I/O
  • 27.
    getchar ( ) This function reads a character type data from standard input.  It reads one character at a time till the user presses the enter key.  Syntax VariableName = getchar();  Example char c; c = getchar();
  • 28.
    putchar( )  Thisfunction prints one character on the screen at a time, read by the standard input.  Syntax  puncher(variableName)  Example char c = ‘C’; putchar(c);
  • 29.
    getch() and getche() These functions read any alphanumeric character from the standard input device.  The character entered is not displayed by the getch() function.  The character entered is displayed by the getche() function.  Exampe  ch = getch();  ch = getche();
  • 30.
    gets()  This functionis used for accepting any string through stdin keyword until enter key is pressed.  The header file stdio.h is needed for implementing the above function.  Syntax char str[length of string in number]; gets(str); void main() { char ch[30]; clrscr(); printf(“Enter the string : “); gets(); printf(“n Entered string : %s”, ch); }
  • 31.
    puts()  This functionprints the string or character array.  It is opposite to gets() char str[length of string in number]; gets(str); puts(str);