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

মডুলার C প্রোগ্রামিং (Modular C Programming)

কেবল মাত্র একটি ফাংশন দিয়ে কোনো বড়ো জটিল সমস্যা সমাধানের চেষ্টা করা ভাল প্রোগ্রামিংয়ের পদ্ধতি নয়। সঠিক পদ্ধতি হ'ল সমস্যাটিকে কয়েকটি ছোট ছোট এবং সরল টুকরো করে ফেলা যাতে তা আরও বিশদে বোঝা যায় । তারপরে এই ছোট এবং সরল সমস্যাগুলি সমাধান করার জন্য ছোট ছোট ফাংশন ব্লক তৈরি করা এবং পরে সেগুলি নিয়মানুযায়ী সংযোজিত করা ।

Programming Style

Programming Style

In this section, I'd like to briefly highlight some points that will help you write clean programs that can easily be read, understood, and maintained.

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 Is the C preprocessor part of the C compiler?

    A No. The C preprocessor is not part of the C compiler. With its own line-oriented grammar and syntax, the C preprocessor runs before the compiler in order to handle named constants, macros, and inclusion of files.

Compiling Your Code Under Conditions

Compiling Your Code Under Conditions

You can select portions of your C program that you want to compile by using a set of preprocessor directives. This is useful, especially when you're testing a piece of new code or debugging a portion of code.