Input and Output Function in C

Input is meant to provide some data to the program which can be used whereas Output means to display the data or result on the screen or write data to the file or any other external storage.

C programming language provides many built-in functions to read any data and to display data onto the screen when there is need to output the result.

Before moving further, please refer to the topics below to understand the concepts better.

All the built-in functions supported by C for input and output are present in the header file

Input and output functions discussed are:

scanf() and printf() functions:

These two functions are present in a standard input-output header file, named stdio.h which contains definition of the function printf() and scanf(), which can be used directly in our program for displaying and accepting the input respectively.

Sample Program:

#include <stdio.h>

int main()
{
   int num1, num2;
   printf("\n Enter the first number\n");
   scanf("\n %d\n", &num1);

   printf("\n Enter the second number\n");
   scanf("\n %d\n", &num2);
  
   printf ("\n The addition is %d\n", num1 + num2);
   return 0;
}

Output:


Enter the first number
10

Enter the second number
14

The addition is 24

Once the code is compiled and run, it will ask for two numbers using scanf() and output the results using printf().

Format Specifier:

The purpose of the format specifier used in scanf() and printf() function is to inform the compiler what type of data to expect as an input and produce as an output.

There are different types of format specifier depending upon the type of data. Some of the common specifers used are:

Format String Meaning
%dScan to print integer as signed decimal digit
%fScan or print a floating point number
%cTo scan or print a number
%sScan or print a character string. The scanning ends at whitespace

Note:

  • We can limit the number of digits or characters that can be input or output, by adding a number with the format specifier, like “%1d” or “%3s”.
  • The first one means a single numeric digit and the second one above means 3 characters.
  • Hence if we try to input “1234”, while scanf() has “%1d”, it will take only 4 as input . The same is the case for output.
  • printf() function returns the number of characters printed by it, and scanf() returns the number of characters read by it.

getchar() and putchar() functions

The getchar() function reads an unsigned character from the terminal. It reads a single character at a time. It can be used in a loop to read more than one character.

The putchar() function displays the character passed to it on the screen and returns the character read by getchar(). Even putchar() can be used in a loop.

These two functions are present in a standard input-output header file, named stdio.h

Sample Program:

#include <stdio.h>

int main()
{
   char c;
   printf("\n Enter the character\n");
   c = getchar();
   /* Display the character read */
   printf("\n Read character is\n");
   putchar(c);
   return 0;
}

Output:


Enter the character
k

Read character is
k

fgets() and fputs() functions

It reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first.

The C library function int fputs() writes a string to the specified stream up to but not including the null character.

These two functions are present in a standard input-output header file, named stdio.h and stdlib.h is used for malloc() function.

Syntax:

char *fgets (char *str, int length, FILE *stream)

where:

  • str is a pointer to an array of chars where the string is copied.
  • length is the maximum number of characters to be copied into str, including the NULL terminated string.
  • stream is a pointer to a FILE object that identifies an input stream. It can be stdin or a file. It is like the source from which the data will be read and copied into str.
  • It returns a pointer to string buffer if anything is written to it, or a null pointer if an error occurred or if the file position indicator was at the end of the file.

Sample Program:

#include <stdio.h>
#include<stdlib.h>

int main()
{
   char c;
   FILE *fp;
   fp = fopen ("file.txt", "w+");
   
   printf ("\n Enter the string\n");
   char *str= malloc (10);
   fgets (str,10, stdin);
   
   /* Put the string on file */
   fputs (str,fp);
  
   return 0;
}

The entered string will be written to the file named file.txt.



Categories: C Language

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: