HP 3000 Manuals

getchar [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation


HP C/iX Library Reference Manual

getchar 

Reads a character from the standard input stream stdin.

Syntax 

     include <stdio.h>
     int getchar (void);

Parameters 

None.

Return Values 

x             The character read from stdin.

EOF           Either an end-of-file was detected or an error occurred.

Description 

The getchar function reads one character from the standard input stream
stdin.

The getchar function returns the next character in the currently defined
stdin stream.  It returns an EOF on end-of-file or file read error.  The
getchar function is identical to getc (stdin).

Examples 

The following program reads stdin and echos the contents to stdout.  The
program ends when an end of file is encountered on stdin.

        #include <stdio.h>
        main()
        {
           int c;

           while((c = getchar()) != EOF)
                putchar(c);
           putchar('\n');
        }

The variable c is declared as an int type instead of char because sign
extension, bit shifting, and similar operations can cause unexpected
results with the char type.  EOF is defined as a negative number.  If EOF
is assigned to a char variable, and chars are not signed in the
implementation, the comparison for EOF will never be true.  Therefore,
all examples in this chapter use the int type.

The last putchar() statement in the program outputs a new line, so
further output appears at the beginning of a new line instead of at the
end of the last line of output.

The getchar and putchar functions are most useful in filters (programs
that accept and modify data before passing it on).  For example, the
following program puts parentheses around each vowel encountered in the
input:

        #include <stdio.h>
        main()
        {
           int c;

           while((c = getchar()) != '\n') {
              if(vowel(c)) {
                 putchar('(');
                 putchar(c);
                 putchar(')');

              }else
                  putchar(c);
          }
        }
        vowel(c)
        char c;
        {
           switch(c) {
              case 'a':
              case 'A':
              case 'e':
              case 'E':
              case 'i':
              case 'I':
              case 'o':
              case 'O':
              case 'u':
              case 'U':
                 return (1);
              default:
                 return (0);
          }
        }

The vowel test is placed in the function vowel; the program terminates
when it encounters a new line.

The getc and putc functions can behave exactly like the getchar  and
putchar functions by specifying the appropriate file pointer.  For
example,

        getc(stdin);

is identical to

        getchar();

and

        putc(c, stdout);

is identical to

        putchar(c);

Thus, the putc() call in the previous program can be stated as

        putchar(c);

without altering the behavior of the program.

See Also 

fread(), getc(), gets(), fscanf(), scanf(), putchar(), ANSI C 4.9.7.6,
POSIX.1 8.1



MPE/iX 5.0 Documentation