Non-Formatted I/O in C

The functions which fall in this category just provide bare bone functionality to read and write a character or string from or to the standard input or output respectively. There are four main functions within this category.

gets()
gets reads characters from keyboard and stores them as a string into the argument until a newline character (‘\n’) is reached. The ending newline character (‘\n’) is not included in the string. A null character (‘\0’) is automatically appended after the last character copied to the argument to signal the end of the string. The general syntax of gets() function is as follows:

char * gets ( char * argument );

where argument is a pointer to an array of chars where the C string is stored. Program 1 shows gets() function in action followed by screenshot of the output.

#include <stdio.h>
void main()
{
char string [256];
printf (“Insert your full address: “);
gets (string);
printf (“Your address is: %s\n”,string);
}

Program 1 : Program shows gets() function in action.

Screenshot from 2020-07-12 17-51-23

puts()
puts writes characters to screen which are stored in some string (argument) until a null character (‘\0’) is reached. The function begins copying from the address specified (argument) until it reaches the terminating null character (‘\0’) which is not copied to screen. The general syntax of puts() function is as follows:

int puts ( char * argument );

where argument is a pointer to an array of chars where the C string is stored. Program 2 shows puts() function in action followed by screenshot of the output.

#include <stdio.h>
void main()
{
char string [256];
puts (“Insert your full address: “);
gets (string);
puts (“Your address is”);
puts (string);
}

Program : Program shows puts() function in action.

Screenshot from 2020-07-12 17-59-16

putchar()
putchar() function writes a single character to the current position in the standard output (screen). The general syntax of putchar() function is as follows:

int putchar (char character );

where character is the character to be outputted. The following program displays W on screen.

#include <stdio.h>
void main ()
{
char c=’W’;
putchar (c);
}

getchar()
getchar() function reads a single character from keyboard. The general syntax of getchar() function is as follows:

char getchar ();

The following program displays what it reads from keyboard on screen.

#include <stdio.h>
void main ()
{
char c;
c = getchar();
putchar (c);
}