twitter
    Find out what I'm doing, Follow Me :)

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.