
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What are reading and writing characters in C language?
In C programming language the reading and writing characters are as follows −
The simplest of the console I/O functions are getche (), which reads a character from the keyboard, and putchar (), which prints a character to the screen.
The getche () function works on until a key is pressed and then, returns its value. The key pressed is also echoed to the screen automatically.
The putchar () function will write its character argument to the screen at the current cursor position.
The declarations for getche () and putchar () are −
int getche (void); int putchar (int c);
The header file for getche () and putchar () is in CONIO.H.
Example
Here is an example which reads characters from the keyboard and prints them in reverse case. This means that the uppercase prints as lowercase and the lowercase prints as uppercase.
The program halts whenever a period is typed. The header file CTYPE.H is required by the islower() library function, which returns true if its argument is lowercase and false if it is not.
Following is the C program for the reading and writing characters −
# include <stdio.h> # include <conio.h> # include <ctype.h> main(void){ char ch; printf (“enter chars, enter a period to stop
”); do{ ch = getche (); if ( islower (ch) ) putchar (toupper (ch)); else putchar (tolower (ch)); } while (ch! = ‘.’); /* use a period to stop */ return 0; }
Output
When the above program is executed, it produces the following result −
enter chars, enter a period to stop tTuUtToOrRiIaAlLsS..
There are two important variations on getche(), which are as follows −
The first one is as follows −
The trouble with getchar() is that it buffers input until a carriage return is entered.
The getchar() function uses the STDIO.H header file.
The second one is as follows −
A second, more useful, variation on getche() is getch(), which operates precisely like getche () except that the character you type is not echoed to the screen. It uses the CONIO.H header.
- Related Articles
- Reading and writing binary file in C/C++
- Reading and Writing to text files in C#
- Reading and Writing Files in Python
- Reading and Writing Files in Perl
- Reading and Writing to text files in Python
- Reading and Writing CSV File using Python
- Reading and Writing to text files in Python Program
- Reading/Writing a MS Word file in PHP
- Reading and writing Excel files using the openpyxl module in Python
- Print * in place of characters for reading passwords in C
- Assigning multiple characters in an int in C language
- What are nested structures in C language?
- What are string literals in C language?
- What are macros in C programming language?
- What are executable statements in C language?
