版本6和7间的区别
于2007-07-15 22:21:36修订的的版本6
大小: 7302
编辑: czk
备注:
于2007-07-15 22:39:35修订的的版本7
大小: 7727
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 64: 行号 64:
下面对程序本身做些说明。一个C语言程序,无论其大小如何,都是由''函数''和''变量''组成的。函数中包含一些''语句'',以指定所要执行的计算操作;变量则用于存储计算过程中使用的值。C语言中的函数类似于Fortran语言中的子程序和函数,与Pascal语言中的过程和函数也很类似。在本例中,函数的名字为main。通常情况下,函数的命名没有限制,但main是一个特殊的函数——每个程序都从main函数的起点开始执行,这意味着每个程序都必须在某个位置包含一个maln函数。 下面对程序本身做些说明。一个C语言程序,无论其大小如何,都是由''函数''和''变量''组成的。函数中包含一些''语句'',以指定所要执行的计算操作;变量则用于存储计算过程中使用的值。C语言中的函数类似于Fortran语言中的子程序和函数,与Pascal语言中的过程和函数也很类似。在本例中,函数的名字为main。通常情况下,函数的命名没有限制,但main是一个特殊的函数——每个程序都从main函数的起点开始执行,这意味着每个程序都必须在某个位置包含一个main函数。
行号 67: 行号 67:
main will usually call other functions to help perform its job, some that you wrote, and others from libraries that are provided for you. The first line of the program,
{{{
   #include <stdio.h>
}}}
tells the compiler to include information about the standard input/output library; the line appears at the beginning of many C source files. The standard library is described in Chapter 7 and Appendix B.
main will usually call other functions to help perform its job, some that you wrote, and others from libraries that are provided for you. The first line of the program,{{{
#include <stdio.h>
}}}tells the compiler to include information about the standard input/output library; the line appears at the beginning of many C source files. The standard library is described in Chapter 7 and Appendix B.

main函数通常会调用其他函数来帮助完成某些工作,被调用的函数可以是程序设计人员自己编写的,也可以来自于函数库。上述程序段中的第一行语句{{{
#include <stdio.h>
}}}用于告诉编译器在本程序中包含标准输入/输出库的信息。许多C语言源程序的开始处都包含
这一行语句。我们将在第7章和附录B中对标准库进行详细介绍。

1.1 Getting Started 入门

The only way to learn a new programming language is by writing programs in it. The first program to write is the same for all languages:

学习一门新程序设计语言的惟一途径就是使用它编写程序。对于所有语言的初学者来说,编写的第一个程序几乎都是相同的,即:

Print the words

hello, world

请打印出下列内容【czk注:这里print(打印)的意思就是指在屏幕上显示】

hello, world

This is a big hurdle; to leap over it you have to be able to create the program text somewhere, compile it successfully, load it, run it, and find out where your output went. With these mechanical details mastered, everything else is comparatively easy.

尽管这个练习很简单,但对于初学语言的人来说,它仍然可能成为一大障碍,因为要实现这个目的,我们首先必须编写程序文本,然后成功地进行编译,并加载、运行,最后输出到某个地方。掌握了这些操作细节以后,其他事情就比较容易了。

In C, the program to print "hello, world" is

   1 #include <stdio.h>
   2 
   3 main()
   4 {
   5     printf("hello, world\n");
   6 }

在C语言中,我们可以用下列程序打印出“hello world”:

   1 #include <stdio.h>
   2 
   3 main()
   4 {
   5     printf("hello, world\n");
   6 }

Just how to run this program depends on the system you are using. As a specific example, on the UNIX operating system you must create the program in a file whose name ends in ".c", such as hello.c, then compile it with the command

cc hello.c

If you haven't botched anything, such as omitting a character or misspelling something, the compilation will proceed silently, and make an executable file called a.out. If you run a.out by typing the command

a.out

it will print

hello, world

On other systems, the rules will be different; check with a local expert.

如何运行这个程序取决于所使用的系统。这里举一个特殊的例子。在UNIX操作系统中,首先必须在某个文件中建立这个源程序,并以“.c”作为文件的扩展名,例如hello.c,然后再通过下列命令进行编译:

