How to shrink your stomach

How to shrink your stomach created by jack federbroke helpd me to loss weight so i want to feedback him for that thank you jack! How to shrink your stomach

Monday, March 5, 2012

C LANGUAGE TUTORIAL 9. Standard Input/Output

9.Standard Input/Output

9.1 The Stdio.H Header File

Load the file simpleio.c for our first look at a file with standard I/O. Standard I/O refers
to the most usual places where data is either read from, the keyboard, or written to, the video
monitor. Since they are used so much, they are used as the default I/O devices and do not need
to be named in the Input/Output instructions. This will make more sense when we actually start
to use them so lets look at the file in front of you.

# include "/sys/stdio.h" /* standard header for input/output */
main( )
char c;
printf("Enter any characters, X = halt program.\n");
do {
c = getchar(); /* Get a character from the kb  */
putchar(c); /* Display the character on the monitor */
  }  while (c != ’X’); /* Until and X is hit  */
printf("\nEnd of program.\n");

The first thing you notice is the first line of the file, the #include "stdio.h" line. This is very
much like the #define we have already studied, except that instead of a simple substitution, an
entire file is read in at this point. The system will find the file named "stdio.h" and read its entire
contents in, replacing this statement. Obviously then, the file named "stdio.h" must contain
valid C source statements that can be compiled as part of a program. This particular file is
composed of several standard #defines to define some of the standard I/O operations. The file
is called a header file and you will find several different header files on the source disks that
came with your compiler. Each of the header files has a specific purpose and any or all of them
can be included in any program.

Most C compilers use the double quote marks to indicate that the "include" file will be found
in the current directory. A few use the "less than" and "greater than" signs to indicate that the
file will be found in a standard header file. Nearly all MSDOS C compilers use the double
quotes, and most require the "include" file to be in the default directory. All of the programs
in this tutorial have the double quotes in the "include" statements. If your compiler uses the
other notation, you will have to change them before compiling.

9.2 Input/Output Operations In C

Actually the C programming language has no input or output operations defined as part of the
language, they must be user defined. Since everybody does not want to reinvent his own input
and output operations, the compiler writers have done a lot of this for us and supplied us with
several input functions and several output functions to aid in our program development. The
functions have become a standard, and you will find the same functions available in nearly every

In fact, the industry standard of the C language definition has become the book written by
Kernigan and Ritchie, and they have included these functions in their definition. You will often,
when reading literature about C, find a reference to K & R. This refers to the book written by
Kernigan and Ritchie. You would be advised to purchase a copy for reference.

You should print out the file named "stdio.h" and spend some time studying it. There will be
a lot that you will not understand about it, but parts of it will look familiar. The name "stdio.h"
is sort of cryptic for "standard input/output header", because that is exactly what it does. It
defines the standard input and output functions in the form of #defines and macros. Don’t worry
too much about the details of this now. You can always return to this topic later for more study
if it interests you, but you will really have no need to completely understand the "stdio.h" file.

You will have a tremendous need to use it however, so these comments on its use and purpose
are necessary.

9.3 Other Include Files

When you begin writing larger programs and splitting them up into separately compiled portions,
you will have occasion to use some statements common to each of the portions. It would be to
your advantage to make a separate file containing the statements and use the #include to insert
it into each of the files. If you want to change any file, you will be assured of having all of the
common statements agree. This is getting a little ahead of ourselves but you now have an idea
how the #include directive can be used.

9.4 Back To The File Named Simpleio.c

Let us continue our tour of the file in question. The one variable "c" is defined and a message
is printed out with the familiar "printf" function. We then find ourselves in a continuous loop
as long as "c" is not equal to capital X. If there is any question in your mind about the loop
control, you should review Chapter 3 before continuing. The two new functions within the loop
are of paramount interest in this program since they are the new functions. These are functions
to read a character from the keyboard and display it on the monitor one character at a time.

The function "getchar()" reads a single character from the standard input device, the keyboard
being assumed because that is the standard input device, and assigns it to the variable "c". The
next function "putchar(c)", uses the standard output device, the video monitor, and outputs the
character contained in the variable "c". The character is output at the current cursor location
and the cursor is advanced one space for the next character. The system is therefore taking care
of a lot of the overhead for us. The loop continues reading and displaying characters until we
type a capital X which terminates the loop.

