Input and Output Functions in C Language

Different ways of performing input - output opeations in C
Input and output operations are performed using predefined library functions. These are classified into two types.

  1. Formatted Input / Output Functions
  2. Unformatted Input / Output Functions

1. Formatted Input / Output Functions

There are two formatted input/ouput functions.

  • scanf() : used for obtaining formatted input.
  • printf() : used for obtaining formatted output.

scanf() :

The scanf() function is used to read the values for variables from standard input device i.e., keyword.

Syntax : scanf(" control string ", address_list);

Control String :

  • Control string is enclosed within double quotes
  • It specifies the type of values that are to be read from keyboard.
  • Control string consists of fields specification written in the form % field specified.

Address_list :

It contains addresses of input/output variables preceded. The addresses are specified by preceding a variable by & operator.

Example : scanf("%d %f %c", &i, &a, &b);

When user enters 10, 5.5 , z from keyboard, 10 is assigned to i, 5.5 is assigned to a, and z is assigned to b.

We can also specify field widths in field formats as % field width type specifier.

Example : scanf("%3d %2d", &a, &b);

If we give input 500 and 10, then 500 is assigned to a and 10 is assigned to b.

It is not always advisable to use field width specifiers in scanf statements.

It may sometimes assign wrong values to variables

Example : scanf("%2d %3d", &a, &b);

Here, if we give 5004 and 10 as input, then 50 is assigned to a and 04 to b. which is wrong assignment. hence, generally field widths are not used with scanf statement.

Field formats for different data types are given below

Format Type of value
%d Integer
%f Floating numbers
%c Character
%ld Long integer
%u Unsigned integer
%lf Double
%s String type
%x Hexadecimal
%o Octal
%i Decimal, hexadeciaml or octal
%e Floating point numbers in exponential form
%g Floating point numbers with trailing zeros

The follwoing letters may be used as prefix for certain conversion characters.

  • for short integers
  • for long integers or double
  • for long double

printf() :

printf() function is used to print result on monitor.

Example : printf("control string", arg1, arg2,...argn);

Control String :

Control string can have,

  • Format specifier given in the form % specifier.
  • Escape sequence characters such as \t(tab), \n(new line), \b(blank) etc.
  • It can have a string that must be printed on the standard o/p device, i.e monitor or console.

arg1, arg2,...argn are variables whose value must be printed on the monitor in the format specified in control string.

Examples :

1. printf("%d %c", num, ch);
2. printf("hello world");
3. printf("%d \n %c", num, ch);

In example 3, suppose num has value 5 and ch has value a then output will be printed as,

The output is printed in two different lines because new line character has been used between %d and %c.

Unformatted Input/output Functions :

Different functions in this category are as follows

  1. getchar()
  2. putchar()
  3. gets()
  4. puts()
  5. getch()
  6. putch()

1. getchar() :

This function returns single character entered form keyboard. No arguments are required for this function. By calling this function we can read a string

Syntax : var = getchar(); var is an identifier of char type

2. putchar(var) :

This function displays a single character on an output device.

3. gets() :

This function reads an input string

Syntax : gets(var); var is a character array
 Example : 
  char name[50];
This statement reads a string from keyboard.

4. puts(var :

This function displays string stored in var on output device.

5. getch()

6. putch()

program 1 : This program explains getchar() and putchar() functions.

 void main()
   int i = 0;
   char ch[10];
      printf("Enter characters from keyboard : ");
   for(i=1;ch[i]!='\0'; i++)
input : Enter charactes from keyboard : abcd
Output : abcd

program 2 : This program explains gets() and puts() functions.

 void main()
    char a[25];
    printf("Enter the string : ");
input : Enter the string : Hello
Output : Hello
0 Komentar untuk "Input and Output Functions in C Language"

Back To Top