0% found this document useful (0 votes)
61 views20 pages

C I/O Functions for Beginners

The document discusses several input/output functions in C including getchar(), putchar(), scanf(), and printf(). It explains that getchar() reads a single character from keyboard input and putchar() writes a single character to output. It also describes how scanf() can be used to read different data types from input using format specifiers and how arguments are passed by reference. Finally, it provides examples of using scanf() to read strings and integers.

Uploaded by

namirabushra92
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)
61 views20 pages

C I/O Functions for Beginners

The document discusses several input/output functions in C including getchar(), putchar(), scanf(), and printf(). It explains that getchar() reads a single character from keyboard input and putchar() writes a single character to output. It also describes how scanf() can be used to read different data types from input using format specifiers and how arguments are passed by reference. Finally, it provides examples of using scanf() to read strings and integers.

Uploaded by

namirabushra92
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

Programming in C

Input –Output Function

Ashis Talukder, PhD


Associate Professor
Department of MIS, DU

Dr. Ashis Talukder, MIS, DU 1


I/O functions in C
• An input/output function can be accessed from anywhere within a program simply by
writing the function name, followed by a list of arguments enclosed in parentheses.
• The arguments represent data items that are sent to the function. Some input/output
functions do not require arguments, though the empty parentheses must still appear.
• we will make use of six of these functions:
• getchar,
• putchar,
• scanf,
• printf,
• gets and
• puts.

Dr. Ashis Talukder, MIS, DU 2


I/O Functions
• The program begins with the
#include <stdio.h>
preprocessor statement #include
main ( )
<stdio. h>.
{ • This statement causes the contents
char c,d; / * declarations * I of the header file stdio. h to be
float x,y; included within the program.
int i,j,k ; • The header file supplies required
c = getchar(); /* character input */ information to the library functions
scanf ( " % f " , &x) ; /* f l o a t i n g ‐ p o i n t input */ scanf and printf .
scanf ( "%d %d", &i, &j); I * integer input * /
• Following the preprocessor
. . . / * action statements */
statement is the program heading
main( ) and some variable
putchar(d); / * character output */
declarations.
printf("%3d %7.4f", k, y ) ; / * numerical output */ • Several input/output statements
} are shown in the skeletal outline
that follows the declarations.
Dr. Ashis Talukder, MIS, DU 3
I/O Functions
• the assignment statement c =
#include <stdio.h> getchar ( ) ; causes a single
main ( ) character to be entered from the
{ keyboard and assigned to the
char c,d; / * declarations * I character variable c.
float x,y;
int i,j,k ; • The first reference to scanf causes
c = getchar(); /* character input */ a floating‐point value to be entered
scanf ( " % f " , &x) ; /* f l o a t i n g ‐ p o i n t input */ from the keyboard and assigned to
scanf ( "%d %d", &i,&j); I * integer input * / the floating‐point variable x,
whereas the second reference to
. . . / * action statements */ scanf causes two decimal integer
quantities to be entered from the
putchar(d); / * character output */
keyboard and assigned to the
printf("%3d %7.4f", k, y ) ; / * numerical output */
}
integer variables i and j,
respectively.
Dr. Ashis Talukder, MIS, DU 4
I/O Functions
• soutputs value of d, returns no
#include <stdio.h> value, so we use parameter.
main ( )
{ • printf() prints values of k and y.
char c,d; / * declarations * I
float x,y;
int i,j,k ;
c = getchar(); /* character input */
scanf ( " % f " , &x) ; /* f l o a t i n g ‐ p o i n t input */
scanf ( "%d %d", &i,&j); I * integer input * /

. . . / * action statements */

putchar(d); / * character output */


printf("%3d %7.4f", k, y ) ; / * numerical output */
}

Dr. Ashis Talukder, MIS, DU 5


getchar()
• Single characters can be entered into the computer using the C library function
getchar
• The getchar function is a part of the standard C I/O library. It returns a single
character from a standard input device (typically a keyboard).
• The function does not require any arguments, though a pair of empty
parentheses must follow the word getchar.
• In general terms, a function reference would be written as
character variable = getchar ( ) ;

char c;
. . . . .
c = getchar();