cc hello.c

如果源程序没有什么错误(例如漏掉字符或拼错字符)。编译过程将顺利进行,并生成一个可执行文件a.out。然后,我们输入:

a.out

即可运行a.out,打印出下列信息:

hello, world

在其他操作系统中,编译、加载、运行等规则会有所不同。

Now, for some explanations about the program itself. A C program, whatever its size, consists of functions and variables. A function contains statements that specify the computing operations to be done, and variables store values used during the computation. C functions are like the subroutines and functions in Fortran or the procedures and functions of Pascal. Our example is a function named main. Normally you are at liberty to give functions whatever names you like, but "main" is special - your program begins executing at the beginning of main. This means that every program must have a main somewhere.

下面对程序本身做些说明。一个C语言程序,无论其大小如何,都是由函数变量组成的。函数中包含一些语句,以指定所要执行的计算操作;变量则用于存储计算过程中使用的值。C语言中的函数类似于Fortran语言中的子程序和函数,与Pascal语言中的过程和函数也很类似。在本例中,函数的名字为main。通常情况下,函数的命名没有限制,但main是一个特殊的函数名——每个程序都从main函数的起点开始执行,这意味着每个程序都必须在某个位置包含一个main函数。

main will usually call other functions to help perform its job, some that you wrote, and others from libraries that are provided for you. The first line of the program,

#include <stdio.h>

tells the compiler to include information about the standard input/output library; the line appears at the beginning of many C source files. The standard library is described in Chapter 7 and Appendix B.

main函数通常会调用其他函数来帮助完成某些工作,被调用的函数可以是程序设计人员自己编写的,也可以来自于函数库。上述程序段中的第一行语句

#include <stdio.h>

用于告诉编译器在本程序中包含标准输入/输出库的信息。许多C语言源程序的开始处都包含这一行语句。我们将在第7章和附录B中对标准库进行详细介绍。

One method of communicating data between functions is for the calling function to provide a list of values, called arguments, to the function it calls. The parentheses after the function name surround the argument list. In this example, main is defined to be a function that expects no arguments, which is indicated by the empty list ( ).

#include <stdio.h>                 include information about standard library
main()                                          define a function called main
                                             that received no argument values
{                                   statements of main are enclosed in braces
    printf("hello, world\n");              main calls library function printf
                                         to print this sequence of characters
}                                         \n represents the newline character

The first C program

The statements of a function are enclosed in braces { }. The function main contains only one statement,

   printf("hello, world\n");

A function is called by naming it, followed by a parenthesized list of arguments, so this calls the function printf with the argument "hello, world\n". printf is a library function that prints output, in this case the string of characters between the quotes.

A sequence of characters in double quotes, like "hello, world\n", is called a character string or string constant. For the moment our only use of character strings will be as arguments for printf and other functions.

The sequence \n in the string is C notation for the newline character, which when printed advances the output to the left margin on the next line. If you leave out the \n (a worthwhile experiment), you will find that there is no line advance after the output is printed. You must use \n to include a newline character in the printf argument; if you try something like

   printf("hello, world
   ");

the C compiler will produce an error message.

printf never supplies a newline character automatically, so several calls may be used to build up an output line in stages. Our first program could just as well have been written

   #include <stdio.h>

   main()
   {
     printf("hello, ");
     printf("world");
     printf("\n");
   }

to produce identical output.

Notice that \n represents only a single character. An escape sequence like \n provides a general and extensible mechanism for representing hard-to-type or invisible characters. Among the others that C provides are \t for tab, \b for backspace, \" for the double quote and \\ for the backslash itself. There is a complete list in Section 2.3.

Exercise 1-1. Run the "hello, world" program on your system. Experiment with leaving out parts of the program, to see what error messages you get.

Exercise 1-2. Experiment to find out what happens when prints's argument string contains \c, where c is some character not listed above.

TCPL/1.01_Getting_Started (2008-02-23 15:36:44由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.