C++ Of Hello World
C Linguistics is in a hurry , And they started to make contact C++, Which leads to the following content can not be well grasped . Now decide to put something down , Review from the beginning C++.
The compiler I chose is C-free5.0, Because it took a long time , I don't want to change it .
By convention , Every programmer learns a new language , The first program to always write is Hello World!
Here is the code :
#include
using namespace std;
int main()
{
cout<<“Hello World”<<endl;
return 0;
}
This involves C++ And C The first difference in language , Even though C++ yes C Superset of , Most C Language in C++ Can be supported by China . But because it's a review C++, So I didn't use the header file <stdio.h>, It's a choice iosteam Library files ,iostream Means iostream library .iostream I / O stream , Directly speaking in( input )
out( output ) stream( flow ), take in,out And stream synthesis .
#include It doesn't need to be like C Same language , add .h, This is in C++ It is also reflected in other libraries .
The next step is using namespace
std; This sentence is actually using Compile instructions , Why compile instructions are used , Because C++ Designed considering the compatibility of the program on different products , When our preprocessed library function does not .h Time , To use using instructions , If any , This command is not required .
The above is equivalent to the following code :
#include
//using namespace std;
int main()
{
std::cout<<“Hello World”<<std::endl;
return 0;
}
We can see , Not used using Compiling instructions can also be completed , But it needs to be added before each instruction std::, It's not worth it . In addition, I put using The command is on main Outside function , So the scope is global , You can also put it in main In function .
#include
int main()
{
using namespace std;
cout<<“Hello World”<<endl;
return 0;
}
But in that case , Only for main In function , If you use a subfunction , You need to use it again at the beginning of each subfunction using instructions .
cout It's a C++ New concept in , stay C In language , We passed printf Equal function to output , and C++ The concept of flow is introduced ( Actually scanf Function runtime , This concept is also reflected in the dark ).
cout<< Add the element you want to output , Can be output by intelligence , No need to look like C Linguistic printf Function declared that way .
endl yes C++ A new introduced element of , Equivalent to newline . Here are some equivalent codes :
printf(“Hello World\n”);
cout<<“Hello World\n”;
cout<<“Hello World”<<endl;
cout<<“Hello”<<" "<<“World”<<endl;
Technology
Daily Recommendation