Monday, 20 July 2015

Input and output in C!

There are two primary ways to access files in C: file descriptors and file streams. File descriptors use a set of low-level I/O functions, and file streams are a higher-level form of buffered I/O that is built on the lower-level functions. Here,the focus will be on the low-level I/O functions that use file descriptors.

In this tutorial we will describe the standard library, a set of functions that provide input and output, and a variety of other services for C programs. The Input means to feed some data into program. The Output means to display some data on screen, printer or in any file. When a C program is started,three file are automatically opened  to provide access to the keyboard and screen. These file are :
standard input
standard output
standard error

                                                            


The corresponding file pointer are called stdin,stdout, and stderr and are declared in <stdio.h> . Normally stdin is connected to the keyboard and stdout and stderr are connected to the screen. stdin and stdout may be redirect to files and pipes.

Standard Input and Output :
Suppose you hack a server and now you want to execute some commands on compromised server.To do so, first you write a program that takes your command(standard input) and execute it and prints the results(standard output) on the screen. So to read on character at a time from the standard input, normally the keyboard :
int getchar(void);
getchar() function reads the next available character from the screen and returns it as an integer. This function reads only single character at a time. You can use this method in the loop in case you want to read more than one characters from the screen.

The function
int putchar(int);
is used for output. putchar(c) put the character c on the standard output, which is by default the screen.

Let's consider a simple program:
  1. #include <stdio.h>
  2. int main( )
  3. {
  4.    int a;

  5.    printf( "Enter Command :");
  6.    a = getchar( );

  7.    printf( "\nYou entered: ");
  8.    putchar( a );

  9.    return 0;
  10. }

Formatted Input and Output:
Reading and writing data one character at a time can be painful. The C standard I/O library provides several convenient routines for reading and writing formatted data. C provides standard functions scanf() and printf(), for performing formatted input and output .

Formatted Output:
The function is defined as follows:
int printf(char *format, arg1, arg2 ...)
writes output to the standard output stream stdout and produces output according to a format provided. where arg1, arg2 .... are argument list.

Example:
printf (“%c”, arg);
The character specified after % is called a conversion character because it allows one data type to be converted to another type and printed.

Formatted input:
This function is defined as follows:
int scanf(char *format, args....)
reads input from the standard input stream stdin and scans that input according to format provided.

For Example:
scanf(“ %c %d”,&Name, &Roll_No);
the data names are listed as &Name and &Roll_No instead of Name and Roll_No respectively. This is how data names are specified in a scnaf() function. In case of string type data names, the data name is not preceded by the character &.
The scanf routine eats whitespace (spaces, tabs, newlines, etc.) in its input whenever it sees a conversion specification or a whitespace character in its format string. Non-whitespace characters that are not part of conversion specifications must match exactly. To detect if scanf parsed everything successfully, look at its return value; it returns the number of values it filled in, or EOF if it hits end-of-file before filling in any values.

The format can be a simple constant string, but you can specify %s, %d, %c, %f, etc., to print or read strings, integer, character or float respectively. There are many other formatting options available which can be used based on requirements. For a complete detail, The C programming language by Dennis M. Ritchie .

Here is the simple example which makes things clear:

  1. #include <stdio.h>
  2. int main( )
  3. {
  4.    char arr[100];
  5.    int a;

  6.    printf( "Enter a value :");
  7.    scanf("%s %d", arr, &a);

  8.    printf( "\nYou entered: %s %d ", arr, a);

  9.    return 0;
  10. }

Well this is all about standard input and output devices handled by C programming language.After handling input and output, the next step is how to open , write, and close the files. In the next tutorial, we will see how C programmers can create, open, close text or binary files for their data storage.

If you like this post or have any question, please feel free to comment !

1 comment:

  1. I was wondering if you could tell me what this means as it is the name of a withdrawal from my bank account.

    OUTPUTON.C 39.95_V

    ReplyDelete

Blogger Widget