C and C++ Programming Resources

Custom Search

Input and Output

First of all we need to learn about streams in C Programming. All C Programming input and output is done with streams, no matter where input is coming from or where output is going to. This is the standard way of handling all input and output and has definite advantages for the programmer. A library package has been evolved which is known as known as the ?Standard I/O Library? which is used in any C program by using stdio.h header. Of course, now that we know its importance, it is essential that we understand what streams are and how they work. First, however, we need to understand exactly what the terms input and output mean in context of C.

The C programming language provides input and output support using library functions, which gives an advantage to system designers to tailor their input and output on their own.

What Exactly Is Program Input/Output?

A C program keeps data in random access memory (RAM) while executing. This data is in the form of variables, structures, and arrays that have been declared by the program. The question is where did this data come from, and what can the program do with it?

  1. Data can come from some location external to the program. Data moved from an external location into RAM, where the program can access it, is called input. The keyboard and disk files are the most common sources of program input.
  2. Data can also be sent to a location external to the program; this is called output. The most common destinations for output are the screen, a printer, and disk files.

Input sources and output destinations are collectively referred to as devices. The keyboard is a device; the screen is a device, and so on. Some devices (the keyboard) are for input only, others (the screen) are for output only, and still others (disk files) are for both input and output. Whatever the device, and whether it’s performing input or output, C carries out all input and output operations by means of streams.

What is a Stream?

A stream is a sequence of characters. More exactly, it is a sequence of bytes of data. A sequence of bytes flowing into a program is an input stream; a sequence of bytes flowing out of a program is an output stream. By focusing on streams, we don’t have to worry as much about where they’re going or where they originated.

The major advantage of streams, therefore, is that input/output programming is device independent. Programmers don’t need to write special input/output functions for each device (keyboard, disk, and so on). The program sees input/output as a continuous stream of bytes no matter where the input is coming from or going to.

Every C stream is connected to a file. In this context, the term file doesn’t refer to a disk file. Rather, it is an intermediate step between the stream that the program deals with and the actual physical device being used for input or output. For the most part, the beginning C programmer doesn’t need to be concerned with these files, because the details of interactions between streams, files, and devices are taken care of automatically by the C library functions and the operating system.

Modes of Streams in C Language

C streams can be divided into two modes: text and binary.

Text Stream

A text stream consists only of characters, such as text data being sent to the screen. Text streams are organized into lines, which can be up to 255 characters long and are terminated by an end-of-line, or newline, character. Certain characters in a text stream are recognized as having special meaning, such as the newline character.

Binary Stream

A binary stream can handle any sort of data, including, but not limited to, text data. Bytes of data in a binary stream aren’t translated or interpreted in any special way; they are read and written exactly as-is. Binary streams are used primarily with disk files.

Predefined Streams

ANSI C has three predefined streams, also referred to as the standard input/output files. If you’re programming for an IBM-compatible PC running DOS, two additional standard streams are available to you. These streams are automatically opened when a C program starts executing and are closed when the program terminates. The programmer doesn’t need to take any special action to make these streams available. Table lists the standard streams and the devices they normally are connected with. All five of the standard streams are text-mode streams.

The five standard streams.
NAME STREAM DEVICE
stdin Standard Input Keyboard
stdout Standard Output Screen
stderr Standard Error Screen
stdprn* Standard Printer Printer (LPT1)
stdaux* Standard Auxiliary Serial Port (COM1)
(*) Supported only under DOS

Whenever we have to use the printf() or puts() functions to display text on-screen, we use the stdout stream. Likewise, when we use gets() or scanf() to read keyboard input, we use the stdin stream. The standard streams are opened automatically, but other streams, such as those used to manipulate information stored on disk, must be opened explicitly.

Pages: [Page - 1] [Page - 2] [Page - 3] [Page - 4] [Page - 5] [Page - 6]

Tags: , , , , ,

There are 154 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.

  • Hackersh says:

    I really want to write a prog that will write to an open page in Microsoft word……..any info

  • Rivera says:

    i want a program thats prompts the user to input an integer and then output the number with the digit reserved.for example if input is 12345, the output should be 54321.

    • Speedy says:

      I dont have C/C++ compiler now. So, giving a code which will work… guess syntax error wont be there

      #include<stdio.h>
      #include<string.h>
      #include<conio.h>
      void main()
      {
      static char a[1000];
      int i=0,n=0;
      printf("Enter the number:");
      gets(a);//if any error occurs, in this line use a=gets();
      n=strlen(a);
      for(i=n-1;i>=0;i--)
      {
          printf("%c",a[i]);
      }
      getch();
      }
      

      ——– Original Message ——–
      i want a program thats prompts the user to input an integer and then output the number with the digit reserved.for example if input is 12345, the output should be 54321.

    • sme lng;-> says:

      #include
      main()
      {
      int n,r;
      clrscr();
      printf(”
      Enter a number:”);
      scanf(“%d”, &n);
      for(; n != 0 ;) {
      r= n%10;
      printf(“%d”, r);
      n= n%10;
      }
      getch();
      }

  • cestinaline says:

    How to write a program that will compute and display the midterm grade of student.
    the midterm grade is equal to 1/3 of the minor A exam and 2/3 of the midterm exam.

  • Hari says:

    i am doing cgi programming.. during server processing,instead of write some details to a temp file i want ot use stdin -stdout.
    what i planed is like….

    exec 1>&0 # redirect stdout to stdin
    system(“ls”) # it will write o/p to stdin
    while(fgets(line,sizeof(line),stdin)!=NULL)
    puts(line);

    but things not working properly…

  • bishwendu says:

    i want a simple project in c language with complete explanation of various commands or syntax used.Level of the project being B.Tech(fifth sem).Please it is urgent as matter relates to on campus placements.It is really important.kindly email me at bishwendu029@gmail.com.please!!!!!!!!

  • Demkish says:

    Hi, is there anyone who can help me make a program in C in which if we enter a month and day the program will outputs a zodiac sign. Please help me I need it badly…:-(

  • Ali says:

    hi is there any one who know about c language Abook or site that can be usefull for bigginer becouse I realy need it.
    Thanks

  • Clarence_L says:

    Please Help Me Onegai i need an output i will put 5 numbers that will set in ascending and descending order using array in c++ thnx in advance please i really in need….

  • miiszarniie says:

    write a C program that will create a 3×3 matrix & compute for the sum of columns & sum of rows.

  • jamz2008 says:

    4.Input the amount of purchase, which is less than P100.00. Calculate the Change with the following breakdown:

    P50.00 – _______;
    P20.00 – _______;
    P10.00 – _______;
    P 5.00 – _______;
    P 1.00 – _______;

    PLEASE HELP ME WITH THIS

  • laarni says:

    How to write a program that will compute and display the midterm grade of student.
    the midterm grade is equal to 1/3 of the minor A exam and 2/3 of the midterm exam.please help me for the code i need it 2mrw.plz email me add dj_bendz67@yahoo.com for the code.tnx

  • scarletdream says:

    how to change the output to input in C++….the another’s program output works as an input…can everybody help me?


Leave a Reply

You must be logged in to post a comment.