C provides several functions that give different levels of input and output capability.
The input and output functions in C are built around the concept of a set of standard data streams. These standard data streams or files are opened by the operating system and are available to every C and assembler program for use without having to open or close the files.
The input/output functions are of two kinds:
nonformatted and formatted functions.
UNFORMATTED INPUT AND OUTPUT FUNCTIONS
Non-formatted input and output can be carried out by standard input-output library functions in C. For input/output functions, it reads/prints a single character from/on the console.
The getchar() and putchar() Functions
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.
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.
Check the following example −
#include <stdio.h>
int main( ) {
int c;
printf( "Enter a value :");
c = getchar( );
printf( "\nYou entered: ");
putchar( c );
return 0;
}
The gets() and puts() Functions
The char gets(char *s) function reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF (End of File).
The int puts(const char *s) function writes the string 's' and 'a' trailing newline to stdout.
#include <stdio.h>
int main( ) {
char str[100];
printf( "Enter a value :");
gets( str );
printf( "\nYou entered:");
puts( str );
return 0;
}
FORMATTED INPUT AND OUTPUT FUNCTIONS
If input and output is required in a specified format the standard library functions scanf() and printf() are used.
The scanf() and printf() Functions
• The general form of printf() function is
printf(“control_string”,variable1,variable2,...);
The control string is important because it specifies the type of each variable in the list and how the user wants it printed.
The control string is also called the format string.
The format string in printf(), enclosed in quotation marks, has three types of objects:
Ordinary characters: these are copied to output
Conversionspecifier field: denoted by % containing the codes and by optional modifiers such as width, precision, flag, and size
Control code: optional control characters such as \n,\b, and \t
The format string in printf() has the following general form:
Printf(“<control code><character string><%conversion specifier> <control code>”);
• The general form of scanf function is
scanf(“control_string”,variable1_address, variable2_address,...);
The scanf() function should have the addresses of the variables rather than just their values. This means that simple variables have to be passed with a proceeding &.
scanf(“%d%d%d%c”, &a, &b, &c, &x);
Ex:
#include <stdio.h>
int main( ) {
char str[100];
int i;
printf( "Enter a value :");
scanf("%s %d", str, &i);
printf( "\nYou entered: %s %d ", str, i);
return 0;
}
0 comments:
Post a Comment