INPUT AND OUTPUT FUNCTION

ARTHEESWARI . S
4 min readSep 29, 2022

There are a number of I/O standard functions in c, based on the data types. The I/O function are classified into two types (a). formatted functions (b).unformatted functions. various functions of these categories are listed

Types of Input and Output Functions in C

In C, input refers to providing it with some information or data to be utilised in the program. On the other hand, output refers to writing the data and information on any printer file and displaying it on the screen. A standard library is available in the C language for reading any input and generating the output to be displayed on the console. There are mainly two types of these functions available in the C language:

Formatted

The formatted functions basically present or accept the available data (input) in a specific format. The standard library in C contains various functions for the input-output operations. The scanf() and printf() out of these functions help a programmer format the functions in their desired format. The program can use these functions for reading any form of data, like a real number, an integer, a character, and many more.

Unformatted

The unformatted functions are not capable of controlling the format that is involved in writing and reading the available data. Thus, these functions constitute the most basic forms of output and input. The supply of input or the display of output isn’t allowed in the user format — thus, we call these functions unformatted functions for input and output.

The unformatted input-output functions further have two categories:

  • The character functions
  • The string functions

Formatted Functions:

INPUT FUNCTION :

scanf() in C

  • scanf is the input function that gets the formatted input from the file stdin that is the keyboard. As mentioned earlier, C treats the keyboard input as a file. scanf is a built-in function that comes with all C compilers. Its header file is stdio.h.
  • The scanf() reads all types of data values given by the user and these values are assigned to the variables. It requires the conversion specification (such as %s and %d ) to identify the type of data to be read during the program execution.
  • You need to pass the address of the variable to the scanf function along with other arguments so that the read values can be assigned to the correct destination.

scanf() Syntax

  • int scanf(const char *format, arg1, arg2,….);
  • The first argument is the format string (conversion specification) and followed by a list of variable arguments of pointer types ie., the address of variables that need to be passed.

scanf() Example

int a;

double b;

scanf(“%d\n”, &a);

scanf(“%lf\n”, &b);

  • scanf() reads the characters from standard input and interprets them according to the format string specification. It stops reading when it encounters a space or when some input fails to match with the conversion specification.

OUTPUT FUNCTION:

printf() in C programming

  • The printf() prints all types of data value to the standard output(typically the screen). It requires a conversion symbol and variable name to print the data. The printf() is part of every C compiler. Its header file is stdio.h.

printf() Syntax:

  • int printf( const char *format, arg1, agr2,….);
  • The first argument is the format string which is composed of ordinary characters and conversion specification. This is followed by zero or more optional arguments.

printf() Example:

printf(“welcome”);

printf(“The result is %d “,a);

  • Usually the format string is enclosed with the double quotation marks in both scanf() and printf().
  • scanf printf Example
  • Let see the example program using scanf() and printf() functions.

#include<stdio.h>

int main()

{

int a;

printf(“Enter the value of a = “);

scanf(“%d”,&a);

printf(“\nThe value of a = %d”,a);

return(0);

}

Output:

Enter the value of a = 10 The value of a = 10

Unformatted I/O Functions

getchar() in C Programming

The getchar() reads character type data from input file stdin. It reads one character at a time till the user pressses the enter key. The getchar() returns a character type. It reads next character in the input file.

getchar() — Syntax

variable-name = getchar();

getchar() — Example

char ch;
ch = getchar();

This places the next character into the variable ch. It is a low level function.

putchar() in C Programming

The putchar() writes the character type data to the output file stdout(screen). It is used to print one character at a time.

putchar() — Syntax

putchar( variable-name);

putchar() — Example

char ch = 'C';
putgachar(ch);

getchar putchar example

#include<stdio.h>
int main()
{
char a;
a = getchar();
putchar(a);
return(0);
}

The most important point is that the both getchar() and putchar() could be implemented as macros, rather than function. This means that it might not be possible to use functions as parameters inside them:

putchar( function() );

gets() in C Programming

The gets() is used to read string from standard input file stdin and places it into some buffer which the programmer must provide. The gets() stops reading when an enter key is pressed or EOF is reached.

gets — Syntax

char *gets(char *str)

puts() in C Programming

The puts() is used to send the string to the output file stdout, until it finds a NULL end of string marker. The NULL is not written, instead the newline character is written to the output stdout.

puts — Syntax

int puts(const char *str)

puts() returns an integer value, whose value is only guaranteed if there is an error.

gets puts Example C Program

#include<stdio.h>
int main()
{
char ch[20];
printf("Enter the text: ");
gets(ch);
puts(ch);
return(0);
}

Output

Enter the text: welcome
welcome

--

--