Standard ইনপুট ও আউটপুট এর জন্য কোনো কিছু পড়া বা ডিসপ্লেতে প্রিন্ট করা

Submitted by administrator on Mon, 01/02/2012 - 14:39

 

CLASS 5: Reading from and Writing to Standard I/O

 

In the last lesson you learned how to print out characters, integers, and floating-point numbers to the screen by calling the printf() function. In this lesson you're going to learn more about printf(), as well as about the following functions, which are necessary to receive the input from the user or print the output to the screen:

  • The getc() function
  • The putc() function
  • The getchar() function
  • The putchar() function

Before we jump into these new functions, let's first get an idea about the standard input and output in C.

The Standard Input and Output (I/O)
A file contains related characters and numbers. Because all characters and numbers are represented in bits on computers, the C language treats a file as a series of bytes. (8 bits make up 1 byte.) A series of bytes is also called a stream. In fact, the C language treats all file streams equally, although some of the file streams may come from a disk or tape drive, from a terminal, or even from a printer.

 

Additionally, in C, there are three file streams that are pre-opened for you:

  • stdin–The standard input for reading.
  • stdout–The standard output for writing.
  • stderr–The standard error for writing error messages.

Usually, the standard input (stdin) file stream links to your keyboard, while the standard output (stdout) and the standard error (stderr) file streams point to your terminal screen. Also, many operating systems allow you to redirect these files' streams.

 

In fact, you've already used stdout. When you executed the printf() function in the last lesson, you were in fact sending the output to the default file stream, stdout, which points to your screen.

 

You'll learn more on stdin and stdout in the following sections.

 

NOTE
The C language provides many functions to manipulate file reading and writing (I/O). The header file stdio.h contains the declarations for those functions. Therefore, always include the header file stdio.h in your C program before doing anything with the file I/O.

 

Getting the Input from the User
In these days, typing from keyboard is still the de facto standard way to input information into computers. The C language has several functions to direct the computer to read the input from the user (typically through the keyboard.) In this lesson the getc() and getchar() functions are introduced.

 

Printing the Output on the Screen
Besides getc() and getchar() for reading, the C language also provides two functions, putc() and putchar(), for writing. The following two sections introduce these functions.

 

 

Summary
In this lesson you've learned the following:

  •  The C language treats a file as a series of bytes.
  • stdin, stdout, and stderr are three file streams that are pre-opened for you to use.
  •  The C library functions getc() and getchar() can be used to read in one character from the standard input.
  • The C library functions putc() and putchar() can be used to write one character to the standard output.
  •  %x or %X can be used to convert decimal numbers to hex numbers.
  • A minimum field width can be specified and ensured by adding an integer into a format specifier.
  • An output can be aligned at either the left or right edge of the output field.
  • A precision specifier can be used to specify the decimal place number for floating-point numbers, or the maximum field width for integers or strings.

 

In the next lesson you'll learn about some important operators in C.

 

 

Related Items

The #define and #undef Directives

The #define and #undef Directives

The #define directive is the most common preprocessor directive, which tells the preprocessor to replace every occurrence of a particular character string (that is, a macro name) with a specified value (that is, a macro body).

The C Preprocessor Versus the Compiler

The C Preprocessor Versus the Compiler

One important thing you need to remember is that the C preprocessor is not part of the C compiler.

What Is the C Preprocessor?

If there is a constant appearing in several places in your program, it's a good idea to associate a symbolic name to the constant, and then use the symbolic name to replace the constant throughout the program. There are two advantages to doing so. First, your program will be more readable.

Exercises : Answer the following Question

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Question and Answer

    Q Why is random access to a disk file necessary?