Computer Programming 简明教程
Computer Programming - Basic Syntax
让我们从一些编码开始,这将真正让你成为一名计算机程序员。我们要编写一个单行计算机程序,在屏幕上写 Hello, World! 。让我们看看如何使用不同的编程语言编写它。
Let’s start with a little coding, which will really make you a computer programmer. We are going to write a single-line computer program to write Hello, World! on your screen. Let’s see how it can be written using different programming languages.
Hello World Program in C
使用 www.compileonline.com 中提供的我们的在线编译器选项尝试以下示例。
Try the following example using our online compiler option available at www.compileonline.com.
对于本教程中给出的大多数示例,您将在我们网站代码部分的右上角找到一个 Try it 选项,它将带您到在线编译器。
For most of the examples given in this tutorial, you will find a Try it option in our website code sections at the top right corner that will take you to the online compiler.
尝试更改 printf() 内部的内容,即在 Hello World! 的位置键入任何内容,然后查看其结果。它只是打印双引号内保留的任何内容。
Try to change the content inside printf(), i.e., type anything in place of Hello World! and then check its result. It just prints whatever you keep inside the two double quotes.
#include <stdio.h>
int main() {
/* printf() function to write Hello, World! */
printf( "Hello, World!" );
}
产生以下结果 −
which produces the following result −
Hello, World!
这个小小的 Hello World 程序将帮助我们理解与 C 编程相关的各种基本概念。
This little Hello World program will help us understand various basic concepts related to C Programming.
Program Entry Point
现在,先不要管 #include <stdio.h> 语句,但请记住你必须将此语句放在 C 程序的顶部。
For now, just forget about the #include <stdio.h> statement, but keep a note that you have to put this statement at the top of a C program.
每个 C 程序都以 main() 开头,称为 main 函数,然后是左花括号。其余程序指令写在其中,最后用右花括号结束程序。
Every C program starts with main(), which is called the main function, and then it is followed by a left curly brace. The rest of the program instruction is written in between and finally a right curly brace ends the program.
这两个花括号内的编码部分称为程序主体。左花括号可以与 main() { 在同一行,也可以在下一行,就像它在上述程序中提到的那样。
The coding part inside these two curly braces is called the program body. The left curly brace can be in the same line as main(){ or in the next line like it has been mentioned in the above program.
Functions
函数是程序的小单元,用于执行特定任务。例如,上述程序使用了两个函数: main() 和 printf() 。这里,main() 函数为程序执行提供入口点,而另一个函数 printf() 用于在计算机屏幕上打印信息。
Functions are small units of programs and they are used to carry out a specific task. For example, the above program makes use of two functions: main() and printf(). Here, the function main() provides the entry point for the program execution and the other function printf() is being used to print an information on the computer screen.
你可以编写我们将在单独章节中看到的自己的函数,但 C 编程本身提供了各种内置函数,如 main()、printf() 等,我们可以根据需要在程序中使用它们。
You can write your own functions which we will see in a separate chapter, but C programming itself provides various built-in functions like main(), printf(), etc., which we can use in our programs based on our requirement.
一些编程语言使用 sub-routine 一词而不是函数,但其功能或多或少相同。
Some of the programming languages use the word sub-routine instead of function, but their functionality is more or less the same.
Comments
一个 C 程序可以拥有 / ….. / 内部的语句。此类语句称为注释,这些注释用于使程序对用户友好且易于理解。注释的好处是编译器和解释器完全忽略它们。因此,你可以使用任何语言来编写注释。
A C program can have statements enclosed inside /…../. Such statements are called comments and these comments are used to make the programs user friendly and easy to understand. The good thing about comments is that they are completely ignored by compilers and interpreters. So you can use whatever language you want to write your comments.
Whitespaces
当我们使用任何编程语言编写程序时,我们会使用各种可打印字符来准备编程语句。这些可打印字符是 a, b, c,……z, A, B, C,…..Z, 1, 2, 3,…… 0, !, @, #, $, %, ^, &, *, (, ), -, _, +, =, \, |, {, }, [, ], :, ;, <, >, ?, /, \, ~. `. ", ' 。希望我没有遗漏键盘上的任何可打印字符。
When we write a program using any programming language, we use various printable characters to prepare programming statements. These printable characters are a, b, c,……z, A, B, C,…..Z, 1, 2, 3,…… 0, !, @, #, $, %, ^, &, *, (, ), -, _, +, =, \, |, {, }, [, ], :, ;, <, >, ?, /, \, ~. `. ", '. Hope I’m not missing any printable characters from your keyboard.
除了这些字符之外,还有一些我们经常使用的字符,但它们在程序中是不可见的,这些字符是空格、制表符(\t)、换行符(\n)。这些字符称为 whitespaces 。
Apart from these characters, there are some characters which we use very frequently but they are invisible in your program and these characters are spaces, tabs (\t), new lines(\n). These characters are called whitespaces.
这三个重要的空白字符在所有编程语言中都普遍存在,但它们在文本文档中是不可见的,
These three important whitespace characters are common in all the programming languages and they remain invisible in your text document −
Whitespace |
Explanation |
Representation |
New Line |
To create a new line |
\n |
Tab |
To create a tab. |
\t |
Space |
To create a space. |
empty space |
只包含空白字符的一行,可能加上注释,称为空行,而 C 编译器会完全忽略它。空白是 C 中用于描述空格、制表符、换行符和注释的术语。因此,您可以按照下面所示编写 printf("Hello, World!" ); 。此处,“Hello, World!”周围创建的所有空格都是无用的,编译器会在编译时忽略它们。
A line containing only whitespace, possibly with a comment, is known as a blank line, and a C compiler totally ignores it. Whitespace is the term used in C to describe blanks, tabs, newline characters, and comments. So you can write printf("Hello, World!" ); as shown below. Here all the created spaces around "Hello, World!" are useless and the compiler will ignore them at the time of compilation.
#include <stdio.h>
int main() {
/* printf() function to write Hello, World! */
printf( "Hello, World!" );
}
产生以下结果 −
which produces the following result −
Hello, World!
如果我们让所有这些空白字符可见,那么上面的程序看起来会像这样,您将无法编译它:
If we make all these whitespace characters visible, then the above program will look like this and you will not be able to compile it −
#include <stdio.h>\n
\n
int main()\n
{
\n
\t/* printf() function to write Hello, World! */
\n
\tprintf(\t"Hello, World!"\t);\n
\n
}\n
Semicolons
C 程序中的每个单独语句都必须以分号 ( ; ) 结尾,例如,如果您想写两次“Hello, World!”,那么它将被写为以下形式:
Every individual statement in a C Program must be ended with a semicolon (;), for example, if you want to write "Hello, World!" twice, then it will be written as follows −
#include <stdio.h>
int main() {
/* printf() function to write Hello, World! */
printf( "Hello, World!\n" );
printf( "Hello, World!" );
}
此程序将产生以下结果:
This program will produce the following result −
Hello, World!
Hello, World!
这里,我们在第一个 printf() 函数中使用了一个新行字符 \n 来创建一个新行。让我们看看如果不使用这个新行字符会发生什么:
Here, we are using a new line character \n in the first printf() function to create a new line. Let us see what happens if we do not use this new line character −
#include <stdio.h>
int main() {
/* printf() function to write Hello, World! */
printf( "Hello, World!" );
printf( "Hello, World!" );
}
此程序将产生以下结果:
This program will produce the following result −
Hello, World! Hello, World!
我们将在接下来的几章中学习标识符和关键字。
We will learn identifiers and keywords in next few chapters.
Program Explanation
让我们了解一下上面 C 程序的工作原理。首先,上面的程序使用 C 编译器转换为二进制格式。所以,让我们将此代码放入 test.c 文件中并按照如下方式编译它:
Let us understand how the above C program works. First of all, the above program is converted into a binary format using C compiler. So let’s put this code in test.c file and compile it as follows −
$gcc test.c -o demo
如果有任何语法错误(在计算机术语中是语法错误),那么我们在将其转换为二进制格式之前会修复它。如果一切正常,那么它会生成一个名为 demo 的二进制文件。最后,我们按照如下方式执行生成的二进制 demo:
If there is any grammatical error (Syntax errors in computer terminologies), then we fix it before converting it into binary format. If everything goes fine, then it produces a binary file called demo. Finally, we execute the produced binary demo as follows −
$./demo
产生以下结果 −
which produces the following result −
Hello, World!
这里,当我们执行二进制文件 a.out 时,计算机从 main() 开始进入程序内部并遇到 printf() 语句。请注意 / …. / 内的这一行是一条注释,并且会在编译时进行筛选。因此,printf() 函数指示计算机在计算机屏幕上打印给定的行。最后,它会遇到一个右大括号,表示 main() 函数结束并退出程序。
Here, when we execute the binary a.out file, the computer enters inside the program starting from main() and encounters a printf() statement. Keep a note that the line inside /…./ is a comment and it is filtered at the time of compilation. So printf() function instructs the computer to print the given line at the computer screen. Finally, it encounters a right curly brace which indicates the end of main() function and exits the program.
Syntax Error
如果不遵循编程语言定义的规则,那么在编译时,您会收到语法错误,程序将不会被编译。从语法角度来看,即使单个句点、逗号或分号都很重要,您也应该注意这些小语法。在下面的示例中,我们跳过了一个分号,让我们尝试编译程序:
If you do not follow the rules defined by the programing language, then at the time of compilation, you will get syntax errors and the program will not be compiled. From syntax point of view, even a single dot or comma or a single semicolon matters and you should take care of such small syntax as well. In the following example, we have skipped a semicolon, let’s try to compile the program −
#include <stdio.h>
main() {
printf("Hello, World!")
}
此程序将产生以下结果:
This program will produce the following result −
main.c: In function 'main':
main.c:7:1: error: expected ';' before '}' token
}
^
所以,底线是,如果您在程序中没有遵循编程语言定义的适当语法,那么您会收到语法错误。在尝试另一个编译之前,您需要修复它们,然后继续。
So the bottom-line is that if you are not following proper syntax defined by the programming language in your program, then you will get syntax errors. Before attempting another compilation, you will need to fix them and then proceed.
Hello World Program in Java
以下是使用 Java 编写的等效程序。此程序也将生成相同的结果 Hello, World! 。
Following is the equivalent program written in Java. This program will also produce the same result Hello, World!.
public class HelloWorld {
public static void main(String []args) {
/* println() function to write Hello, World! */
System.out.println("Hello, World!");
}
}
产生以下结果 −
which produces the following result −
Hello, World!
Hello World Program in Python
以下是使用 Python 编写的等效程序。此程序也将生成相同的结果 Hello, World! 。
Following is the equivalent program written in Python. This program will also produce the same result Hello, World!.
# print function to write Hello, World! */
print "Hello, World!"
产生以下结果 −
which produces the following result −
Hello, World!
希望您注意到,对于 C 和 Java 示例,我们首先编译程序,然后执行生成的二进制文件,但在 Python 程序中,我们直接执行它。正如我们在前面的章节中解释的那样,Python 是解释性语言,不需要称为编译的中间步骤。
Hope you noted that for C and Java examples, first we are compiling the programs and then executing the produced binaries, but in Python program, we are directly executing it. As we explained in the previous chapter, Python is an interpreted language and it does not need an intermediate step called compilation.
Python 不需要分号 (;) 来终止语句,而是新的一行总是表示语句的终止。
Python does not require a semicolon (;) to terminate a statement, rather a new line always means termination of the statement.