At this point we need to mention that 1616/OS, Unix and MS-DOS sometimes do different
things, even in apparently simple areas such as this. In some version of HiTech C for the
1616/OS, characters are repeated as you type them, but are not repeated when you press the
return key. This is because the "getchar()" function was redefined as the 1616/OS function
"getch()", which is not precisely the same.

Compile and run this program on an MS-DOS machine for a few surprises. When you type on
the keyboard, you will notice that what you type is displayed faithfully on the screen, and when
you hit the return key, the entire line is repeated. In fact, we only told it to output each character
once but it seems to be saving the characters up and redisplaying them. A short explanation is
in order.

9.5 Dos Is Helping Us Out (Or Getting In The Way)

We need to understand a little bit about how MS-DOS works to understand what is happening
here. When data is read from the keyboard, under MS-DOS control, the characters are stored
in a buffer until a carriage return is entered, at which time the entire string of characters is given
to the program. While the characters are being typed, however, the characters are displayed
one at a time on the monitor. This is called echo, and happens in many of the applications you

With the above paragraph in mind, it should be clear that when you are typing a line of data into
"SIMPLEIO", the characters are being echoed by MS-DOS, and when you return the carriage,
the characters are given to the program. As each character is given to the program, it displays
it on the screen resulting in a repeat of the line typed in. To better illustrate this, type a line with
a capital X somewhere in the middle of the line. You can type as many characters as you like
following the "X" and they will all display because the characters are being read in under
MS-DOS, echoed to the monitor, and placed in the MS-DOS keyboard input buffer. MS-DOS
doesn’t think there is anything special about a capital X. When the string is given to the program,
however, the characters are accepted by the program one at a time and sent to the monitor one
at a time, until a capital X is encountered. After the capital X is displayed, the loop is terminated,
and the program is terminated. The characters on the input line following the capital X are not
displayed because the capital X signalled program termination.

In the Applix 1616, the stdio.h function getchar() is defined in stdio.h as getch(), which
is provided by 1616/OS, but is not exactly identical to the expected reslts from "getchar()". The
keyboard buffer is read by getch() and the characters passed straight along to the program.
Within the program, the characters are echoed to the display by putchar(), and the 1616/OS
only has to change the cursor position. If you leave out the #include "stdi.h", then the HiTech
C compiler will use its default "getchar()" function, which is correct, and will work the same as
Compile and run"simpleio.c. After running the programseveral times and feeling confidant
that you understand the above explanation, we will go on to another program.

Don’t get discouraged by the above seemingly weird behaviour of the I/O system. It is strange,
but there are other ways to get data into the computer. You will actually find the above method
useful for many applications, and you will probably find some of the following useful also.

9.6 Another Strange I/O Method

Load the file named singleio.c and display it on your monitor for another method of
character I/O. Once again, we start with the standard I/O header file, we define a variable named
"c", and we print a welcoming message. Like the last program, we are in a loop that will continue
to execute until we type a capital X, but the action is a little different here.

# include "/sys/stdio.h" /* standard header for input/output */
main( )
char c;
printf("Enter any characters, terminate program with X\n");
do {
c = getch(); /* Get a character  */
putchar(c); /* Display the hit key */
  }  while (c != ’X’);
  printf("\nEnd of program.\n");

The "getch()" is a new function that is a "get character" function. It differs from "getchar()" in
that it does not get tied up in DOS. It reads the character in without echo, and puts it directly
into the program where it is operated on immediately. This function then reads a character
immediately displays it on the screen, and continues the operation until a capital X is typed.

You wil recognise that using this "getch()" function accidently in the #include "stdio.h" caused
problems in the previous program.

When you compile and run this program, you will find that there is no repeat of the lines when
you hit a carriage return, and when you hit the capital X, the program terminates immediately.
No carriage return is needed to get it to accept the line with the X in it. We do have another
problem here, there is no linefeed with the carriage return.

9.7 Now We Need A Line Feed

It is not apparent to you in most application programs but when you hit the enter key, the program
supplies a linefeed to go with the carriage return. You need to return to the left side of the
monitor and you also need to drop down a line. The linefeed is not automatic. We need to
improve our program to do this also. If you will load and display the program named betterin.c, you will find a change to incorporate this feature.