Dr. Ashis Talukder, MIS, DU 6


putchar()
• Single characters can be displayed (Le, written out of the computer) using
the C library function putchar.
• This function is complementary to the character input function getchar,
• The putchar function, like getchar, is a part of the standard C I/O library.
• In general, a hction reference would be written as
putchar ( character variable)
char c;
. . . . .
putchar(c);

Dr. Ashis Talukder, MIS, DU 7


scanf()
• Input data can be entered into the computer from a standard input
device by means of the C library function scanf.
• This function can be used to enter any combination of numerical
values, single characters and strings.
• The function returns the number of data items that have been
entered successfully.
• In general terms, the scanf function is written as
scanf(contro1 string, argl, arg2, . . . , argn)

Dr. Ashis Talukder, MIS, DU 8


scanf()
scanf(contro1 string, argl, arg2, . . . , argn)

• The control string consists of individual groups of characters, with one


character group for each input data item.
• Each character group must begin with a percent sign (%). In its simplest
form, a single character group will consist of the percent sign, followed by a
conversion character which indicates the type of the corresponding data
item
• Within the control string, multiple character groups can be contiguous, or
they can be separated by whitespace characters (i.e., blank spaces, tabs or
newline characters).

Dr. Ashis Talukder, MIS, DU 9


scanf()

Dr. Ashis Talukder, MIS, DU 10


scanf()
scanf(contro1 string, argl, arg2, . . . , argn)

• Each variable name must be preceded by an ampersand.


• The arguments are actually pointers that indicate where the data items are
stored in the computer's memory
• However, array names should not begin with an ampersand.

scanf ( "%d %d", &i,&j); I * integer input * /


scanf ( "%d %s", array[1],string); I * array/string input * /

Dr. Ashis Talukder, MIS, DU 11


scanf()
• The consecutive non‐whitespace characters that define a data item
collectively define afield. It is possible to limit the number of such
characters by specifying a maximum field width for that data item.
• To do so, an unsigned integer indicating the field width is placed
within the control string, between the percent sign (%) and the
conversion character.

scanf( "%3d %3d %3d" , &a, &by &c) ;

Dr. Ashis Talukder, MIS, DU 12


scanf()
#include <stdio.h>
main( )
{
char item[20];
int partno;
float cost;
. . . . .
scanf("%s %d %f", item, &partno, &cost);
. . . . .
}

Dr. Ashis Talukder, MIS, DU 13


scanf()
#include <stdio.h>
main ( )
{
char c1 , c2, c3;
scanf("%c%c%c", &c1, &c2, &c3);
// c1 = a, c2 = <blankspace>, c3 = b
}

scanf(" %c%1s%1s", &c1, &c2, &c3) // c1 = a, c2 = b, c3 = c

Dr. Ashis Talukder, MIS, DU 14


printf()
• the printf function moves data from the computer’s memory to the
standard output device, whereas the scanf function enters data from
the standard input device and stores it in the computer’s memory.
• In general terms, the p r i n t f function is written as

printf(control string, arg7, arg2, . .. , argn);

Dr. Ashis Talukder, MIS, DU 15


printf()

Dr. Ashis Talukder, MIS, DU 16


printf()
#include <stdio.h>
main ( )
{
char item[20];
int partno;
float cost;
printf( "%s %d %f' I , item, partno, cost ) ;
...
}
Dr. Ashis Talukder, MIS, DU 17
printf()
• A floating‐point number will be rounded if it must be shortened to
conform to a precision specification.
#include <stdio.h>
main() /* display a floating-point number with
several different precisions */
{
float x = 123.456;

printf("%7f %7.3f %7.1f\n\n", x, x, x ) ;


}
123.456000 123.456 123.5

Dr. Ashis Talukder, MIS, DU 18


THE gets AND puts FUNCTIONS
#include <stdio.h>
main( ) /* read and write a line of text * /
{
char line[80] ;

gets(1ine);
puts(1ine);
}

Dr. Ashis Talukder, MIS, DU 19


I/O Functions in C
Lets us see some practical program…

Thank You
Dr. Ashis Talukder, MIS, DU 20

You might also like