The basic course of C language (iii) input and output functions and control flow statements (6)

Source: Internet
Author: User
Tags printf

Two, Putchar (), Getch (), Getche (), and GetChar () functions
1. Putchar () function
Putchar () function is to output a character to a standard output device in the form of: Putchar (CH);
Where Ch is a character variable or constant. The
Putchar () function is equivalent to printf ("%c", ch);
Example 6:
#include <stdio.h>
Main ()
{
Char c: /* define character variable/
c= ' B ';  /* assign value to character variable/
Putchar (c);  /* output the character */
Putchar (' \x42 ');  /* output Letter b*/
Putchar (0x42);  /* output the letter b*/
} directly with ASCII code value The
output function statements from four consecutive characters in this example can distinguish different assignment methods for character variables.
2. Getch (), Getche (), and GetChar () functions
(1) getch () and Getche () functions
Both functions read a character from the keyboard. The calling format is:
Getch ();
Getche (); The difference between the
is that the Getch () function does not echo the read Word to the display screen, while the Getche () function echo the read Word to the display screen.
Example 7:
#include <stdio.h>
Main ()
{
char c, ch;
C=getch ();  /* reads a character from the keyboard without echoing back to the character variable c*/
Putchar (c);  /* output the character/
Ch=getche ();  /* A character is sent back from the keyboard to the character variable ch*/
Putchar (CH);
}
uses the features of ECHO and no echo, which are often used to perform functions such as pausing during interactive input.

Example 8:
#include <stdio.h>
Main ()
{
Char c, s[20];
printf ("Name:");
Gets (s);
printf ("Press any key to confinue ...");
Getch (); /* Wait to enter any key
}

(2) GetChar () function
The GetChar () function also reads a character from the keyboard and brings it back. It differs from the previous two functions in that the GetChar () function waits for input until it is pressed to enter, and all input characters are displayed on the screen one by one before the carriage return. But only the first character is the return value of the function.
The call format for the GetChar () function is:
GetChar ();
Example 9:
#include <stdio.h>
Main ()
{
char c;
C=getchar (); /* Read the character from the keyboard until the carriage return is over
Putchar (c); /* Display the first character of the input/*
Getch (); * * Waiting to press Ning * *
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.