# include "stdio.h"
# define CR 13        /* this defines CR to be 13 */
# define LF 10        /* this defines LF to be 10 */
main( )
char c;
    printf("input any characters,hit X to stop.\n");
    do {
       c = getch();                  /* get a character */
       putchar(c);                   /* display the hit key */
       if (c == CR) putchar(LF);     /* if it is a carriage return
                                        put out a linefeed too */
    } while (c != ’X’);
    printf("\nEnd of program.\n");

In betterin.c, we have two additional statements at the beginning that will define the
character codes for the linefeed (LF), and the carriage return (CR). If you look at any ASCII
table you will find that the codes 10 and 13 are exactly as defined here. In the main program,
after outputting the character, we also output a linefeed which is the LF. We could have just as
well have left out the two #define statements and used "if (c == 13) putchar(10);" but it would
not be very descriptive of what we are doing here. The method used in the program represents
better programming practice.

Compile and run betterin.c to see if it does what we have said it should do. It should
display exactly what you type in, including a linefeed with each carriage return, and should stop
immediately when you type a capital X.

If you are using a nonstandard compiler, it may not find a "CR" because your system returns a
"LF" character to indicate end-of-line. It will be up to you to determine what method your
compiler uses. The quickest way is to add a "printf" statement that prints the input character in
decimal format.

9.8 Which Method Is Best?

We have examined two methods of reading characters into a C program, and are faced with a
choice of which one we should use. It really depends on the application because each method
has advantages and disadvantages. Lets take a look at each.

When using the first method, 1616/OS is actually doing all of the work for us, by storing the
characters in an input buffer and signalling us when a full line has been entered. We could write
a program that, for example, did a lot of calculations, then went to get some input. While we
were doing the calculations, 1616/OS would be accumulating a line of characters for us, and
they would be there when we were ready for them. However, we could not read in single
keystrokes because 1616/OS would not report a buffer of characters to us until it recognized a
carriage return.

The second method, used in betterin.c, allows us to get a single character, and act on it
immediately. We do not have to wait until 1616/OS decides we can have a line of characters.
We cannot do anything else while we are waiting for a character because we are waiting for the
input keystroke and tying up the entire machine. This method is useful for highly interactive
types of program interfaces. It is up to you as the programmer to decide which is best for your

I should mention at this point that there is also an "ungetch" function that works with the "getch"
function. If you "getch" a character and find that you have gone one too far, you can "ungetch"
it back to the input device. This simplifies some programs because you don’t know that you
don’t want the character until you get it. You can only "ungetch" one character back to the input
device, but that is sufficient to accomplish the task this function was designed for. It is difficult
to demonstrate this function in a simple program so its use will be up to you to study when you
need it.

The discussion so fact in this chapter, should be a good indication that, while the C programming
language is very flexible, it does put a lot of responsibility on you as the programmer to keep
many details in mind.

9.9 Now To Read In Some Integers

Load and display the file named intin.c for an example of reading in some formatted data.
The structure of this program is very similar to the last three except that we define an "int" type
variable and loop until the variable some how acquires the value of 100.

#include "/sys/stdio.h"
main( )
int valin;
printf("Input a number from 0 to 2 billion, stop with 100.\n");
do {
scanf("%d",&valin); /* read a single integer value in */
printf("The value is %d\n",valin);
} while (valin  != 100);
printf("End of program\n");

Instead of reading in a character at a time, as we have in the last three files, we read in an entire
integer value with one call using the function named "scanf". This function is very similar to
the "printf" that you have been using for quite some time by now except that it is used for input
instead of output. Examine the line with the "scanf" and you will notice that it does not ask for
the variable "valin" directly, but gives the address of the variable since it expects to have a value
returned for the function. Recall that a function must have the address of a variable in order to
return the value to the calling program. Failing to supply a pointer in the "scanf" function is
probably the most common problem encountered in using this function.

The function "scanf" scans the input line until it finds the first data field. It ignores leading
blanks and in this case, it reads integer characters until it finds a blank or an invalid decimal
character, at which time it stops reading and returns the value.

Remembering our discussion above about the way the 1616/OS input buffer works, it should
be clear that nothing is actually acted on until a complete line is entered and it is terminated by
a carriage return. At this time, the buffer reading is input, and our program will search across
the line reading all integer values it can find until the line is completely scanned. This is because
we are in a loop and we tell it to find a value, print it, find another, print it, etc. If you enter
several values on one line, it will read each one in succession and display the values. Entering
the value to 100 will cause the program to terminate, and the increased responsibility you must
assume using C rather than a higher level language such as Pascal, Modula-2, etc.

