Input / Output Functions in C.

 Input / Output Functions in C



How to Use the C Input/output Functions to Get Input/output from the User.

In C, the standard input/output library provides functions to perform input and output operations.
In C, these input/output operations are performed by two standard input/output functions, these are printf() and scanf(). These functions can be accessed by including the standard input/output library (stdio.h) in the program.

printf Function

The standard library function printf (pronounced as print-eff) is used for formatted output. It takes as arguments a format string and an optional list of variables to output. The values of variables are displayed according to the specifications in the format string.

Syntax

The print() function will take the form:

To print values of variables:

   printf (format string, var1, var2, var3, ......);

To print a constant message:

   printf (Format string);

Example 

  • #include<stdio.h>
  • int main()
  • {
  • int number;
  • printf("enter a number:");
  • scanf("%d",&number);
  • printf("cube of number is:%d",number*number*number);
  • return 0;
  • }

The format string is a character string nothing more and the variables are optional.

Scanf Function

The scanf ( pronounced as scan-eff) function is versatile as it is equally good for numeric as well as string input.
It takes as arguments a format string and a list of variables to hold the input values.

Syntax

Here is the syntax of scanf function:
  scanf (format string, &var1, &var2, &var3, ........);

Example

#include <stdio.h>
int main()
{
    int testInteger;
    printf("Enter an integer: ");
    scanf("%d", &testInteger);  
    printf("Number = %d",testInteger);
    return 0;
}

Format specifier

Format specifier specifies the format in which values of a variable should be displayed on the screen. Format specifiers are specified in the format string

Some format specifiers

Example

#include <stdio.h>
int main()
 { 
  char first_ch = 'f'; 
  printf("%c\n", first_ch); 
  return 0; 

Field-width Specifier

The number of columns used to display a value on the screen is referred to as field-width. Field-width specifiers describe the number of columns that should be used to print a value.

Example

We simply need to add a number between the % and data type of the format specifier in the print format string. This number specifies the field-width or the numbers of columns to be used for the display of the value.
                      
                              printf("Area = %4d");            printf("Height = 6.2f", height);

Escape Sequences

Escape sequences are the characters that are specified in the format string of printf statement in combination with a backslash (\).
These cause an escape from the normal interpretation of a string so that the next character is recognized as having a special meaning.

Example

#include<stdio.h>
int main()
{
    printf("Happy\tNew\tYear");
    return 0;
}

Character Input

In C, there are many functions to accept character input. The versatile scanf can also be used for this purpose. But scanf requires
pressing the return key at the end of input value. In some cases, it is desirable to get character input without pressing return key. To overcome such situation C is eqquiped with many other functions for character input such as getch and getche.

getch/getche

The getch and getche are very handy in character manipulation. In contrast to getch function which does not echo the character typed, getche echo the typed character. Both of these functions do not accept any argument.

Example

#include <stdio.h>
#include <conio.h>
 
int main() {
    char ch = getch();
    printf("Received Input: %c\n", ch);
    return 0;
}
----------------------------------------------------------[]---------------------------------------------------------
Feel free to ask any questions in the comment section.

Here is the third article on C-Language. 

Lecture No. 4 on C-language will be uploaded soon.

Post a Comment

0 Comments