Let's take a look at C + + one. Say hello to the world

Source: Internet
Author: User
Tags ming

If you say to a programmer, "hello! "Then he would probably return a sentence" world! ”。 This is because for countless programmers, the first program they touch, even the first program in every language, is output "Hello world! ”。 No matter how zimingqinggao the procedural ape, it will make a deep impression on this sentence. Therefore, I will inevitably follow the large forces, to introduce you how to use C + + to output a "Hello world! ”。

First, let's look at a piece of code first.

 1  #include <iostream>2  using  namespace   Std;  3  int   Main ()  5  { "  hello world!\n    7  return  0  ;  8 } 

If you've been in touch with programming before, you might have understood the basics of C + + after reading this code, but if you're a pure novice, you might be confused, what the heck is.

Anyway, don't worry, let me give you the first line to start slowly.

We first translate the first sentence into Chinese: #包括 < input/output stream >.

First of all, the beginning of this # is certain to have, as for why, I can only say that this is a norm. Then the following include <iostream>,iostream is a library, like a warehouse, he has a variety of things inside, and iostream this library, is placed in the program input, and to the program out of the way, When we need to input or output, we will go to the warehouse to see, there is no way to input and output. If we do not declare the use of this library in advance, then when we want to input the output, the program will not know how to enter the output. The include is used to indicate that the library is included in the program, that is, to use the library.

Then the second row using namespace Std; in Essential C + +, the author did not find a good way to explain. In order to prevent this tutorial from becoming a college school thing, I decided to make up an explanation, if the great God saw the father's explanation, but not to face.

First of all, using is used, you see the code, he is blue, and most of the things are black (under different circumstances, the color may change). Why is he blue, then? Because he is the reserved word left by C + +, he has a certain meaning, here, his purpose is to tell us that we want to use namespace Std.

and namespace std refers to an area, namespace is the city, STD is the name of the city, like we said Haidian District, Licheng District, but in C + +, we want to call the District Haidian, District Licheng. Each city has a pile of warehouses, different urban warehouses, iostream this warehouse is in the Std city. So, after explaining which library to use, we also need to explain where he is in the city so that the program can find the repository. Std area, there are a lot of warehouses inside, there are some very common things, so we call the library is the standard library. The right name for namespace is called namespaces.

Why should these libraries be placed separately, because this can avoid the application within the two different things have the same name, like in the class named, a class there are two Xiao Ming, then there is no way to distinguish directly, we had to two Xiao Ming to parse, so we put one of the small Ming throw into another class, There would be no such conflict. Namespaces are like a wall of walls erected between many names.

There are, of course, some other ways to point out the namespace, but this is the simplest.

Next, we say the fourth line, we first look at main, this main is the main function of the meaning, C + + to the program is divided into a function, main is the most important function, what a program needs to do, what should be done, are written in this function. and the int in front of him refers to the data type of the function. Since he is a function there must be a function value, C + + function value can be a number can be a character, int is a number format, his data range is -2^ (32-1) ~ 2^ (32-1)-1. The parentheses are not available now, you just need to remember to add this stuff, and in the next lesson I'll explain it.

We need to talk about why the data range is such a thing. We in real life, write a number need to occupy a certain space on the paper, in the computer storage also need to occupy a certain space. However, we can adjust the size of the digital footprint on paper, but in the computer, how much space is not so good adjustment. So we created a lot of data types, each type occupies a certain amount of space, so he can represent the range of numbers is also certain.

The five and eighth lines of {} are designed to frame the contents of the program and tell the program where you don't have to go anymore.

The beginning of line six cout is the output, he followed by << means the output "" inside the box of things, is to output something. But we will only use "" to box up a bunch of things, if you just want to frame a thing, it will use ". The thing behind//is a note, the program will ignore him, he exists only to let us understand.

The output of this thing is necessary to be carefully spoken.

cout << "3 + 4 =";

cout << 3 + 4;

cout << ' \ n ';

So he outputs 3 + 4 = 7 and then wraps. You find that the second output has no two quotes, which is why that? Try to see if I will scare you, the things in the quotation marks, the program generally will not care about, the direct output. Without parentheses, the program will look at whether he is a variable or an expression, and then output the value of the variable and the expression. So here comes the question, what is a variable (an expression)? A variable is the amount of a value that can change, like Y and X in y=x, and what we output is the value of x or Y. The expression is a formula, and the expression in C + + is rich in content. This will later be used to speak again. The last ' \ n ' means a newline. Output in the back of the general will have its special meaning, this is not the general situation, \ n means to change the line. The reason to do this is because some things can not be directly output, such as newline characters (' N ') tab (' t '), as well as quotation marks, \, such as the program will be misunderstood by the symbol. You can finish the output by adding \ in front.

The notation for the input is CIN >>. For example, CIN >> X is the value of reading into X. If we need to read the x, then we need to first explain the application to use the x variable, if x is a number, then we must precede him with an int x. int x has the same meaning as int main, but the x here is a variable and not a function name. Of course you can also try some other data types.

Finally we have only one return 0, I said before the function must have a function value, return 0 is the function of return 0 is the value of the functions, because we do not need to use the value of main, so you return a few can, but we will still habitually choose 0 as the return value. If the program does not return a 0, then the program is not executed successfully. In general, when you omit this sentence, he will automatically be added by the IDE.

Appendix: C + + basic data types

Type + meaning + Minimum storage (by 2 binary) is as follows:

BOOL Boolean type

Char character 8-bit

wchar_t Wide-character 16-bit

Short 16-bit

unsigned short unsigned 16-bit (unsigned type cannot represent a negative number, but a positive number can represent a greater range)

Long length 32 bit

unsigned long unsigned 32-bit

Long long double-length 64-bit

Unsigned long long unsigned double-long 64 is

Float single-precision floating-point 6 is a valid number (floating-point numbers can be understood as small numbers)

Double dual-precision floating-point 10-bit valid digits

Long double extended precision floating-point 10-bit valid digits

  

Let's take a look at C + + one. Say hello to the world

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.