Page 1 of 1

C KEYBOARD INPUT AND SCREEN OUTPUT TUTORIAL C KEYBOARD INPUT AND SCREEN OUTPUT TUTORIAL Rate Topic: -----

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 102
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 20 April 2010 - 08:24 AM

C KEYBOARD INPUT AND SCREEN OUTPUT TUTORIAL


CONTENTS
I. INTRODUCTION
II. READING FROM THE KEYBOARD
III. WRITING TO THE CONSOLE
IV. REFERENCES

WHAT YOU WILL LEARN IN THIS TUTORIAL

1. You will learn how to read from the keyboard.

2. You will learn how to write to the console.

I. INTRODUCTION

Hello; nice to meet you. 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++ 2008 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. 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).


Is This A Good Question/Topic? 0
  • +

Replies To: C KEYBOARD INPUT AND SCREEN OUTPUT TUTORIAL

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Posted 24 April 2010 - 02:11 PM

Just wanted to tell you how bad gets can be.
Was This Post Helpful? 0
  • +
  • -

#3 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 102
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 25 April 2010 - 01:47 AM

Hello Sarmanu,

You are absolutely correct!

I should have included a warning in the tutorial that gets() is deprecated and unsafe.

In order to provide backwards compatibility, gets() was not removed from the C standard.

gets() is unsafe because gets() does not perform a boundary check on the array receiving the input. Therefore, it is possible for the user to enter more characters than the array can hold. gets() should never be used in commercial code. You should always use fgets() or getline() instead.

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1