--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD-IT)
Contact No : 7600044051
E-Mail : rupareleducation@gmail.com
I/O Formatting Functions
Printf() function :
·
Printf() function is use to display something on the console or to
display the value of some variable on the console The general syntax for
printf() function is as follows
printf(<”format
string”>,<list of variables>);
·
To print some message on the screen
·
Printf(“God is great”);
·
This will print message “God is great” on the screen or console.
·
To print the value of some variable on the screen
int a=10;
printf(“%d”,a);
Here %d is format string
to print some integer value and a is the integer variable whose valuewill be
printed by printf() function. This will print value of a “10” on the screen.
·
To print multiple variable’s value one can use printf() function
in following way.
Int p=1000,r=10,n=5;
Printf(“amount=%d rate=%d year=%d”,p,r,n);
This will print
“amount=1000 rate=10 year=5” on the screen
Scanf() Function :
·
Scanf() function is use to read data from keyboard and to store
that data in the variables. The general syntax for scanf() function is as
follows.
Scanf(“Format String”,&variable);
·
Here format string is used to define which type of data it
is taking as input this format string can be %c for character, %d
for integer variable and %f for float variable.
·
Where as variable the name of memory location or name of the
variable and & sign is an operator that tells the compiler the address of
the variable where we want to store the value.
·
One can take multiple input of variable with single scanf()
function but it is recommended that there should be one variable input with one
scanf() function.
Single character input – the getchar( ) function :
·
Single characters can be entered into the computer using the “C”
library function getchar. The getchar function is a part of the standard “C”
language I/O library.
·
It returns a single character from a standard input device
(typically a keyboard).
·
The function does not required any arguments through a pair of
empty parentheses must follow the word getchar.
·
In general terms, a reference to the getchar function is written
as.
Character variable=getchar();
·
Where character variable refers to some previously declared
character variable.
Single character output – The putchar( ) function:
·
Single character can be displaye using the C library function putchar.
·
The putchar function, like getchar is a part of the standard “C”
language I/O library.
·
It transmits a single character to a standard output device.
·
The character being transmitted will normally be represented as a
character-type variable.
·
It must be expressed as an argument
to the function, enclosed in parentheses following the word putchar.
·
In general a reference to the
putchar function is written as
Putchar (character variable)
Where character
variable refers to some previously declared character
getc() Function
The C library function int
getc(FILE *stream) gets the
next character (an unsigned char) from the specified stream and advances the
position indicator for the stream.
Following is the declaration for getc() function.
int getc(FILE *stream)
stream is the pointer to a FILE object that
identifies the stream on which the operation is to be performed. This function returns
the character read as an unsigned char cast to an int or EOF on end of file or
error.
putc () Function :
The C library function int
putc(int char, FILE *stream) writes
a character (an unsigned char) specified by the argument char to the specified stream and advances
the position indicator for the stream.
Following is the declaration for putc() function.
int putc(int char, FILE *stream)
char is the character to be written. The
character is passed as its int promotion. stream is the pointer to a FILE object that
identifies the stream where the character is to be written. This function returns
the character written as an unsigned char cast to an int or EOF on error.
gets() Function
The C library function char
*gets(char *str) reads a line
from stdin and stores it into the string pointed to by str. It stops when
either the newline character is read or when the end-of-file is reached,
whichever comes first.
Following is the declaration for gets() function.
char *gets(char *str)
str is the pointer to an array of chars where
the C string is stored. This function returns str on success, and NULL on error or when
end of file occurs, while no characters have been read.
puts() Function
The C library function int
puts(const char *str) writes
a string to std out up to but not including the null character. A newline
character is appended to the output.
Following is the declaration for puts() function.
int puts(const char *str)
str is the C string to be written. If successful,
non-negative value is returned. On error, the function returns EOF.
getchar () Function
The C library function int
getchar(void) gets a
character (an unsigned char) from stdin. This is equivalent to getc with stdin as its argument.
Following is the declaration for getchar() function.
int getchar(void)
This function returns the character read as an unsigned char cast
to an int or EOF on end of file or error. The int
getchar(void) function reads
the next available character from the screen and returns it as an integer. This
function reads only single character at a time. You can use this method in the
loop in case you want to read more than one character from the screen.
putchar () function
The C library function int
putchar(int char) writes a
character (an unsigned char) specified by the argument char to std out.
Following is the declaration for putchar() function.
int putchar(int char)
char is the character to be written. This is
passed as its int promotion.
This function returns the character written as an unsigned char
cast to an int or EOF on error. The int
putchar(int c) function puts
the passed character on the screen and returns the same character. This
function puts only single character at a time. You can use this method in the
loop in case you want to display more than one character on the screen.
Ungetc() function
The C library function int
ungetc(int char, FILE *stream) pushes
the character char (an
unsigned char) onto the
specified stream so that the this is available for the
next read operation.
Following is the declaration for ungetc() function.
int ungetc(int char, FILE *stream)
char is the character to be put back. This is
passed as its int promotion. stream
is the pointer to a FILE object that identifies an input stream. If successful, it
returns the character that was pushed back otherwise, EOF is returned and the
stream remains unchanged.
Best Of Luck
Please Share and give your valuable comment....