C + + Learning Note 1 (standard input and output)

Source: Internet
Author: User
Tags what operator

Objective:

Individuals have been relatively lazy, recently prepared to record their own learning C + + learning process, I hope that they can write a blog when you can persist, welcome everyone in the blog spending problems, good enough to say, I can persist. I'm going to be able to understand the differences and advantages of C + + in my blog by comparing it with the language, so let's go into the C + + door.

The standard output

First, the header file used in C + + for standard input and output is #include <iostream>, You may also find that direct use of this header file is not possible using the standard output cout, that is, cout is an object, type is ostream type, And iostream can be seen as a combination of IStream and ostream. and cout, CIN and other objects are defined in the standard namespace std, as to what is called the namespace, I will be in the follow-up of the course, we can now be understood as a scope can be.

Well, let's take a look at one of the simplest HelloWorld programs:

1#include <iostream>//standard input and output header file2 using namespaceStd//contains the standard namespace STD:<stdardard>3 4 /*5 The main function, like the main function in C, is the entry for a program to run,6 A program has and can only have one main function7 */8 intMainintargcChar**argv)9 {Ten     /*standard output in C + + is consistent with the printf function of C language*/ One     /*cout: Standard ostream type Object, Endl: and escape character ' \ n ' function similar*/ Acout <<"Hello World"<<Endl; - GetChar (); -     return 0; the}
View Code

Output Result:

Hello World

The simplest of a C + + program is finished, or simple, and C language to make a comparison, the actual gap is not very large, the first header file format, in C + + is not. h end, again to use the standard output using the Cout object, and C language using the System library function printf (), and using the Cout object in C + + must use the standard namespace STD, which is important to note, as well as overloading the operator << in C + +, which can be used for the output of an object, and as for not knowing what operator overloading is, you can probably think of this operator as having multiple functions.

Let's take a look at how to output variables in C + +, remember how we output in C, we use the formatted string of the printf library function to do the output, and output different data types with format placeholders, like this:

int%d

Short%d

Long%ld

Float%f

Double%LF

Char%c

Output variables in C + + is very simple, only need to use cout to output it, do not need to know what type (except the basic type, other types must be overloaded operator <<), let's see how to Output the variable

1#include <iostream>//standard input and output header file2 using namespaceStd//contains the standard namespace STD:<stdardard>3 4 intMainintargcChar**argv)5 {6     intVI = -;7     DoubleVD =3.14;8     CharVC ='Z';9     Ten     //outputs the values of three variables respectively Onecout <<"VI:"<< VI <<Endl; Acout <<"VD:"<< VD <<Endl; -cout <<"VC:"<< VC <<Endl; -      the     return 0; -}
View Code

Output Result:

 3.14 vc:z

is not compared with the language is much simpler, no, you have not read wrong, is so simple.

The standard input

  Actually understand the C + + standard output problem, again, the standard input is much simpler, should be for him and the standard output format when the same, just use CIN instead of cout as input. Or a simple example to look at:

1#include <iostream>2 3 using namespacestd;4 5 intMainintargcChar*argv[])6 {7     intVI = -;8     DoubleVD =3.14;9     CharVC ='Z';Ten  One     //outputs the values of three variables respectively Acout <<"VI:"<< VI <<Endl; -cout <<"VD:"<< VD <<Endl; -cout <<"VC:"<< VC <<Endl; the  -     //Modify the values of three variables -     //standard Read, the value of a variable can be read standard , -     //It is also possible to read multiple variables in succession note not to call Endl +Cin>>VI; -Cin>>vd>>VC; +  A     //outputs the values of three variables respectively atcout <<"VI:"<< VI <<Endl; -cout <<"VD:"<< VD <<Endl; -cout <<"VC:"<< VC <<Endl; -     return 0; -}
View Code

The output should be:

1  - 2 3.14 3 vc:z 4 3 5 3.14 a 6 3 7 3.14 8 Vc:a

The above is about the standard input and output problems in C + +, of course, about C + + input and output problems, but also about similar and C language formatting control issues, I will be the next chapter specifically on this piece of the issue to be discussed.

C + + Learning Note 1 (standard input and output)

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.