You are on page 1of 23

Managing Input / output

functions
Managing Input / output functions
 In ‘C’ language I/O functions are categorized into following two
types

Input/output function

Unformatted I/O Formatted I/O

getchar(); putchar();
gets(); scanf(); printf();
puts();
getch();
getche();
scanf();
 The scanf() function is an input function.
 It used to read the mixed type of data from the
keyboard.
 You can read integer, float and character data by using
its control codes or format codes.
Syntax:
scanf(“control string”,&v1,&v2…);
Example: scanf(“%d”,&n);

 The control string %d specifies that an integer value is


to be read from the terminal (keyboard).
printf():
 printf() function for printing captions and numerical results the
general form of printf statement is….
Syntax:
printf(“controle string”,arg1,arg2…);
control string consists of three types of items:

 Characters that will be printed on the screen as they appear.


 Format specifications that define the output format for display of
each item.
 Escape sequence character such as \n,\t….. the control string
indicates how many arguments follow and what their types are the
argument arg1,arg2… are the variable whose values are formatted
and printed according to the specification of control string.
READING & WRITING USING
SCANF & PRINTF
# include <stdio.h>
main()
{
char c;
printf("Enter single character:");
scanf("%c", &c);
printf("The Entered Character is %c", c);
}
getchar():
 This is an input function.
 It is used for reading a single character from the
keyboard.
 It is buffered function. Buffered functions get the input
from the keyboard and store it in the memory temporarily
until you press the enter key.
 After you pressed the enter key then input move to a
relevant variable.
Syntax: v=getchar();
Example: char a; a=getchar();
gets():
 It is used for read a string from the keyboard.
 It is needed because scanf() function has some limitations while
receiving string of characters
 gets() functions, gets a string from the keyboard and it is
terminated when an Enter key is hit.
 Thus spaces and tabs are perfectly acceptable as part of the input
string
 It is a buffered function it will read a string, when you type the
string from the keyboard and press the enter key from the
keyboard.
 It will mark null character (‘\0’) in the memory at the end of the
string, when you press the enter key.

Syntax: gets(v);
Example: char s[20]; gets(s);
getch():
 This is also an input function.
 This is used to read a single character from the keyboard
like getchar() function.
➢ But getchar() function is a buffered function;
➢ getch() function is a non buffered function.
➢ When type the character data from the keyboard it does
not visible on the screen.
Syntax: v=getch();
getche():
 All are same as getch() function except
that when you type the character data
from the keyboard it will be visible on the
screen.

Syntax: v=getche();
putchar():
 This function is an output function. It is
used to display a single character on the
screen.

Syntax: putchar(v);

Example: char a; getchar(a);


puts():
 This function is an output function .

 It is used to display a string inputted by gets()


function, which you will read in the next on the
screen.

Syntax: puts(v); or puts(“text”);

Example: char a[10]; gets(a);


READING & WRITING USING
GETCHAR & PUTCHAR
# include <stdio.h>
main()
{
char c;
printf("Enter single character:");
c = getchar();
printf("The Entered Character is “) ;
putchar(c);
}
READING & WRITING A STRING
USING SCANF & PRINTF
# include <stdio.h>
main()
{
char name[50];
printf("Enter name:");
scanf("%s", name);
printf("%s", name);
}
OUTPUT
Enter name: Shyam Sundar
Shyam
READING & WRITING A STRING
USING GETS & PUTS
# include <stdio.h>
main()
{
char name[50];
puts("Enter name:");
gets(name);
puts(name);
}
OUTPUT
Enter name: Shyam Charan
Shyam Charan
INPUTTING INTEGER
NUMBER
 The field specification for reading an integer number is
%wd
 The percentage sign (%) indicates that a conversion
specifications
 w is an integer that specifies the filed width of the
number to be read
 d, known as data type character, indicates that the
number to be read is in integer mode
Example: scanf (“%2d %5d”, num1, num2);
Data: 50 31426
 The value 50 is assigned to num1 and 31246 to num2
FORMATTED OUTPUT
 The format specification
%w.p type-specifier
 w is an integer number that specifies the total
number of columns for the output value
 p is another integer number that specifies the
number of digits to the right of the decimal
point
 Type-specifier is the datatype
FORMATTED OUTPUT
• Examples:
– printf(“Programming in C”);
– printf(“ “);
– printf(“\n”);
– printf(“%d”, x);
– printf(“a = 5f\n b= %f”, a, b);
– printf(“sum = %d”, 1234);
– printf(“\n\n”);
• printf never supplies a newline automatically and
therefore multiple printf statements may be used to
build one line of output
• A newline can be introduced by the help of a newline
character ‘\n’
OUTPUT OF INTEGER
NUMBERS
 The format specifications for
printing an integer number is
% wd
 w specifies the minimum field
width for the output
 d specifies that the value to be
printed is an integer
 If the number is greater than
the specified field width, it will
be printed in full, overriding the
minimum specifications
 The number is right-justified in
the given field width
OUTPUT OF REAL NUMBERS
• The format specifications for
printing a real number is
% w.pf
• The integer w indicated the
minimum number of positions
that are to be used for the
display of the value
• The integer p indicates the
number of digits to be displayed
after the decimal point
(precision)
• The value is rounded to p
decimal places and printed right-
justified in the field of w
columns.
END

You might also like