Friday, September 23, 2011

C KEYBOARD INPUT AND SCREEN OUTPUT









REVISED: Sunday, March 15, 2015


CONTENTS:
I.    C KEYBOARD INPUT AND SCREEN OUTPUT INTRODUCTION
II.  READING FROM THE KEYBOARD
III. WRITING TO THE CONSOLE
IV. C KEYBOARD INPUT AND SCREEN OUTPUT REFERENCES

YOU WILL LEARN HOW TO:
1. Read from the keyboard.
2. Write to the console.

I. C KEYBOARD INPUT AND SCREEN OUTPUT INTRODUCTION


Welcome to the “C Keyboard Input and Screen Output Tutorial.”


During this tutorial we will discuss the library functions C uses for keyboard input and screen output. The prototypes for these functions are in the header file conio.h.


C++ includes the entire C language; therefore, all C programs, with a few minor exceptions, are also C++ programs.

The C code shown in the tutorial were written using the Microsoft Visual C++ Express Edition Integrated Development Environment (IDE) and the “C99 subset.”

II. READING FROM THE KEYBOARD


The following are basic keyboard functions:

getchar( ) reads a character from the keyboard; waits for a carriage return. Its prototype is:

int getchar(void);


getche( ) reads a character from the keyboard with echo to the screen; does not wait for carriage return. Its prototype is:

int getche(void);


getch( ) reads a character from the keyboard without echo to the screen; does not wait for carriage return. Its prototype is:

int getch(void);


gets( ) reads a string from the keyboard. Its prototype is:

char *gets(char *str);

str is a character array.


scanf( ) reads data from the keyboard. Its prototype is:

int scanf(const char *control_string, …);


The scanf( ) control string contains three parts. First, format specifiers. Second, white-space characters. Third, non-white-space characters.


Each scanf( ) format specifier is a %, followed by a format code. Each argument is matched with a format specifier in order from left to right.


The following are examples of scanf( ) format specifiers:

%c  Read a single character.
%d  Read a decimal integer.
%i  Read an integer in either decimal, hexadecimal or octal format.
%e  Read a floating point number.
%f  Read a floating point number.
%g  Read a floating point number.
%o  Read an octal number.
%s  Read a string.
%x  Read a hexadecimal number.
%p  Read a pointer.
%n  Receives an integer value equal to the number of characters read so far.
%u  Read an unsigned decimal integer.
%[ ]  Scan for a set of characters.
%%  Read a % sign.


A white-space character in the scanf( ) control string causes scanf( ) to skip over one or more leading white-space characters; e.g., space, tab, vertical tab, form feed, or newline in the input stream.


A non-white-space character in the scanf( ) control string causes scanf( ) to read and discard matching characters in the input stream.

III. WRITING TO THE CONSOLE

The following are basic console functions:


putchar( ) writes a character to the screen. Its prototype is:

int putchar(int c);


puts( ) writes a string to the screen. Its prototype is:

int puts(const char *str);


printf( ) writes data to the console. Its prototype is:

int printf(const char *control_string, …);


The printf( ) control string is in two parts. The first part consists of characters that will be printed to the screen. The second part is format specifiers which define the display of the arguments.


Each printf( ) format specifier is a %, followed by a format code. Each argument is matched with a format specifier in order from left to right.


The following are examples of printf( ) format specifiers:

%c  Character
%d  Signed decimal integers
%i  Signed decimal integers
%e  Scientific notation lowercase e
%E  Scientific notation uppercase e
%f  Decimal floating point
%g  Uses %e or %f, whichever is shorter
%G  Uses %E or %F, whichever is shorter
%o  Unsigned octal
%s  String of characters
%u  Unsigned decimal integers
%x  Unsigned hexadecimal lowercase letters
%X  Unsigned hexadecimal uppercase letters
%p  Displays a pointer
%n  The associated argument must be a pointer to an integer. This specifier causes the number of  characters written so far to be put into that integer.
%%  Prints a % sign

IV. C KEYBOARD INPUT AND SCREEN OUTPUT REFERENCES


The C Programming Language by Brian Kernighan and Dennis Ritchie (Englewood Cliffs, N.J.: Prentice-Hall, 1978).


C++: The Complete Reference, Fourth Edition by Herbert Schildt (Berkeley, California: McGraw-Hill/Osborne, 2003).

YOU HAVE LEARNED HOW TO:
1. Read from the keyboard.
2. Write to the console.

Elcric Otto Circle





--> --> -->







How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"






No comments:

Post a Comment