Basic Input Output in C - Characters and Formatted IO

Basic Input Output in C - Characters and Formatted IO
In this, you will learn about:

  • Basic Input Output in C - Characters

When we write programs, we generally require some input from user and must provide outputs to the user. C language does not provide any keyword for receiving input or producing output. Instead these things will be done using library functions. Following are some example functions for reading/writing various kind of data in our program.


Reading characters:- The simplest functions to read/write data are getchar() and putchar(). The getchar() function will wait for a character to be typed from keyboard and then return it. The putchar() function will print a character at current position on screen. The prototypes for these functions are: -

int getchar(void);
int putchar(int c);

They work will characters in their ASCII form, so they use integer variables. If there is any error then getchar() will return EOF which is generally equal to -1. Following program will use them: -

Program:
#include <stdio.h>
int main( )
{
char ch;
printf("Enter any character : ");
ch = getchar();
printf("The entered character is: ");
putchar(ch);
return 0;
}

Because the getchar() function is implemented in an not obvious manner in C i.e. it will return a single character only after seeing an ENTER from keyboard, so if user type 3 characters and then hit ENTER, in this case getchar() leaves the other two characters in buffer. Also the first getchar() function will return 2 bytes, 1st byte is the character returned and 2nd byte the ENTER key. This creates annoying situations when there is a sequence of getchar() functions. So some other functions are defined like getch() or getche() in “conio.h” header to facilitate the programmer. So if you run following program which must ask three characters 1 by 1 from user, may not work perfectly if the user enters more characters in first go: -

Program:
#include <stdio.h>
int main() 
{
	char c1, c2, c3;
	printf("Enter any character : ");
	c1 = getchar();
	printf("\nThe entered character is: ");
	putchar(c1);
	
	   // Second character
	printf("\nEnter any character : ");
	c2 = getchar();
	printf("\nThe entered character is: ");
	putchar(c2);
	
	   // Third character
	printf("\nEnter any character : ");
	c3 = getchar();
	printf("\nThe entered character is: ");
	putchar(c3);
	return 0;
}
In this program if user enters a multiple characters in first line, then next getchar() function will not wait for keyboard, instead they read from buffer and it will not be a friendly situation in programs. Try running this program with different inputs and learn by yourself.


  • Basic Input Output in C - Formatted IO

Unformatted IO supports functions those will work on characters or string data mainly. To input other kind of data we need formatted functions. Famous of these functions are scanf() and printf() for taking input and writing output respectively.

printf() and scanf() functions also use some escape sequences. These are special character constants used during printing output to screen. These are helpful in making the output more readable and more catchy. The following are the escape sequences defined: -

Basic Input Output in C                                      

These will be used in printf() function in next sections.

Program: 

#include<stdio.h>
int main() 
{
  int a=10;
  char b='b';
  char str[100]="Hello and Welcome to Learning CS.";
  float f=10.4;
  double d=123456.9876;
  long int l=123456789;
  long double ld=28761872.67352;
  printf("int a=%d, char b=%c",a,b);
  printf("\nstr=%s",str);
  printf("\nfloat f=%f, double d=%lf",f,d);
  printf("\nlong int l=%ld, long double ld=%Lf",l,ld);
  return 0;
}
Output:-

There are also format modifiers for printf() like minimum field width, number of decimal places, alignment etc. these will be placed between % and format code. These are as follows: -

Minimum width specifier: - If we add a number between % sign and format code, it works like the minimum width of the output. It will then pads the output with spaces to make it happen. Although if the value is bigger than this number then it will printed as it is. For example “%5d”, %6f”

The precision specifier: - it will follow the minimum width specifier (if exists). It consists of a period followed by an integer. so “%10.4f” will display a floating number at least 10 characters wide with 4 decimal places.

Justification: -By default the justification in printf() is right alignment. We can change it by placing a ‘-‘ (minus) sign after % sign in format specifier for example “%-10.4f”.


scanf()

It is used to read the data from keyboard in the same way as the printf() is used for all built-in types. The prototype of scanf() is: -

int scanf(const char *control_string,…);

similar to printf() the control_string will contain format specifiers. Which are defined below to read different kind of data types: -

Code      Meaning
%a       Reads a floating -point value (C99 only).
%c       Reads a single character.
%d       Reads a decimal integer.
%i       Reads an integer in either decimal, octal, or hexadecimal format.
%e       Reads a floating -point number.
%f       Reads a floating -point number.
%g       Reads a floating -point number.
%o       Reads an octal number.
%s       Reads a string.
%x       Reads a hexadecimal number.
%p       Reads a pointer.
%n       Receives an integer value equal to the number of characters read so far.
%u       Reads an unsigned decimal integer.
%[ ]       Scans for a set of characters.
%%       Reads a percent sign.


scanf() function will take the address of the variable in which the value needs to be saved so that the vlue entered from keyboard will be placed at that location in memory. So the variable name is passed preceded by a & (address of operator) sign.

The following program will show the use of printf() and scanf() functions: -

Program:

#include <stdio.h>
int main( ) 
{
  char str[100];
  int i;
  printf( "Enter an integer value: ");
  scanf("%d", &i);
  printf( "Enter a String: ");
  scanf("%s", str);   // as array name is the address already so no need of & operator
  printf( "\nYou Entered: %s %d ", str, i);
return 0;
}

Post a Comment

If you have any doubts, Please let me know

Previous Post Next Post