Basic input and output functions in C + + guidelines for use in _c language

Source: Internet
Author: User
Tags lowercase

In C language, the print function is used for output, and the scanf function is used to input. C + + retains the use of this language.
The general format of the scanf function is:

  SCANF (format control, output table column)

The general format of the printf function is
SCANF (format control, output table column)

  SCANF (format control, output table column)

"Example" uses scanf and printf functions for input and output.

#include <iostream>
using namespace std;
int main ()
{
 int A; float b; Char c;
 scanf ("%d%c%f", &a,&c,&b); Note that the address operator &
 printf ("a=%d,b=%f,c=%c\n", a,b,c) should be added before the variable name.
 return 0;
}

The operating conditions are as follows:

67.98↙ (This behavior input, the input of 3 data between the space between)
a=12,b=67.980003,c=a (This behavior output)

The integer 12 entered is given to the integer variable A, and the character ' a ' is given to the character variable c,67.98 to a single-precision variable B.

C + + also retains functions that are used to input and output single characters, which are easy to use. The most commonly used are getchar functions and Putchar functions.
Putchar function (character output function)

The role of the Putchar function is to output a character to the terminal. For example:

  Putchar (c);

It outputs the value of the character variable C.

"Example" outputs a single character.

#include <iostream>/or include header file stdio.h: #include <stdio.h>
using namespace std;
int main ()
{
 char a,b,c;
 A= ' B '; b= ' O '; c= ' Y '
 ; Putchar (a);p Utchar (b);p Utchar (c);p Utchar (' \ n ');
 Putchar ();p Utchar (;p);p Utchar (Utchar);
 return 0;
}

Run result is

BOY
BOY

You can see that you can output escape characters with Putchar, and Putchar (' \ n ') is to output a newline character so that the current position of the output moves to the beginning of the next line. The function of Putchar (66) is to convert 66 as ASCII code to character output, 66 is the ASCII code of the letter ' B ', so Putchar (66) Outputs the letter ' B '. The rest is similar. The 10 in Putchar (10) is the ASCII code of the newline character, and Putchar (10) outputs a newline character that acts as Putchar (' \ n ').

You can also output other escape characters, such as

    • Putchar (' \101 '); (The output character ' a ', octal 101 is ' a ' 's ASCII code)
    • Putchar (' \ '); (Output single quote character ')
    • Putchar (' \015 '); (Output carriage return, do not wrap, so that the current position of the output moved to the beginning of the line)

GetChar function (character input function)

The function is to enter a character from the terminal (or the system implicitly specifies the input device). The GetChar function has no arguments, and its general form is the value of the GetChar () function, which is the character obtained from the input device.

"Example" enter a single character.

#include <iostream>
using namespace std;
int main ()
{
 char C;
 C=getchar (); Putchar (C+32); Putchar (' \ n ');
 return 0;
}

At run time, if you enter capital ' a ' from the keyboard and press ENTER, the lowercase letter ' a ' is printed on the screen.

Note that GetChar () can only receive one character. The characters obtained by the GetChar function can be assigned to a character variable or integer variable, or not to any variable, as part of an expression. For example, line 5th can be replaced with the following line:

  Putchar (GetChar () +32);p Utchar (' \ n ');

Because GetChar () reads ' A ', ' a ' +32 is the ASCII code of the lowercase ' a ', so the Putchar function outputs ' a '. You do not have to define variable C at this time.

You can also use the cout output GetChar function to get the ASCII value of the character:

  Cout<<getchar ();

The output is an integer 97, because the ASCII code, which is actually a character read in GetChar (), is not assigned to a character variable, and the cout is output in integer form. If you change into

  cout<< (C=getchar ()); Set C defined as a character variable

The output is the letter ' a ' because the value of the character variable C is required.

You can see that the output and input characters with the Putchar and GetChar functions are very flexible and convenient, because they are functions that can appear in expressions, such as

  cout<< (C=getchar () +32);

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.