本文共 5214 字,大约阅读时间需要 17 分钟。
source: Github
资料来源:Github
#includeusing namespace std;int main(){ cout << "Hello World" << endl; return 0;}
Hello World!
Now, let’s break down the code:
现在,让我们分解一下代码:
#includeusing namespace std;
iostream
stands for input-output stream . The “iostream” file contains code for allowing the computer to take input and generate an output, using the C++ language.
iostream
代表输入输出流。 “ iostream”文件包含用于允许计算机使用C ++语言进行输入并生成输出的代码。
The second line tells the computer to use the standard namespace which includes features of standard C++. You could write this program without this line, but you’d have to use std::cout
instead of cout
and std::endl
instead of endl
on line 4. It makes the code more readable and our lives as programmers easier.
第二行告诉计算机使用标准名称空间,其中包括标准C ++的功能。 您可以在没有此行的情况下编写此程序,但是必须在第4行上使用std::cout
代替cout
和std::endl
而不是endl
。这使代码更易读,并且我们更容易成为程序员。
int main(){
C++ starts execution of a program from the -main function- int main()
. During execution , the computer starts running the code from every line from {
(opening bracket) till }
(closing bracket) NOTE : Every function starts with an opening curly brace ”{” and ends with a closing curly brace ”}“.
C ++从-main function- int main()
开始执行程序。 在执行过程中,计算机从{
(右括号)到}
(右括号)的每一行开始运行代码。 注意:每个函数都以一个大括号“ {”开头,以一个大括号“}”结尾。
cout << "Hello World" << endl; return 0;}
The word cout
in C++ is used to output.
C ++中的单词cout
用于输出。
It is followed by <<
, the insertion operator .
其后是插入运算符 <<
。
Whatever is in the double quotes ""
is printed . Certain special characters have a different syntax for print statements
双引号中的任何内容都将打印""
。 某些特殊字符的打印语句语法不同
Now to print any other kind of data , you have to add <<
.
现在要打印任何其他类型的数据,您必须添加<<
。
Challenge: Try to change Hello World to any other sentence or word(s). What will be the output ?
挑战:尝试将“ Hello World”更改为任何其他句子或单词。 输出是什么?
endl
is a reserved word when using the C++ language to end this line and go to the next line during output . - cout stands for “console output”
当使用C ++语言结束此行并在输出期间转到下一行时, endl
是保留字。 -cout代表“控制台输出”
Finally, finish the command with a semicolon ;
.
最后,以分号结束命令;
。
NOTE : Every command except the main function definition and the #include directive needs to be ended by the semicolon. Without a ”;” , you may encounter an error.
注意:除主函数定义和#include指令以外的所有命令都必须以分号结尾。 没有 ”;” ,您可能会遇到错误。
return 0;
safely terminates the current function i.e. ‘main()’ in this case and since no function follows after ‘main()’ the program is terminated.
return 0;
在这种情况下,安全地终止当前函数,即“ main()”,并且由于在“ main()”之后没有函数跟随程序终止。
Don’t forget to tell the computer that this is end of the main() function. To do this , you add the closing curly brace ”}“. You will encounter an error before program execution if you do not include the } .
不要忘记告诉计算机这是main()函数的结尾。 为此,可以添加右花括号“}”。 如果不包含},则会在程序执行前遇到错误。
Programmers use a Hello World program (like this one) as a ritual on using a new programming language. It is a symbol of good luck.You have finished coding your first C++ program and have understood most of the code you have written/typed. CONGRATULATIONS!
程序员使用Hello World程序(如该程序)作为使用新编程语言的一种习惯。 这是好运的象征。 您已经完成了第一个C ++程序的编码,并且已经理解了您编写/键入的大多数代码。 恭喜!
Good Luck to all of you and happy coding! :)
祝大家好运,并祝您编程愉快! :)
Happy Coding ! :)
快乐编码! :)
Feel free to ask any questions on FreeCodeCamp’s GitHub page or
随时在FreeCodeCamp的GitHub页面或上提问
You may need some software to write and execute C++ code. I recommend using CodeBlocks. There’s a download link below :
您可能需要一些软件才能编写和执行C ++代码。 我建议使用CodeBlocks。 下面有一个下载链接:
Download Link :
下载链接:
Other alternatives could be visual studio, using a compiler or an online IDE such as Cloud9 or repl.it
其他替代方案可以是Visual Studio,使用编译器或在线IDE(例如Cloud9或repl.it)。
Link #2 for Mac :
Mac的链接2:
翻译自:
转载地址:http://vlzzd.baihongyu.com/