C Input and output Functions

Introduction of Input and Output C Functions

In any programming language, an interface plays a very important role. It takes data from the user and displays the output. One of the essential operations performed in C program is to provide input value to the program and output the data produced by the program to be standard output device. Input refers to entering some data into the program and output refers to displaying some data on the screen, printer or any file. You can provide input in the form of file or as a command from the command prompt. C programming language provides a set of build-in-function to read the given input and output the required data.


Input/Output Functions

The C language provides several input-outputs that can perform input/output operations. These functions are collectively unknown as standard input/output Library. This library is denoted by stdio. The stdio.h is the header file that contains the stdio functions. 
      In C programming Language, some of the standard input./output functions are

  1. scanf() : Reads inputs from the standard input device
  2. printf() : Displays information on the screen
  3. getchar() : Reads a single character
  4. putchar() : Writes characters one at a time
  5. gets() : Reads a strings
  6. puts() : Prints a strings
These Functions can be categorized into Formatted and Unformatted Functions.

Type
Inputs Functions (s)
Output Functions (s)
Formatted
Scanf()
Printf()
Unformatted
Getcchar(), gets()
Putchar(), puts()


printf() Function

The printf() function is used to display information on the screen. It return the number of character printf by printf() function, or a negative value if an output error occurs.
          The syntax for printf() function is:
          printf ('control_character", variable_list);
      where control_character determines the type and format of the values to be displayed and variable_list is the list of variable that you want to display.

Let's see some example where, we have used the printf() functions:

printf("Rajasthan Technical University");
printf("%d",var1);
printf("%f %f", m, n);
printf("sum of four numbers are=%d", sum);
printf("\n A=%d, B=%d", a, b);

First statement displays text enclosed within the pair of double-quotes.Second statement displays the value of var1. Third statement displays the value of variable  m and n. Fourth statement displays the message and then displays the value of sum variable next to it. fifth statement displays the value of variable a and b next to A= and B=. The following program shows how to use the printf() functions:
WAP using the printf() function
Using printf() Function
Output:
abcde
Total characters printed 6
....................................................................

scanf() Function

The scanf() function is a formatted function in C that is used to read information from standard input device, such as keyboard. The scanf() function is used to enter the numeric that contains the first string agrument and it may have additional arguments. The additional agruments must be pointers (to implement call by reference).
                The syntax for scanf() function is as follows:
       scanf("control_character", address_list);

     where control_character is a sequence of one or more control characters. Control character decides the type of values that need to be provided to the variables. Control characters are preceded by a % sign.
         address_list determines the address of memory locations where the values of input variable should be stored.

Rules to Remember:

Following rules sholud be kept in mind before using the scanf() function:
  1. A pair of double quotes is used within the control character for example
    scanf("%d, &b);
  2. For every input variable, there must be a control character.
    scanf("%d %d %d", &m, &n, &p);
  3. Multiple number of character groups can be allowedwithin the control character.
    scanf("%d %f %c", &a, &b, &c);
  4. Use ampersand (&0 symbol follwed by the input variables in the
    address_list.
      scanf("%c",&a);
  5. Use commas to separate input variablezs.
    scanf("%f %d", &m, &n);
  6. Use comma to separate control_characters and address_list
    scanf("%u %d", &var1, &var2);

The Follwing program helps you to understand the use of scanf() Function

WAP using scanf() Function
Using the scanf() Function
Output:
Enter Three Integer : 34 56 43
Total values inputted : 3
The input values are : 34 56 43

getchar() Function

The getchar() function reads a single character from the standard input device. getchar() returns the character it reads, or, the special value EOF(end of file), if there are no more character available. The syntax of getchar() function is as follows:
               var_char = getchar();
          where var_char is a character type variable.
The following program shows how to use the getchar() function

WAP using the getchar() function
Using the getchar() function
Output:
Type a character : n
You typed : n

gets() Function

The gets() function reads a strimgs of characters from the user until a <newline> is reads or an end-of-file condition is encountered. It stores the string of characters in a set of consecutive memory locations, which is called an array. The example for this functions is as follows:

char string[50]
gets(string);

 The following program shows how to use gets() functions:

WAP using gets() function
Using the gets() Function
Output:
Tyoe something : Jamshedpur workers college
You Typed : Jamshedpur workers college

putchar() Function

The putchar() function writes a single character to the standard output stream, stdout. The putchar() function is specified in C language's standard library headerfile stdio.h. the character to be printed is fed into the function as a argument , and if the writing is sucessful, the argument character is returned. Otherwise, end-of-file is returned.

The following program shows how to use
putchar() functions:

WAP using the putchar() functions
Using the putchar() function
Output:
Type a character : N
You typed : N

puts() Function

The puts() functions displays a string of text and appends a newline character ('\n') to it. The syntax for this functions is:

puts(string);

The following program shows how to use puts() functions:
WAP using the puts() function
Using the puts() function
Output:
Type Something : World of programming C
You Typed : World of programming C
--------------------------------------------------------------------


Post a Comment

0 Comments