The above paragraph is true for the HiTech C compiler for the Applix, which uses 32 bit integers.
MS-DOS compilers typically use an integer value of 16 bits, and thus accept input values only
up to 32767. If your compiler is different, the same principles will be true but at different limits
than those given above.

Compile and run this program, entering several numbers on a line to see the results, and with
varying numbers of blanks between the numbers. Try entering numbers that are too big to see
what happens, and finally enter some invalid characters to see what the system does with
non-decimal characters.

9.10 Character String Input

Load and display the file named stringin.c for an example of reading a string variable.
This program is identical to the last one except that instead of an integer variable, we have
defined a string variable with an upper limit of 24 characters (remember that a string variable
must have a null character at the end).

#include "/sys/stdio.h"
main( )
char big[25];
printf("Input a character string,up to 25 characters.\n");
printf("An X in column 1 causes the program to stop.\n");
do {
printf("The string is -> %s\n",big);
} while (big[0] !=’X’);
printf("End of program.\n");

The variable in the "scanf" does not need an & because "big" is an array variable and by definition
it is already a pointer. This program should require no additional explanation. Compile and
run it to see if it works the way you except.

You probably got a surprise when you ran it because it separated your sentence into separate
words. When used in the string mode of input, "scanf" reads characters into the string until it
comes to either the end of a line or a blank character. Therefore, it reads a word, finds the blank
following it, and displays the result. Since we are in a loop, this program continues to read
words until it exhausts the DOS input buffer. We have written this program to stop whenever
it finds a capital X in column 1, but since the sentence is split up into individual words, it will
stop anytime a word begins with capital X. Try entering a 5 word sentence with a capital X as
the first character in the third word. You should get the first three words displayed, and the last
two simply ignored when program stops.

Try entering more than 24 characters to see what the program does. It should generate an error,
but that will be highly dependent on the system you are using. In an actual program, it is your
responsibility to count characters and stop when the input buffer is full. You may be getting
the feeling that a lot of responsibility is placed on you when writing in C. It is, but you also get
a lot of flexibility in the bargain too.

9.11 Input/Output Programming In C

C was not designed to be used as a language for lots of input and output, but as a systems
language where a lot of internal operations are required. You would do well to use another
language for I/O intensive programming, but C could be used if you desire. The keyboard input
is very flexible, allowing you to get at the data in a very low level way, but very little help is
given to you. It is therefore up to you to take care of all of the bookkeeping chores associated
with your required I/O operations. This may seem like a real pain in the neck, but in any given
program, you only need to define your input routines once and then use them as needed.
Don’t let this worry you. As you gain experience with C, you will easily handle your I/O

One final point must be made about these I/O functions. It is perfectly permissible to intermix
"scanf" and "getchar" functions during read operations. In the same manner, it is also fine to
intermix the output functions "printf" and "putchar".

9.12 In Memory I/O

The next operation may seem a little strange at first, but you will probably see lots of uses for
it as you gain experience. Load the file named inmem.c and display it for another type of I/O,
one that never accesses the outside world, but stays in the computer.

