30 Aug 2013

5_Character_Input_Output_System_in_C_Language

Home page

Character Input & Ouput System


BACK                                                                                                                            NEXT



(read this if you want to continue understanding next articles.)

We are going to consider a family of related programs for processing character data. 
You will find that many programs are just expanded versions of the prototypes that we discuss here.
The model of input and output supported by the standard library is very simple. Text input or output, regardless of where it originates or where it goes to, is dealt with as streams of characters. 
A text stream is a sequence of characters divided into lines; each line consists of zero or more characters followed by a newline character. It is the responsibility of the library to make each input or output stream confirm this model; the C programmer using the library need not worry about how lines are represented outside the program.

The standard library provides several functions for reading or writing one character at a time, of which getchar and putchar are the simplest. 
Each time it is called, getchar reads the next input character from a text stream and returns that as its value. That is, after

   c = getchar(); the variable c contains the next character of input. The characters normally 

come from the keyboard. The function putchar prints a character each time it is called:

   
putchar(c); prints the contents of the integer variable c as a character, usually on the screen. 

Calls to putchar and printf may be interleaved; the output will appear in the order in which the calls are made.

4_Symbolic_Constants_C_Language

Home page

Symbolic Constants

BACK                                                                                                                            NEXT


 A final observation before we leave temperature conversion forever. Its bad practice to bury ``magic numbers'' like 300 and 20 in a program; they convey little information to someone who might have to read the program later, and they are hard to change in a systematic way. One way to deal with magic numbers is to give them meaningful names. 

A #define line defines a symbolic name or symbolic constant to be a particular string of characters:   #define name replacement list thereafter, any occurrence of name (not in quotes and not part of another name) will be replaced by the corresponding replacement text.

The name has the same form as a variable name: a sequence of letters and digits that begins with a letter. The replacement text can be any sequence of characters; it is not limited to numbers.

#include <stdio.h>
#define LOWER 0     /* lower limit of table */
#define UPPER 300   /* upper limit */
#define STEP   20    /* step size */
/* print Fahrenheit-Celsius table */
main() 
 {
int fahr;
for(fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}


 The quantities LOWER, UPPER and STEP are symbolic constants, not variables, so they do not appear in declarations. Symbolic constant names are conventionally written in upper case so they can ber readily distinguished from lower case variable names. Notice that there is no semicolon at the end of a #define line.

Pageviews