main( )
int numbers[5], result[5], index;
char line[80];
number[0] = 74;
number[1] = 18;
number[2] = 33;
number[3] = 30;
number[4] = 97;
sprintf(line,%d %d      %d %d %d\n",numbers[0],numbers[1],
sscanf(line,"%d %d %d %d       %d",&result[4],&result[3],
for (index = 0;index < 5;index++)
printf("The final result is %d\n",result[index]);

In inmem.c, we define a few variables, then assign some values to the ones named "numbers"
for illustrative purposes and then use a "sprintf" function except that instead of printing the line
of output to a device, it prints the line of formatted output to a character string in memory. In
this case the string goes to the string variable "line", because that is the string name we inserted
as the first argument in the "sprintf" function. The spaces after the 2nd %d were put there to
illustrate that the next function will search properly across the line. We print the resulting string
an+find that the output is identical to what it would have been by using a "printf" instead of the
"sprintf" in the first place. You will see that when you compile and run the program shortly.

Since the generated string is still in memory, we can now read it with the function "sscanf". We
tell the function in its first argument that "line" is the string to use for its input, and the remaining
parts of the line are exactly what we would use if we were going to use the "scanf" function and
read data from outside the computer. Note that it is essential that we use pointers to the data
because we want to return data from a function. Just to illustrate that there are many ways to
declare a pointer several methods are used, but all are pointers. The first two simply declare
the address of the elements of the arrays, while the last three use the fact that "result", without
the accompanying subscript, is a pointer. Just to keep it interesting, the values are read back in
reverse order. Finally the values are displayed on the monitor.

9.13 Is That Really Useful?

It seems sort of silly to read input data from within the computer but it does have a real purpose.
It is possible to read data in using any of the standard functions and then do a format conversion
in memory. You could read in a line of data, look at a few significant characters, then use these
formatted input routines to reduce the line of data to internal representation. That would sure
beat writing your own data formatting routines.

9.14 Standard Error Output

Sometimes it is desirable to redirectthe outputfrom the standard output device to a file. However,
you may still want the error messages to go to the standard output device, in our case the monitor.
This next function allows you to do that in MS-DOS systems. Load and display special.c
for an example of this new function.

#include "/sys/stdio.h"
main( )
int index;
for (index = 0;index < 6;index++) {
printf("This line goes to standard output.\n");
fprintf(stderr,"This line goes to the error device.\n");
/* This can be tested with the MS-DOS errorlevel
   command in a batch file. The number returned
   is used as follows;
   (continue here if less than 4)
   (continue here if 4 or greater)

The program consists of a loop with two messages output, one to the standard output device and
the other to the standard error device. The message to the standard includes the device name
"stderr" as the first argument. Other than those two small changes, it is the same as our standard
"printf" function. (You will see more of the "fprintf" function in the nextchapter, but its operation
fit in better as a part of this chapter.) Ignore the line with the "exit" for the moment, we will
return to it.

Compile and run this program, and you will find 12 lines of output on the monitor. To see the
difference, run the program again with redirected output to a file named "STUFF" by entering
the following line at a 1616/OS prompt;
F0/ special >stuff
More information about I/O redirection can be found in your 1616/OS Users Manual. This time
you will only get the 6 lines output to the standard error device, and if you look in your directory,
you will find the file named "STUFF" containing the other 6 lines, those to the standard output
device. You can use I/O redirection with any of the programs we have run so far, and as you
may guess, you can also read from a file using I/O redirection but we will study a better way to
read from a file in the next chapter.

9.15 What About The Exit(4) Statement?

Now to keep our promise about the exit(4) statement. Redisplay the file named special.c
on your monitor. The last statement simply exits the program and returns the value of 4 to
MS-DOS. Any number from 0 to 9 can be used in the parentheses for DOS communication. If
you are operating in a BATCH file, this number can be tested with the "ERRORLEVEL"
command. Note that these characteristics only apply to MS-DOS.
Most compilers that operate in several passes return a 1 with this mechanism to indicate that a
fatal error has occurred and it would be a waste of time to go on to another pass resulting in
even more errors.

It is therefore wise to use a batch file for compiling programs and testing the returned value for

9.16 Programming Exercise

1. Write a program to read in a character using a loop, and display the character in its normal
"char" form. Also display it as a decimal number. Check for a dollar sign to use as the stop
character. Use the "getch" form of input so it will print immediately. Hit some of the special
keys, such as function keys, when you run the program for some surprises. You will get two
inputs from the special keys in MS-DOS systems, the first being a zero which is the indication
to the system that a special key was hit. This won’t happen with the 1616/OS. Function keys
do return leading zeros (since this is done by the microprocessor actually inside the keyboard),
however the 1616/OS keyboard driver does not pass them along to the program. The way to
getaround this is to use syscall .29to redefine function keys, so that they pass whatever character
string you desire to the program. See the file vc.c function initfk() for a clean example of this.

next lesson 

C LANGUAGE TUTORIAL 10.file Input/Output

App creators your key for sucsses

If you can click a mouse, you can
profit from online video!
Recent news articles show how some YouTubers are earning upwards of $100,000 per year from the ads they display on and around their videos...

What if you could earn money from those ads, without actually creating the content?


All you need to know in one place

Small step for huge knowledge
This is the place you learn everything you need to move forward and become the leader in programming and creating mobile apps.

No need to be an expert anyone can start from scratch !

You just need to find the area that interests you and access, from there the way to success is up to you .

Good Luck!

 Click Here App Creators

App creators


i want to know

hello everyone

i wanted to ask from the viewers

if ther any subject thet you read and want continue reading and its not completed yet please let me know by comment or email

or if ther any subject thet you want me to add pls let me know