ABookOnC Chapter 1 - 1.3
òµ·ºƒÃ: ºÁÚòÚÀò†©[òÁ›ò©Þ] [òÈÚò©Õ] 2001-01-21 04:13:52
Chapter 1

An Overview of C

This chapter gives an overview of the C Programming language. A series of programs is presented, and the elements of each program are carefully explained.

òµ· ò–ÕóîºÊÔ C óÔú¡ÞŒ•‚º×Úº®ÍòÖ‚òÖ·òµÚ Œ®ÞòÜÔº¥… º‡·òÙ‹ò⁄ ºË⁄. óÔú¡ÞŒ•‚º×¤ºÓ⁄òµÚ òËÞºƒ‹òƒÈ óÕÚºÁÚŒ®  òÅÌŒ®ÞºÐÚòÖ·ò§ Œ‡©, Œ®ÀŒ®ÀòµÚ óÔú¡ÞŒ•‚º×¤òµÚ òÜÔòÅ̺Ó⁄òµ  òË©ò⁄ÑóÕÚŒ†Ì òÃ⁄º“ĺÐÚòÖ·ò§ÃºË⁄.

Experimentation and interaction are emphasized throughout the text. In this chapter, we emphasize how to use the basic input/output functions of C.

òË⁄óÙÚŒ‡… ò×ÑòÜ’òµ  textº¥… óŦóÕ·òÃÞ Œ®Õò¡®ºÐÚòÖ· ò§ÃºË⁄.(Œ•‚º÷‹ºËÈŒ„Ì òË⁄ò©Þ compile Œ‡… Œ•‚Œ†ÂòµÚ òË⁄óÖÉòµ· Œ®Õò¡®ºÐÚòÖ· ò§ÃºË⁄.)
òµ· ò–ÕóîòÙÐòÃÞ òÜ®ºƒ‹ºÊÔ CòµÚ Œ‚®º‡‚ óÕ¤òÈÚ(input/output functions)ºÓ⁄òµ· òÖ·ºÖ»Œ†Ì òÁ‹òÜ’ºÐÚòÖ·ò§ ºÊÔò§  Œ®Õò¡®óÕÞºË⁄.

Note carefully that all our C code also serves as C++ code and that all the ideas we discuss hold for c++ as well as for C. Of course, the C++ programmer has available a richer set of tools and techniques out of which programs can be constructed. (See Chapter 13, í ÞMoving from C to C++.í µ)

òË©ò⁄ÑóÕÚŒ†Ì ºÄ‚óÊ‚óÕ·ºµ…. òÜ®ºƒ‹òµÚ º“¤ºÓ© C ò‰ÔºÓÞºÊÔ, C++ code Œ•‚ºƒ‹Œ‡© CòÛ  º§Èò®‹Œ® ò§ º¡Þ C++òµÃ òÞ©ò§ ,º‡·òÈÚ(hold for) óÕÚŒ‚® òÞÃóÕ· ºÄ…òµÚóÕÚºÊÔ º“¤ºÓ© òÕÃòµ·ºÔÔòÖ·ºÓ⁄òµÚ òÙ›óÕ©òµÃ,(Œ¦‹òË⁄òµÃ)(serve as) ºÚÐóÕÞ óÕÞºË⁄.
º‹…º¡©, C++óÔú¡ÞŒ•‚º×Úº¤‚ºÊÔ ò§ÞòÙ‹ò§È òÈÚ ò×ȺÊÔ óÔú¡ÞŒ•‚º×¤ºÓ⁄òµÚ º®ÖòÙÐòÃÞ,(òÛ‚òÙÐ) óÒͺ¶ óÕÞ tools Œ‡… techniquesº¥… Œ®ÖºÊÔ Œ†Âòµ· Œ® ºÊ¥óÕÚºË⁄.

Except for Section 1.8, í ÞArrays, Strings, and Pointers,í µ on page 36, everyone should read all the material in this chapter. Any reader who has had experience with arrays, pointers, and files in some other language can read all the sections of this chapter to get a more complete overview of C.

36pageòÙÐ ò×ȺÊÔ1.8ò×¥ í Þº®®òÙ·ºÓ⁄, òÊ⁄óÊ‚º§ÀºÓ⁄, óÏ‹òµ‚óîºÓ⁄,í µ òµÃ ò©ÞòÛ‚óÕÚŒ‡©ºÊÔ, º“¤ºÓ© òµ·ºÓ⁄òµ  òµ· ò–ÕóîòµÚ º“¤ºÓ© òÝк£Ì(ò×к£Ì,Œ¦‹òÖòÜÔòÅÌ)º¥… òµ‰òÖ·òÕ…º§Ì óÕÞºË⁄.
òÖ·ºÖ⁄ ºÏÄò×кÓ⁄òÙÐŒ†Ì ò×ÈòÖ·òÃÞºÊÔ, Œ•‚º÷‹ºËÈŒ„Ì º®®òÙ·, óÏ‹òµ‚óî, óÌÌòµ…òÙÐ ºÌ óÕ·òÃÞ òÖ·ºÖ⁄ ºË⁄º¥‚ languageòÕÈòÙÐòÃÞòµÚ Œ†‰óÙÚòµÃ òµ·ºfl‚ Œ® ò§ Œ‡© ò×ȺÍÚ ºÏÄò×кÓ⁄òµ  C òµÚ Œ®ÞòÜÔº¥…(overview of C) ºÍÔ òÛú†‰óÕÚŒ†Ì òÖ»Œ‚® òÞÃóÕ·òÃÞ òµ· ò–ÕóîòµÚ º“¤ºÓ© ò×Ñòµ  ºË¤òÝкÓ⁄òµÃ(sections) òµ‰òµÃ òÈÚ ò×ȺË⁄.
(Œ•‚º÷‹ºËÈŒ„Ì º®®òÙ·, óÏ‹òµ‚óî, óÌÌòµ…òÙÐ ºÌ óÕÞ Œ†‰óÙÚòµ· ò×ÈòÞ…º’· 1.8ò×¥ Œ„Ìò§ ºÏà òµ‰òµÃ òÈÚ ò×ȺÊԺͮ, Cº¥… ò†ÚòµÌ ò©ÑóÕÚºÊÔ ºÏÄò×кÓ⁄òµ  ò¦ÞòÅÌóÕÞ 1.8ò×¥òµÃ ò©ÞòÛ‚óÕÞ ºË⁄º¥‚ ºÁÚº¤‚ò§  sections òµÃ º“¤ºÑÐ òµ‰òÖ·òÕ…º§Ì óÕÞºË⁄ºÊÔ òÖÚŒ‚®òµ‚Œ–® Œ®ÛòʦºËȺË⁄. ^_^)

Others can come back to material when they feel they are ready. Everyone should read this chapter with the understanding that technical details and further explanations will come in later chapter.

ºË⁄º¥‚ òµ·ºÓ⁄òµ  Œ•‚ºÓ⁄òµ· ò⁄ º„ÃŒ®  ºÐÚòÙȺË⁄Œ‡© ºÊкÁà ºÕÌ ºË⁄òËÞ òÝк£Ì,(ºÁ·òÜ’òÞ…)(material)º¡Þ ºÏÌòÕà òÚ‹ òÈÚ ò×ȺË⁄.
(òµ· ºÁ·òÜ’ ºÚÐóÕÞ 1.8ò×¥òµÃ Œ•‚ºÂ¥ ºÃÚòÖ·Œ®Ã ºÏÄò×кÓ⁄òµ· ºÁÚò⁄ÑòÙÐ º®®òÙ·Œ‡…, óÏ‹òµ‚óî, óÌÌòµ… ºÓ–òÙÐ ºÌ óÕ·òÃÞ òÖ·ºÊÐ ò©ÕºÏà òÕÌŒ‡©
ºÁÞ ºË⁄òµÌòÙÐ 1.8ò×¥òÞ…º¡Þ ºË⁄òËÞ ºÏÌòÕÃòÛ  ºÁ·òÜ’òµÃ º‡… òÈÚ ò×ȺË⁄ºÊÔ òÖÚŒ‚®ò×ĺËȺË⁄.)
º“¤ºÓ© òµ·ºÓ⁄òµ  Œ‚®òÈ©ò©Àòµ‚ òº¶ òÁ‹óÕ›Œ‡… ºÍÔ º§Îòµ  òÃ⁄º“ĺÓ⁄òµ· ºË⁄òµÌ ò–ÕóîòÙÐòÃÞ Œ‡ÃòÅÍ ºÐÞºË⁄(will come)ºÊÔ òµ·óÕ·òÛ  ºÍÔº¶ÈòÖ· º‡‚ ò–Õó… òµ‰òÖ·òÕ… óÕ© Œ†Âòµ·ºË⁄.

1.1 Programming and preparation

Resident on the machine is a collection of special programs called the operating system. Commonly available operating systems include MS-DOS. OS/2, and UNIX. An operating system manages machine resources, provides software for the user, and acts as an interface between the user and hardware.

º¤‚òË©òÞÃòÙÐ ò¡·ò׋óÕÚºÊÔ(òÁ·òÕÃŒ® ºÊÔ, Resident) óÊ„º‡ÃóÕÞ óÔú¡ÞŒ•‚º×¤ºÓ⁄òµÚ ò§ÑóÕ’ò†·º¥… òÜ·òÚÀò†·ò©Þ(operating system) ºµ… º¶ º¥‚ºË⁄. òµ…º®Úò©ÀòÞ…º¡Þ Œ® ºÊ¥óÕÞ òÜ·òÚÀò†·ò©ÞºÊÔ MS-DOS, OS/2, and UNIXº¥… óÏ‹óÕ¤óÕÞºË⁄(òµ…ò»«ºÊÔºË⁄). óÕÚºÁÚòµÚ òÜ·òÚÀò†·ò©ÞºÊÔ º¤‚òË©òµÚ ò×ÐòÝкÓ⁄òµÃ Œ· ºƒ‹óÕÚŒ‡©, òÁ‹òÜ’ò×к¥… òÞÃóÕÞ òÅÌóÔÃóÊ‚òݤòÖ·òÛ  Œ•‚ºƒ‹Œ‡© òÁ‹òÜ’ò×ÐòÛ  óÕÚºÓÞòݤòÖ·òµÚ òÁ‹òµ·º¥… òÙ®Œ†®óÕÚºÊÔ(ò¡®ò©ÕóÕÚºÊÔ,interface)óÖÉòÞúÓ⁄òµÃ ò©ÞŒ‡¦óÕÞºË⁄(òÁ‹òÜ’ò×ÐòÛ  óÕÚºÓÞòݤòÖ· òÁ‹òµ·òµÚ interfaceº¥… ò©ÞŒ‡¦óÕÞºË⁄).

Among the many software packages provided by the operating system are the C compiler and various text editors. The principal text editor on the UNIX system is called vi. Some systems, such as Borland C++, integrate the text editor and the compiler.

òÜ·òÚÀò†·ò©Þ(Operating system)òÙÐ òµÚóÕ·òÃÞ ò©ÞŒ‡¦ºÐÚòÖ·ò§ ºÊÔ º§Îòµ  òÅÌóÔÃóÊ‚òݤòÖ· óÌ’óÁ⁄ò§ ºÓ⁄ ò⁄ÑòÙкÊÔ C compiler òÛ  ºË⁄òÖÑóÕÞ º‹‚òÃÞó΂ò§ÑŒ‚®(text editor)Œ®  ò×ȺË⁄. UNIX systemòÙÐòÃÞòµÚ ò£…ºÐÞ º‹‚òÃÞ ó΂ò§ÑŒ‚®ºÊÔ vi ºµ… º¶Èºƒ®ºË⁄. Borland C++ Œ‡… Œ®Ûòµ  òÖ·ºÖ⁄ òËÞòÊ⁄óÄÞºÓ⁄òµ  º‹‚òÃÞó΂ò§ÑŒ‚®òÛ  compilerº¥… óŦóÕ’óÕÚŒ‡© ò×ȺË⁄.

We assume that the reader is able to use some text editor to create files containing C code. Such files are called source files, and they are compiled on most UNIX system with the cc command, which invokes the C compiler. Because the cc command invokes the compiler, the name of the command is also the name of the compiler. Thus, C compiler and cc compiler are used interchangeably. Roughly speaking, a compiler translates source code to object code that is executable.

òÜ®ºƒ‹ºÊÔ Œ® ò©ÕóÕÞºË⁄. ºÏÄò×кÓ⁄òµ· C codeº¥… óÏ‹óÕ¤óÕÚŒ‡© ò×ȺÊÔ óÌÌòµ…òµÃ òµòÖóÕÚŒ‚® òÞÃóÕÞ òÖ·ºÖ⁄ º‹‚òÃÞ ó΂ò§ÑŒ‚®º¥… òÁ‹òÜ’óÕ© òÈÚ ò×ȺË⁄ºÊÔ Œ†ÂòµÃ. Œ•‚º÷‹óÕÞ óÌÌòµ…ºÓ⁄òµÃ sourceóÌÌòµ… òµ·ºµ… óÕÚŒ‡©, Œ•‚ºÓ⁄òµ  ºÌ º¶ º¶ÃòµÚ UNIX systemòÙÐòÃÞcc ºµ…ºÊÔ º“ĺ©„òÞ…º¡Þ compile ºÐÚòÖ·ò§ÃºË⁄. cc ºµ…ºÊÔ º“ĺ©„òµ  C compilerº¥… º¶Èº÷‹ºÓ⁄òµ‚ºË⁄. cc ºµ…ºÊÔ º“ĺ©„òÖ·Œ®  ò»·óÌÌòµ…º÷‹º¥… º¶Èº÷‹ºÓ⁄òµ·Œ‚® ºÕ̺‹‚òÙÐ, º“ĺ©„òÖ·òµÚ òµ·ºƒÃòµ  ºÚÐóÕÞ ò»·óÌÌòµ…º÷‹òµÚ òµ·ºƒÃòµ·ºË⁄. Œ•‚º÷‹ºfl º¡Þ, C compiler òÛ  cc compiler ºÊÔ Œ¦ÐóÛÚóÕÚòÙ‹(Œ®Ûòµ  òµÚºfl‚º¡Þ) òÁ‹òÜ’ ºÐÚòÖ· ò¡ÌºË⁄. Œ–®ò„©Œ†Ì º§ÐóÕÚò×к’·, ò»·óÌÌòµ…º÷‹ºÊÔ source codeº¥… òË⁄óÖÉóÕ© òÈÚ ò×ȺÊÔobject codeº¡Þ º‡ óÛÚòËÞò…Þò£…ºÊÔ Œ†Âòµ·ºË⁄.

On UNIX systems, this compiled code is automatically created in a file named a.out.
On MS-DOS systems, this compiled code is automatically created in a file with the same name as the .c file, but with the .exe extension replacing the .c extension. At the end of this chapter, in Section 1.10, í ÞOperating System Considerations,í µ on page 53, we present in detail the steps necessary to edit, compile, and execute a program.

UNIX systemòÙÐòÃÞºÊÔ òµ· ò»·óÌÌòµ…ºÐÞ ò‰ÔºÓÞŒ® a.outòµ·ºµ…ºÊÔ óÌÌòµ… òµ·ºƒÃ òÕÈòÙÐ ò×кÏÛò©ÀòÞ…º¡Þ º§ÌºÓ⁄òÖ·ò§ÃºË⁄.(ò»·óÌÌòµ… óÕÚº’· Œ•‚ óÌÌòµ… òµ·ºƒÃòµ· a.outòµ· ºÐÞºË⁄.)
MS-DOS systemòÙÐòÃÞºÊÔ òµ·º©ÇŒ†Ì compile ºÐÞ ò‰ÔºÓÞŒ®  .c óÌÌòµ…Œ‡… Œ®Ûòµ  òµ·ºƒÃòÞ…º¡Þ ò®‰ò¡®ºÐÚòÖ· ò§ÃºË⁄. ºË¤ò§  .c óÛÕò×¥ò×ÐŒ®  .exe óÛÕò×¥ò×к¡Þ Œ¦Ðò†·ºÐÚº’·òÃÞ. (ºÏÃòÊ⁄òÙÐòÃÞºÊÔ poem.c ºµ…ºÊÔ òÅÌòÊ⁄ ò‰ÔºÓÞº¥… ò»·óÌÌòµ… óÕÚº’· poem.exe ºµ…ºÊÔ òË⁄óÖÉ óÌÌòµ…òµ· º§ÌºÓ⁄òÖ·ò§ ºÊԺͮ ºÒ⁄òÙÐ ò×ȺÊÔ óÛÕò×¥ò×Ð(.c -> .exeº¡Þ)º§Ì º®Ôº Ìº’·òÃÞ òÕ×òµÚ í Úpoemí Ûòµ·ºµ…ºÊÔ òµ·ºƒÃòµ  Œ•‚ºÌ º¡Þ òÞ©ò§  ºÐÞºË⁄ºÊÔ Œ†Âòµ·ºË⁄.)
òµ· ò–ÕóîòµÚ º§Èò§ º§É º¶ º¶Ã, Section 1.10òÕÈòµÚ, 53pageòÙÐ ò×ȺÊÔí µOperating System Considerationsí µ(òÜ·òÚÀò†·ò©ÞòµÚ Œ‡©ò®®)òÙÐòÃÞ òÜ®ºƒ‹ºÊÔ óÔú¡ÞŒ•‚º×¤òµÚ ó΂ò§Ñ, ò»·óÌÌòµ…, Œ•‚ºƒ‹Œ‡© òË⁄óÖÉóÕÚŒ‚®òÞÃóÕ· óÕÃòÜÔóÕÞ òº¶ ò©Àòµ‚ Œ‡…ò©ÕºÓ⁄òµÃ òÅÌŒ®ÞóÕÞºË⁄.

1.2 Program Output

Programs must communicate to be useful. Our first example is a program that prints on the screen the phrase í Þfrom sea to shining C.í µ The complete program is

óÔú¡ÞŒ•‚º×¤ºÓ⁄òµ  òÞ©òÜ’óÕ¤òµÃ(òͤº¤„Œ‚®)òÞÃóÕ· ò©ÃºË‹(communicate) ºÐÚòÖ·òÕ… óÕÞºË⁄. òÜ®ºƒ‹òµÚ ò†«º†Èò§‚ òÚȺÊÔ òÊ⁄óÀ‹ºƒ®òÙÐ í Þfrom sea to shining C.í µ ºµ…ºÊÔ º‹‚Œ¦‹º¥… óÔúƒ®óÊ‚óÕÚºÊÔ óÔú¡ÞŒ•‚º×¤òµ·ºË⁄. òÛÃòÖºÐÞ óÔú¡ÞŒ•‚º×¤òµ  In file sea.c òÙÐ ò×ȺË⁄.

In file sea.c

#include

int main(void)
{
printf(í Þfrom sea to shining C\ní µ);
return 0;
}

Using a text editor, we type this into a file whose name ends in .c. The choice of a file name should be mnemonic. Let us suppose the program has been written in the file sea.c. To compile the program, we give the command

º‹‚òÃÞ ó΂ò§ÑŒ‚®(text editor)º¥… òÁ‹òÜ’óÕÚòÙ‹, òÜ®ºƒ‹ºÊÔ Œ•‚ óÌÌòµ…òµÚ òµ·ºƒÃòµÚ ºÀµòµ· .c òµ‚ óÌÌòµ… òÕÈòÙÐ òµ·Œ†Â(òÞÃòÙÐ ò×ȺÊÔ source code)òµÃ òͤ ºÃ£ºÊÔºË⁄. óÌÌòµ… òµ·ºƒÃòµÚ òéóµòµ  Œ‚®òÖ¦óÕÚŒ‚® òÉ‹òÜ· Œ†ÂòÞ…º¡Þ óÕÞºË⁄. sea.c ºµ…ºÊÔ óÌÌòµ… òÕÈòÙÐ óÔú¡ÞŒ•‚º×¤òµ· òÓ®òÙ‹ò¡ÌºË⁄Œ‡© Œ® ò©ÕóÕ· º‡·ò×Ð. Œ•‚ óÔú¡ÞŒ•‚º×¤òµÃ ò»·óÌÌòµ… óÕÚŒ‚® òÞÃóÕ· òÜ®ºƒ‹ºÊÔ º“ĺ©„òµÃ ò⁄ ºË⁄.

cc sea.c

If there are no errors in the code, the executable file a.out is created by this command.
Now the command

º§ÌòÕ‰ ò‰ÔºÓÞ òÕÈòÙÐ òÕú‹·º÷® òÙк÷‹Œ®  òÙźË⁄º’·, òµ· º“ĺ©„(òÞÃòÙÐ òÓ· cc sea.c)òÙÐ òµÚóÕ· òË⁄óÖÉ Œ® ºÊ¥óÕÞ óÌÌòµ… a.outòµ· º§ÌºÓ⁄òÖ· ò§ÃºË⁄.
òµ·ò©Þ º“ĺ©„ a.out òµ 

a. out

executes the program and prints on the screen
óÔú¡ÞŒ•‚º×¤òµÃ òË⁄óÖÉóÕÚŒ‡©(ºË⁄òµÌ ºÁ·òÜ’òµÃ) òÊ⁄óÀ‹ºƒ® òÞÃòÙÐ óÔúƒ®óÊ‚ óÕÞºË⁄.

from sea to shining C

Dissection of the sea Program (óÔú¡ÞŒ•‚º×¤ seaòµÚ óÕ·º¶ )

#include

A preprocessor is built into the C compiler. When the command to compile a program is given, the code is first preprocessed and then compiled. Lines that begin with a # communicate with the preprocessed. This #include line causes the preprocessor to include a copy of the header file at this point in the code.
This header file is provided by the C system. The angle brackets around indicate that the file is to be found in the usual place, which is system-dependent. We have included this file because it contains information about the printf() function.

Preprocessor(ò©Ãò†Úºƒ‹ ò×¥ò„Ú, ºfl‚ºƒ‹ º¶ÃòõóÕÚºÊÔ ò×¥ò„Ú: ò© òµÚ º†ÈòÙ›òÙÐòÃÞºÊÔ preprocessorº¥… Œ•‚ºÂ¥ Œ‡©òÞ© º“ÄòÁ‹óÛÔ óÕ·òÃÞ òÁ‹òÜ’óÕ© Œ†Âò×ĺËȺË⁄. í Úò©Ãò†Úºƒ‹ ò×¥ò„Úí Ûºµ…ºÊÔ ºË¤òÖ·º‡·ºË⁄ preprocessorºµ…ºÊÔ ºË¤òÖ·Œ®  ºÍÔ ºÁ«ºË⁄Œ‡© òµŒ®ÀóÕ’ºËȺË⁄. òÕ×òÞ…º¡ÞºÏà º“Ç Œ®Þ ò©ÕºÏúÊÔ Œ‡©òÞ© º“ÄòÁ‹óÛÔ óÕ·òÃÞ òÁ‹òÜ’óÕÚºÊÔ ºË¤òÖ·ºÓ⁄òµ· ò×ÈòµÃ Œ†ÀºËȺË⁄.)ºÊÔ C ò»·óÌÌòµ…º÷‹òÙÐ òµÚóÕ·(C ò»·óÌÌòµ…º÷‹º¥… òͤòÃÞ) ò¡®ºƒ‰ºÐÚòÖ· ò¡ÌºË⁄(º§ÌºÓ⁄òÖ· ò¡ÌºË⁄, is built into).
#Œ‡… óÕ¤Œ»Ú òËÞò×ÑóÕÚºÊÔ ò⁄Ã(º‹‚ò×¥)ºÓ⁄òµ  ºfl‚ºƒ‹ º¶ÃòõºÐÚòÖ·òÃÞ(ò©Ãò†Úºƒ‹ ºÐÚòÖ·òÃÞ,preprocessed) ò©ÃºË‹(òµÚòÁ‹òÅÌóŦ, óŦòË©)ºÐÞºË⁄.
òµ· #include ºµ…òµ‚òµ  (preprocessorº¥… òµ·òÜ’óÕ·) ò‰ÔºÓÞ òÕÈòµÚ òµ· º¶ º¶Ã, òƒÉ óÙ⁄ºÍÔ óÌÌòµ…òµ‚ òµÚ óÕÚºÁÚòµÚ ò„·óÔ…(º‡¦ò©Þ,a copy)º¥… º¶Èº÷‹ºÓ⁄òµ·Œ‚®(to include)òÞÃóÕ· preprocessorº¥… òµ…òÞ…óÁ¤ºË⁄.
(Œ®Þòµ‚ò©Àòµ‚ óÕ·òÃ⁄: # include ºµ…òµ‚òµ  ºµ…ºÊÔ óÙ⁄ºÍÔ óÌÌòµ…òµÃ º¶Èº÷‹ ºÓ⁄òµ·ºÊԺͮ(º‡¦òÁ‹ óÕÚºÊԺͮ) Œ•‚º©ÇŒ†Ì óÕÚŒ‚® òÞÃóÕ·òÃÞ ºfl‚ºƒ‹ º¶ÃòõóÕÚºÊÔ ò×¥ò„Úòµ‚ preprocessorº¥… òµ…òÞ…óÁ¤ºË⁄ºÊÔ(causes) óÚ„òµ  òÁ‹òÜ’óÕÞºË⁄ºÊÔ òµ·òÕ…Œ‚® ò×ĺËȺË⁄. Œ•‚º÷… òÛÞ óÙ⁄ºÍÔóÌÌòµ…òµÃ º¶Èº÷‹ ºÓ⁄òÙ‹òÕ… óÕÚºÁÚòÜÔ? ò»·óÌÌòµ…º÷‹Œ®  òµ‚Œ®ÃòµÚ source codeº¥… Œ‚®Œ‡ÃòÖ·º¡Þ º®ÔŒ¬‚òÖ·ò£…òÖ· 0Œ‡… 1,(ò©Ãº¥ÚŒ®  óµÐº¥·ºÂÐ óµÐº¥·ò§  òÕʺÊкÂÐ)º®ÖòÙÐ òÕÌ òÈÚ òÙźÊÔmachine òÙÐŒ†Ì òµ‚ò˵óÕÚºÏú¡µ óÕ·ò£…ºÊÔ Œ†Âòµ·ºµ…ºÊÔ Œ†Âòµ·ºµ…Œ‡© òµŒ®À óÕ© ºÕÌ Œ•‚ ò⁄ÑŒ®ÃòÙÐ òÖ·ºÖ©óÕÞ òÙ®Œ†® Œ‡©ºƒ‹Œ®  ò×ÈòÖ·òÕ… óÕÚŒ†©ò§ òÜÔ? óÚ„òµ  òÕÃò£… Œ‚®º‡‚òµ· ºÐÚºÊÔ óÕ¤òÈÚºÓ⁄òµ· ò×ÈòÖ·òÕ… óÕÚò§  òÕÊòµÃŒ„ÌòÜÔ? 0Œ‡… 1òÙÐ Œ® Œ„µŒ–®ºÁÚ óÚ„òµ  Œ•‚Œ†ÂºÓ⁄òÙÐ ò©ÑŒ•…óÕ© òÈÚ ò×ȺÊÔ Œ‚®º‡‚ò©Àòµ‚ óÕ¤òÈÚºÓ⁄ º§Ðòµ·ò£©. º‹…º¡© ò¡®Œ‚È òµÑòÜ’ºÐÞ óÕ¤òÈÚºÓ⁄ºÏà ò×ÈŒ†©ò§ òÜÔ. º›Ð Œ…› òµ·º÷‹óÕÞ Œ†Âòµ· óÙ⁄ºÍÔóÌÌòµ…òµÚ ò©ÕòµÚºÊÔ òÕúËÈŒ†©ò§ º§Ì òÛÞ óÙ⁄ºÍÔóÌÌòµ…òµÃ º¶Èº÷‹ºÓ⁄òÙ‹òÕ…º§Ì óÕÚºÊÔŒ® òÙÐ ºÌ óÕÞ Œ¶ÀŒ‚Èòƒµ ò©ÕºÏÃŒ®  óÒ ºƒ‹ºÊÔ ºÍ®òÙкÊÔ ºÏÃòÝ òµ· ºÐ© Œ†Â Œ®ÛòʦºËȺË⁄.)

int main(void)

This is the first line of the function definition for main(). (We write parentheses after the name main to remind the reader that main() is a function.) The two words int and void are keywords, also called reserved words. They have special meaning to the compiler. In Section 2.4, í ÞKeywords,í µ on page77, we will see that there are 32 keywords in C, including int and void.

òµ·Œ†Âòµ  main()óÕ¤òÈÚº¥… òÞÃóÕÞ óÕ¤òÈÚò©ÕòµÚòµÚ ò†«º†Èò§‚ ºµ…òµ‚òµ·ºË⁄. (òÜ®ºƒ‹ºÊÔ main()òµ· óÕ¤òÈÚºµ…ºÊÔ Œ†ÂòµÃ òµ‰ºÊÔ òµ·òÙÐŒ†Ì òÂÀŒ‚®òËÞóÁ⁄Œ‚® òÞÃóÕ· mainòµ·ºµ…ºÊÔ òµ·ºƒÃ ºÒ⁄òÙÐ Œ·ÃóÚ‚(parenthesis)º¥… òÓ·ºË⁄.) ºÑÐ ºË¤òÖ· int òÛ  voidºÊÔ keywords(òµ·Œ†Â ºÚÐóÕÞ Œ‡©òÞ©º“ÄòÁ‹º¡Þ òÓ®Œ†©òʦºËȺË⁄.)òµ·ºË⁄. ºÚÐóÕÞ òÚÈòÕ‰ºÐÞ(óÝÙºÁ©òµÃ òÞÃóÕ· ºÖ…òÖ· ºÅÓòµ , reserved) ºË¤òÖ·ºÓ⁄òµ·ºµ…Œ‡©ºÏà º¶ º¥‚ºË⁄. Œ•‚ºÓ⁄òµ  ò»·óÌÌòµ…º÷‹òÙÐŒ†Ì óÊ„º‡ÃóÕÞ òµÚºfl‚º¥… ò§ ºË̺Ë⁄. 77pageòÙÐ In Section 2.4, í Þkeywords,í µ òÙÐòÃÞ, òÜ®ºƒ‹ºÊÔ C òÕÈòÙÐòÃÞòµÚ 32Œ®ÞòµÚ keywordsº¥… º‡… Œ†Âòµ·ºË⁄, int òÛ  void º¥… óÏ‹óÕ¤óÕÚòÙ‹.

int main(void)
{
....

Every program has function named main(). Program execution always starts with this function. The top line should be read as í Þmain() is a function that takes no arguments and return an int value.í µ

º“¤ºÓ© óÔú¡ÞŒ•‚º×¤òµ  main()òµ·ºµ…ºÊÔ òµ·ºƒÃòµÚ óÕ¤òÈÚº¥… Œ®ÖºÊÔºË⁄. óÔú¡ÞŒ•‚º×¤òµÚ òË⁄óÖÉòµ  óÕ›òÂÀ òµ· óÕ¤òÈÚòÛ  óÕ¤Œ»Ú òËÞò×ÑóÕÞºË⁄. ò©Þòµ… òÞÃòµÚ ºµ…òµ‚òµ  í Þmain()òµ  òÖ·ºÖ⁄ ºÏĺƒ‰º‡ òÈÚ(arguments) ºÓ⁄ºÏà ò•¤óÕÚò§  òÕÊòÞ…º’® int valueº¥… ºÐÚºÏ̺ƒ®ºË⁄í µ ºµ…Œ‡© òµ‰óÚ ò©‚òÕ… óÕÞºË⁄.

Here, the keyword int tells the compiler that this function returns a value of type int. The word int stands for integer, but the word integer itself cannot be used. The parentheses following main indicate to the compiler that main is a function.

òÙ‹Œ‚®, óÁ⁄òÝ̺ÓÞ int ºÊÔ ò»·óÌÌòµ…º÷‹òÙÐŒ†Ì òµ· óÕ¤òÈÚºÊÔ int óÚÕ(int type)òµÚ Œ®ÒòµÃ ºÐÚºÏ̺ƒ®ºË⁄Œ‡© òÖÚŒ‚®óÕÞºË⁄. int ºµ…ºÊÔ ºË¤òÖ·ºÊÔ ò©ÕòÈÚ(integer)º¥… òµÚºfl‚óÕÚò§ º§Ì(integer òµÚ òÕ‰ò×Ðòµ·ò§ º§Ì), integerºµ…ºÊÔ ºË¤òÖ·ºÊÔ Œ•‚ òÊ⁄òÊ⁄º¡Þ òÁ‹òÜ’ºÐÚòÖ· ò§È òÈÚ òÙźË⁄. main óÕ¤òÈÚ ºÒ⁄òÙÐòÚ⁄ºÊÔ Œ·ÃóÚ‚() ºÊÔ ò»·óÌÌòµ…º÷‹òÙÐŒ†Ì mainòµ  óÕ¤òÈÚºµ…ºÊÔ Œ†ÂòµÃ Œ® ºƒ‹óÁ¤ºË⁄.

This idea is confusing at first because what you see following main is (void), but only the parentheses () constitute an operator telling the compiler that main is a function. The keyword void indicates to the compiler that this function takes no arguments. When we write about functions such as main() and printf(), we usually follow the name in print with parentheses. This indicates to the reader that we are discussing a function. (Many programming books follow this practice.)

òµ· óÚÕóÂÞ(òÛ‚Œ· ,idea)ºÊÔ mainºÒ⁄òÙÐ ºÔ®º¥·ºÊÔ (void) º¥… ºË„òË©òµ· º‡·ºÊÔ òµ·òÞ©º¡Þ ò†ÚòµÌòÙÐ óÚ…º× òÊ⁄º÷‰ºË⁄.
óÕÚò§ º§Ì ºË¤ò§  Œ·ÃóÚ‚ºÊÔ mainòµ  óÕ¤òÈÚºµ…Œ‡© ò»·óÌÌòµ…º÷‹òÙÐŒ†Ì òÖÚŒ‚®óÕ·ò£…ºÊÔ òÙ®òÁ®ò×Ð(operator)º¥… Œ¦‹òÖóÕÞºË⁄. óÁ⁄òÝ̺ÓÞ void ºÊÔ, òÖ·ºÖ©óÕÞ ºÏĺƒ‰º‡ òÈÚ(arguments) ºÓ⁄ºÏà ò•¤óÕÚò§  òÕʺÊÔ òµ· óÕ¤òÈÚº¥… ò»·óÌÌòµ…º÷‹òÙÐŒ†Ì òµ‚ò˵ò…  óÕÞºË⁄. (Œ•‚º÷‹ºËÈŒ„Ì int main(void)òÙÐòÃÞ voidºÊÔ òµ· óÕ¤òÈÚŒ®  òÖ·ºÖ©óÕÞ ºÏĺƒ‰º‡ òÈÚºÏà ò•¤óÕÚò§  òÕʺÊÔºË⁄ºÊÔ Œ†ÂòµÃ Œ® ºƒ‹óÁ¤ºË⁄ºÊÔ Œ†Âòµ·ºË⁄. òÜ®ºƒ‹Œ®  main() Œ‡… printf()òÛ  Œ®Ûòµ  óÕ¤òÈÚºÓ⁄òÙÐ ºÌ óÕ· òÓ‚ ºÕÌ, òÜ®ºƒ‹ºÊÔ òµ…º®Úò©ÀòÞ…º¡Þ Œ·ÃóÚ‚òÛ  óÕ¤Œ»Ú òÓ®òµ‚ òµ·ºƒÃºÓ⁄òµÃ ºÔ®º¥‚ºË⁄. (òµ· º‹‚ò×¥òµ  òÕÃò£… ò׋º®ÌºÊÔ º‹‚ò×¥òµ‚ºÍ® Œ•‚º÷‹ºËÈŒ„Ì óÕ¤òÈÚºÓ⁄òµÃ òÓ‚ ºÕÌ main() , printf() òµ·º÷® ò˵òÞ…º¡Þ Œ·ÃóÚ‚òÛ  óÕ¤Œ»Ú òÓ·ºË⁄ºÊÔ ºÁ·òÜ’ò×ĺËȺË⁄.) òµ·Œ†Âòµ  ºÏÄò×кÓ⁄òÙÐŒ†Ì òÜ®ºƒ‹Œ®  óÕ¤òÈÚòÙÐ ºÌ óÕ· ºÄ…òµÚóÕÚŒ‡© ò×ȺË⁄ºÊÔ Œ†ÂòµÃ Œ® ºƒ‹óÁ¤ºË⁄.
(º§Îòµ  óÔú¡ÞŒ•‚º×Úº®Í ò–ĺÓ⁄òµ· òµ·º÷‹óÕÞ òʦŒ· (Œ· º¡ )òµÃ ºÔ®º¥‚ºË⁄.)

{

Braces surround the body of a function definition. They are also used to group statement together.
ò⁄ÑŒ·ÃóÚ‚ºÊÔ óÕ¤òÈÚò©ÕòµÚòµÚ º“‚òµÃ º‹¶ºÊÔºË⁄. Œ•‚ºÓ⁄òµ  ºÚÐóÕÞ º‹‚ò×¥ Œ•‚º£„òÞ…º¡Þ óÕ¤Œ»Ú òÁ‹òÜ’ºÐÚòÖ·ò§ÃºË⁄.

printf()

The C system contains a standard library of functions that can be used in programs.
This is a function from the library that prints on the screen. We included the header file stdio.h because it provides certain information to the compiler about the function printf().

C systemòµ  óÔú¡ÞŒ•‚º×¤ºÓ⁄òµ· òÁ‹òÜ’ºÐÚòÖ·ò§È òÈÚ ò×ȺÊÔ óÕ¤òÈÚºÓ⁄òµÚ óÑÞò⁄  ºµ…òµ·º‚̺÷‹ºƒ‹º¥… óÏ‹óÕ¤óÕÞºË⁄. òµ· óÕ¤òÈÚºÊÔ òÊ⁄óÀ‹ºƒ® òÞÃòÙÐ óÔúƒ®óÊ‚ ºÐÚòÖ·ò§ ºÊÔ ºµ…òµ·º‚̺÷‹ºƒ‹ óÕ¤òÈÚòµ·ºË⁄. òÜ®ºƒ‹ºÊÔ óÙ⁄ºÍÔóÌÌòµ… stdio.hº¥… óÏ‹óÕ¤òËÞò…®ºË⁄. òµ·òÞ©ºÊÔ Œ•‚Œ†Âòµ  printf()óÕ¤òÈÚòÙÐ ºÌ óÕÞ óÛÕòË⁄óÕÞ ò©Õº‡·º¥… ò»·óÌÌòµ…º÷‹òÙÐŒ†Ì ò©ÞŒ‡¦óÕÚŒ‚® ºÕ̺‹‚òÙÐ.

í Þfrom sea to shining C\ní µ

A string constant in C is a series of characters surrounded by double quotes. This string is an argument to the function printf(), and it controls what gets printed. The two characters \n at the end of the string(read backslash n) represent a single character called newline. It is a nonprinting character. It advances the cursor on the screen to the beginning of the next line.

C òÕÈòÙÐòÃÞòµÚ º‹‚ò×ÐòÙ· òÂÀòÈÚ(string constant)ºÊÔ ºÔ®òÚ·óÑÞòÙÐ òµÚóÕ· ºÑÚº÷‹òË‚òÙ‹ò§Ã º‹‚ò×кÓ⁄òµÚ óÕÞ òËÞºƒ‹òƒÈºË⁄.
òµ· º‹‚ò×ÐòÙ·òµ  printf() óÕ¤òÈÚòµÚ óÕÞ ºÏĺƒ‰º‡ òÈÚ(Œ‡©ò©Õº‡ òÈÚ, argument)òµ·ºË⁄. Œ•‚ºƒ‹Œ‡© Œ•‚Œ†Âòµ  º‹·òÙÇòµ· óÔúƒ®óÊ‚ ºÐÚòÖ·ò§Èò§  ò‰ÚóÊ‚º¡⁄ óÕÞºË⁄. º‹‚ò×ÐòÙ·òµÚ ºÀµòÙÐ ò×ȺÊÔ ºÑÐ º‹‚ò×кÓ⁄ \n (í Þbackslash ní µ òµ·ºµ…Œ‡© òµ‰ºÊÔºË⁄)òµ  òÂȺ¡ÞòÜ· ò⁄Ã(newline)òµ·ºµ… º¶Èºƒ‹ºÊÔ óÕÚºÁÚòµÚ ºÏÄò×ÐòÖòµÃ Œ® ò§Ã(òÈÞòÈÚóÕÞ,single) º‹‚ò×к¥… ºÁÚó ºÁ‚ºË⁄. Œ•‚Œ†Âòµ  ò®Íó×Èò§  òÕʺÊÔ º‹‚ò×Ðòµ·ºË⁄. Œ•‚Œ†Âòµ  òÊ⁄óÀ‹ºƒ® òÞÃòµÚ ò»⁄òÃÞº¥… ºË⁄òµÌ ºµ…òµ‚òµÚ òËÞò×Ѻ¶ º¶ÃòÙÐ ò§Ãº‡·(òÞÃò„Ú)òËÞóÁ¤ºË⁄.

Printf(í Þfrom sea to shining C\ní µ);

This is statement. Many statements in C end with a semicolon.
òµ·Œ†Âòµ  ò§ÃòÈ©òµ·ºË⁄(º‹‚ò×¥òµ·ºË⁄). CòÕÈòÙÐòÃÞ º§Îòµ  º‹‚ò×¥(ò§ÃòÈ©, statements)ºÓ⁄òµÚ ºÀµòµ  òºfl‚ò‰Þº¡©Œ‡… óÕ¤Œ»ÚóÕÞºË⁄. (C òÖ‚òÖ·òÙÐòÃÞºÊÔ º‹‚ò×¥òµÚ ºÀµòÙÔ òºfl‚ò‰Þº¡© semicolon òµÃ òÓ·ºË⁄ºÊÔ òÖÚŒ‚®ò×ĺËȺË⁄.)

return 0;

This is a return statement. It causes the value zero to be returned to the operating system, which in turn may use the value in some way, but is not required to do so.
(See Section 12.7, í ÞReturning the Status,í µ on page 579, for further discussion.)

òµ·Œ†Âòµ  ºƒ‹ó÷ º‹‚òµ·ºË⁄. òµ·Œ†Âòµ  òÜ·òÚÀò†·ò©ÞòÙÐŒ†Ì òÚÀòµÚ Œ®ÒòµÃ ºÐÚºÏ̺©⁄ º®ÝºÊÔ òÝÐòµ‚òµ· ºÐÞºË⁄. òÜ·òÚÀò†·ò©ÞºÊÔ òÖ·ºÖ⁄ Œ†‰º¡Þ(Œ†‰òÜ®, º®’º†Õ)òÙÐ ò×ÈòÖ·òÃÞºÊÔ ºÏ̺©⁄ º®ÝºÊÔ Œ®Òòµ· òÁ‹òÜ’ºÐÚòÖ· ò§È òÈÚºÏà ò×Èò§ º§Ì, óÕÚò§ º§Ì Œ•‚º©ÇŒ†Ì óÕÚŒ‚®òÞÃóÕ· òÜÔŒ¦‹ºÐÚòÖ·ò§ ò§ ºÊÔ òÕʺÊÔºË⁄. (Œ®ÒòµÃ ºÏ̺©⁄ º®ÝòµÃ òÈÚºÏà òÕúËÐ òÈÚºÏà ò×ȺË⁄.)

Our use of this return statement keeps the compiler happy. If we do not use it, the compiler will complain.(See exercise 4, on page 60.) One of the principal rules of programming is í Þkeep your compiler happy.í µ

òµ·º÷‹óÕÞ ºƒ‹ó÷ º‹‚òµÚ òÓ®ò×ÃòÂȺÊÔ ò»·óÌÌòµ…º÷‹º¥… óÖɺ‡¦óÕÚŒ†Ì òÞ©ò§ óÕ·ò⁄ ºË⁄. º§ÌòÕ‰ òÜ®ºƒ‹Œ®  òµ·Œ†ÂòµÃ òÁ‹òÜ’óÕÚò§  òÕʺÊÔºË⁄º’·, ò»·óÌÌòµ…º÷‹ºÊÔ º¶ÈóÏÉòµÃ óÕ© Œ†Âòµ·ºË⁄. óÔú¡ÞŒ•‚º×Úº®ÍòµÚ Œ•…º‡‚ò©Àòµ‚ º†Õò„Ûòµ  í Þò»·óÌÌòµ…º÷‹º¥… óÖɺ‡¦óÕÚŒ†Ì óÕ·ºµ….í µ òµ·ºË⁄.

}

The right brace matches the left brace above, ending the function definition for main().
òÚ⁄º¥‚ò“‰ ò⁄ÑŒ·ÃóÚ‚ºÊÔ òÕ×òÙÐ ò×ȺÍÚ òÛ…ò“‰òµÚ ò⁄ÑŒ·ÃóÚ‚òÛ  º§⁄ò„ÚºÐÚŒ‡©, main()òµÃ òÞÃóÕÞ óÕ¤òÈÚò©ÕòµÚòµÚ ò¢ÄŒ†®òµ·ºË⁄.


+ + + + + + +

The function printf() acts to print continuously across the screen. It moves the cursor to the start of a new line when a newline character is read. The screen is a two-dimensional display that prints from left to right and top to bottom. To be readable, output must appear properly spaced on the screen.
We can rewrite our first program as follows:

óÕ¤òÈÚ printf()ºÊÔ òÊ⁄óÀ‹ºƒ®òµÃ ò§ òÅÍò©ÀòÞ…º¡Þ Œ® º¡Þò§ º¥·ºÊÔ óÔúƒ®óÊ‚º¥… óÕÚŒ‚® òÞÃóÕ· òË⁄óÖÉóÕÞºË⁄. Œ•‚Œ†Âòµ  newline(\n) º‹‚ò×ÐŒ®  òµ‰óÚ  ò¡ÌòµÃ ºÕÌ òÂȺ¡ÞòÜ· ò⁄ÃòµÚ òËÞò×ÑòÙÐ ò»⁄òÃÞº¥… Œ®ÖºË⁄ ºÅÓºÊÔºË⁄. òÊ⁄óÀ‹ºƒ®òµ  2ò®¤òÝÐòµÚ, Œ•‚º÷‹ºËÈŒ„Ì òÛ…ò“‰òÙÐòÃÞº¶ óî òÚ⁄º¥‚ò“‰òÞ…º¡Þ Œ•‚ºƒ‹Œ‡© òÞÃòÙÐòÃÞº¶ óî òÕú×Úº¡Þ óÔúƒ®óÊ‚óÕÚºÊÔ dispalyòµ·ºË⁄. òµ‰òµÃ òÈÚ ò×ÈŒ†Ì óÕÚŒ‚® òÞÃóÕ·, ò¶Þº©¥(output)òµ  òÊ⁄óÀ‹ºƒ® òÞÃòÙÐ ò©Àò©ÈóÕÞ Œ‡¦Œ®ÃòµÃ ºÁÚó ºÁ·òÖ·òÕ… óÕÞºË⁄.
òÜ®ºƒ‹ºÊÔ òÜ®ºƒ‹òµÚ ò†«º†Èò§‚ óÔú¡ÞŒ•‚º×¤òµÃ ºË⁄òµÌŒ‡… Œ®Ûòµ· ºË⁄òËÞ òÓ‚ òÈÚ ò×ȺË⁄.

#include

int main(void)
{
printf(í Þfrom sea to í Þ);
printf(í Þshining Cí µ);
printf(í Þ\ní µ);
return 0;
}

Although it is different from the first version, it will produce the same output. Each time printf()is called, printing begins at the position where the previous call to printf() left off. If we want to print our phrase on three lines, we can use newline characters.

º„ú¡µ ò†«º†Èò§‚ º†Ãò©ÃŒ‡…ºÊÔ ºË⁄º¥·ò§ º§Ì, Œ•‚Œ†Âòµ  Œ®Ûòµ  ò¶Þº©¥(output)òµÃ º§ÌºÓ⁄òÖ· ºÁ… Œ†Âòµ·ºË⁄. printf()óÕ¤òÈÚºÊÔ º§⁄º†È º¶Èº©⁄ò§ Œ‡©, òµ·ò©ÃòÙÐ òÛ…ò“‰òÙÐòÃÞ ºÀµºÁÞ printf()óÕ¤òÈÚº¥… º¶ º¥‚ Œ•‚ ò×¥òÅÌòÙÐòÃÞº¶ óî óÔúƒ®óÊ‚ºÊÔ òËÞò×ѺÐÞºË⁄. º§ÌòÕ‰ òÜ®ºƒ‹Œ®  òÂŒ®ÞòµÚ ò⁄à òÞÃòÙÐ òÜ®ºƒ‹òµÚ òµ·òÕ…Œ‚®º¥…(Œ¦‹,phrase) óÔúƒ®óÊ‚óÕÚŒ‡© ò˶ºË⁄º’·, òÜ®ºƒ‹ºÊÔ newline (\n) º‹‚ò×к¥… òÁ‹òÜ’óÕ© òÈÚ ò×ȺË⁄.

#include

int main(void)
{
printf(í Þfrom sea\ní µ);
printf(í Þto shining\nC\ní µ);
return 0;
}

When executed, this program will print
óÔú¡ÞŒ•‚º×¤òµ· òË⁄óÖɺÐÚòÖ· ò¡ÌòµÃ ºÕÌ, òµ· óÔú¡ÞŒ•‚º×¤òµ  ºË⁄òµÌò†Úº÷… óÔúƒ®óÊ‚ óÕ© Œ†Âòµ·ºË⁄.

from sea
to shining
C

Let us write one additional on this program, one that will box the phrase in a rectangle of asterisks. It will show how each character, including blanks and newline characters, is significant, and when it is executed, it will give some sense of the screen proportions.

òµ· óÔú¡ÞŒ•‚º×¤òÞÃòÙÐ óÕÚºÁÚº¥… ºÍ§º¶ÛòÙ‹ òͤº‡·ò×Ð, Œ•‚ óÕÚºÁںנ òÕÃòÊ⁄óÄ̺ƒ‹òÊ⁄óÀ‹(*)ºÓ⁄òÙÐ òµÚóÕÞ òÁ‹Œ®ÀóÚÕ òÕÈòµÚ º®ÕòÊ⁄ òµ·òÕ…Œ‚®(Œ¦‹,phrase)Œ®  ºÐ©Œ†Âòµ·ºË⁄. Œ•‚Œ†Âòµ· òË⁄óÖÉ ºÐÚòÙÈòµÃ ºÕÌ º„È ò„‚ºÓ⁄Œ‡… newline(\n)º‹‚ò×к¥… óÏ‹óÕ¤óÕÚºÊÔ Œ®ÀŒ®ÀòµÚ º‹‚ò×кÓ⁄òµÃ º‡·òÙ‹ò⁄à Œ†Âòµ·ºË⁄. Œ•‚Œ†Âòµ  ò⁄ÑòÜÔóÕÞ Œ†Âòµ·Œ‡©, Œ•‚Œ†Âòµ  òÊ⁄óÀ‹ºƒ®òµÚ º„ÃòÞ¤(Œ•©óÚÕ,proportions)òµÚ òÖ·ºÖ⁄ Œ®ÐŒ®ÀòµÃ ò£…Œ†Ì ºÐ© Œ†Âòµ·ºË⁄.

In file sea2.c

#include

int main(void)
{
printf(í Þ\n\n\n\n\n\n\n\n\n\ní µ);
printf(í Þ              **********************\ní µ);
printf(í Þ              *   from sea                  *\ní µ);
printf(í Þ              * to shining C               *\ní µ);
printf(í Þ              **********************\ní µ);
printf(í Þ\n\n\n\n\n\n\n\n\n\ní µ);


return 0;
}

1.3 Variables, Expressions, and Assignment

We will write a program to convert the distance of a marathon in miles and yards to kilometers. In English units, a marathon is defined to be 26 miles and 385 yards. These numbers are integers.

òÜ®ºƒ‹ºÊÔ milesŒ‡… yardsòµÚ º§Èºµ…óÅ⁄òµÚ Œ–®ºƒ‹º¥… kilometersº¡Þ º‡ óÛÚòËÞóÁ‹ Œ†Âòµ·ºË⁄. òÚÀŒ¦›òµÚ ºË¤òÞÃòÙÐòÃÞºÊÔ, º§Èºµ…óÅ⁄òµ  26º§Èòµ… 385òÕ…ºÓÞº¡Þ ò©ÕòµÚºÐÚòÖ· ò×ȺË⁄. òµ·º÷‹óÕÞ òÈÚºÓ⁄òµ  ò©ÕòÈÚºÓ⁄òµ·ºË⁄.

To convert miles to kilometers, we multiply by the conversion factor 1.609, a real number. In memory, computers represent integers differently from real. To convert yards to miles, we divide by 1760.0, and, as we shell see, it is essential to represent this number as a real rather as an integer.

Milesº¥… kilometersº¡Þ º‡ óÛÔ òËÞóÁ⁄Œ‚® òÞÃóÕ·òÃÞ, òÜ®ºƒ‹ºÊÔ òË⁄òÈÚòµ‚ º‡ óÛÚ òµ‚ò×Ð 1.609òÙÐ òµÚóÕÞ Œ‡–òÄÈòµÃ óÕÞºË⁄. º’Ôº“¤ºƒ‹òÙÐòÃÞ, ò»·óÓ¤óîºÓ⁄òµ  òË⁄òÈÚòÛ  ò©ÕòÈÚº¥… ºË⁄º¥‚ Œ†ÂòÞ…º¡Þ óÑÞóÚÃóÕÞºË⁄. yardsº¥… milesº¡Þ º‡ óÛÚòËÞóÁ⁄Œ‚® òÞÃóÕ·òÃÞ, 1760.0òÞ…º¡Þ ºÁÚºÈÃŒ‡©, Œ•‚ºƒ‹Œ‡©, òÜ®ºƒ‹Œ®  º‡·ºÊÔ º®ÔòÛ  Œ®Ûòµ·, òµ· òÈ«ò×к¥… ºÁÚó ºÁ·ºÊÔ ºÍ®òÙкÊÔ ò©ÕòÈÚº‡·ºË⁄ºÊÔ òË⁄òÈÚŒ®  óÕÃòÜÔóÕÚºË⁄.(òË⁄ò§Èò©Àòµ·ºË⁄.)

Our conversion program will use variables capable of strong integer values and real values.

òÜ®ºƒ‹òµÚ º‡ óÛÚ óÔú¡ÞŒ•‚º×¤òµ  ò©ÕòÈÚóÚÕ Œ®ÒŒ‡… òË⁄òÈÚóÚÕ Œ®ÒòµÚ º‡ òÈÚºÓ⁄òµÃ òÁ‹òÜ’óÕ© Œ†Âòµ·ºË⁄.
In C, all variables must be declared, or named, at the beginning of the program. A variable name, also called an identifier, consists of a sequence of letters, digits, and underscores, but may not start with a digit.

CòÙÐòÃÞºÊÔ, º“¤ºÓ© º‡ òÈÚºÓ⁄òµ· Œ•‚ óÔú¡ÞŒ•‚º×¤òµ· òËÞò×ѺÐÚòÖ· ò§È ºÕÌ òéòÖ‚ºÐÚòÖ·ò§ Œ–®ºÁÚ òµ·ºƒÃ ò§ òÖ·ò§ÃºË⁄. º‡ òÈÚòµÚ òµ·ºƒÃòµ , ºÚÐóÕÞ identifier(óÛÕòµ‚ò×Ð)ºµ…Œ‡© º¶Èºƒ‹º’®, òÈ«ò×к¡Þ òËÞò×ÑóÕÚò§ ºÊÔ òÕʺÊÔ º‹‚ò×кÓ⁄òµÚ òÙ·Œ‡… òÈ«ò×кÓ⁄, Œ•‚ºƒ‹Œ‡© º®Ñò⁄ÃòÙÐ òµÚóÕ· Œ¦‹òÖºÐÚòÖ·ò§ÃºË⁄.

Identifiers should be chosen to reflect their use in the program. In this way, they serve as documentation, making the program more readable.

óÛÕòµ‚ò×кÓ⁄(º‡ òÈÚºÓ⁄)òµ  óÔú¡ÞŒ•‚º×¤òÙÐòÃÞ Œ•‚ºÓ⁄òµÚ òÓ®ò×ÃòµÃ º®ÚóÖ¥óÕÚŒ‚® òÞÃóÕ·òÃÞ òéóµºÐÚòÖ· ò©‚òÕ… óÕÞºË⁄. òµ·º÷® º®’º†ÕòÞ…º¡Þ, Œ•‚ºÓ⁄òµ  º§ÌºÓÞºÊÔ óÔú¡ÞŒ•‚º×¤òµÃ º‹‚òÃÞò†Úº÷… ºÍÔ òµ‰òµÃ òÈÚ ò×ÈŒ†Ì óÕ·ò⁄ ºË⁄.

In file marathon.c

/* The distance of a marathon in kilometers */
#include

int main(void)
{
int    miles, yards;
float    kilometers;

miles = 26;
yards = 385;
kilometers = 1.609 * (miles + yards / 1760.0);
printf(í Þ\nA marathin is %f kilometers. \n\ní µ, kilometers);
return 0;
}

The output of the program is

A marathon is 42.185970 kilometers.

Dissection of the marathon Program

/* The distance of a marathon in kilometers. */

Anything written between the characters /* and */ is a comment and is ignored by the compiler. All programs in this book that start with a comment are listed in the index.

º‹‚ò×кÓ⁄ /* Œ‡… */ºÓ⁄ òÁ‹òµ·òÙÐ òÓ®òÙ‹ò§Ã òÖ·ºÖ⁄ Œ†ÂºÓ⁄ºÏà ò£…òõ,óÕ·òÃ⁄,òÃ⁄º“Äòµ·º’® ò»·óÌÌòµ…º÷‹òÙÐòµÚóÕ· º‹·òË޺˄óÕÞºË⁄. òµ· ò–Ä òÕÈòÙÐòÃÞòµÚ º“¤ºÓ© óÔú¡ÞŒ•‚º×¤ºÓ⁄òµ  indexòÕÈòÙÐòÃÞ list ºÐÚòÖ·ò§Ã ò£…òõº‹‚Œ‡… óÕ¤Œ»Ú òËÞò×ÑóÕÞºË⁄.

int    miles, yards;

This is a declaration. Declarations and statements end with a semicolon. int is a keyword and is one of the fundamental types of the language. It informs the compiler that the variables flowing it are type int and are to take on integer values. Thus, the variables miles and yards in this program are of type int.

òµ·Œ†Âòµ  òéòÖ‚òµ·ºË⁄. òéòÖ‚ºÓ⁄Œ‡… º‹‚ò×¥ºÓ⁄òµ  òºfl‚ò‰Þº¡©Œ‡… óÕ¤Œ»Ú ºÀµºÁÞºË⁄. int ºÊÔ óÁ⁄òÝ̺ÓÞ(keyword)òµ·º’® òÖ‚òÖ·òµÚ Œ‚®º‡‚ò©Àòµ‚ óÚÕ(types)ºÓ⁄ ò⁄ÑòµÚ óÕÚºÁÚòµ·ºË⁄. Œ•‚Œ†Âòµ  ò»·óÌÌòµ…º÷‹òÙÐŒ†Ì º‡ òÈÚºÓ⁄òµ· int óÚÕòµÃ ºÔ®º¥‚ºË⁄ºÊÔ Œ†ÂŒ‡… ò©ÕòÈÚóÚÕòµÚ Œ®ÒºÓ⁄òµÃ ò•¤óÕÚŒ‚® òÞÃóÕÞ Œ†Âò×ÃòµÃ òÕ̺©⁄ò⁄ ºË⁄. Œ•‚º÷‹ºfl º¡Þ, óÔú¡ÞŒ•‚º×¤ òÕÈòÙÐòÃÞ milesòÛ  yardsºµ…ºÊÔ º‡ òÈÚºÓ⁄òµ  int óÚÕòµ·ºË⁄.

float    kilometers;

This is a declaration. float is a keyword and is one of the fundamental types of the language. It informs the compiler that the variables following it are of type float and are to take on real values. Thus, the variable kilometers in this program is of type float.

òµ·Œ†Âòµ  òéòÖ‚òµ·ºË⁄. float ºÊÔ óÁ⁄òÝ̺ÓÞ(keyword)òµ·Œ‡© òÖ‚òÖ·òµÚ Œ‚®º‡‚ò©Àòµ‚ óÚÕ(types)ºÓ⁄ò⁄ÑòµÚ óÕÚºÁÚòµ·ºË⁄. Œ•‚Œ†Âòµ  ò»·óÌÌòµ…º÷‹òÙÐŒ†Ì òÕ̺©⁄ò⁄ ºË⁄. º‡ òÈÚºÓ⁄òµ· floatóÚÕòµÃ ºÔ®º¥·Œ‡© òË⁄òÈÚòµÚ Œ®ÒºÓ⁄òµÃ ò•¤óÕÞºË⁄ºÊÔ Œ†ÂòµÃ. Œ•‚º÷‹ºfl º¡Þ, óÔú¡ÞŒ•‚º×¤òÙÐòÃÞ º‡ òÈÚ kilometers ºÊÔ floatóÚÕòµ·ºË⁄.

miles = 26;
yards = 385;

These are assignment statements. The equal sign is an assignment operator. The two numbers 26 and 385 are integer constants. The value 26 is assigned to the variable miles. The value 385 is assigned to the variable yards.

òµ·Œ†ÂºÓ⁄òµ  óÕ©ºË„ º‹‚ºÓ⁄òµ·ºË⁄. òµ·ó Ã Œ‚®óÚ‚ºÊÔ óÕ©ºË„ òÙ®òÁ®ò×Ðòµ·ºË⁄. ºÑÐŒ®ÞòµÚ òÈ«ò×кÓ⁄ 26Œ‡… 385ºÊÔ ò©ÕòÈÚ òÂÀòÈÚºÓ⁄òµ·ºË⁄. Œ®Ò 26òµ  º‡ òÈÚ milesòÙÐ óÕ©ºË„ºÐÚòÖ· ò§ÃºË⁄. Œ®Ò 385ºÊÔ º‡ òÈÚ yardsòÙÐ óÕ©ºË„ºÐÚòÖ· ò§ÃºË⁄.

Kilometers = 1.609 * (miles + yards / 1760.0);

This is assignment statement. The value of the expression on the right side of the equal sign is assigned to the variable kilometers. The operator *, +, and / stand for multiplication, addition, and division, respectively. Operations inside parentheses are performed first. Because division has higher precedence than addition, the value of the sub expression

òµ·Œ†Âòµ  óÕ©ºË„º‹‚òµ·ºË⁄. òµ·ó Ã Œ‚®óÚ‚òµÚ òÚ⁄º¥‚ò“‰ó΂òÙÐ ò×ȺÊÔ óÑÞóÚà Œ®Òòµ  º‡ òÈÚ kilometersòÙÐ óÕ©ºË„ºÐÚòÖ· ò§ÃºË⁄. òÙ®òÁ®ò×Р *, +, Œ•‚ºƒ‹Œ‡© / òµ  ò©ÞŒ®ÀŒ®À Œ‡–òÄÈ, ºÍ§òÄÈ, ºÁÚºÈÙòÄÈòµÚ òÕ‰ò„›,òÕ‰ò×Ð,ºÌ òË©óÕ·òÃÞ òÓ®òµ·ºÊÔ Œ†Â(stand for)òµ·ºË⁄. ()Œ·ÃóÚ‚ òÕÈòÙÐ ò×ȺÊÔ òÙ®òÁ®ò×кÓ⁄òµ  ò†«º†Èò§‚º¡Þ òÈÚóÖɺÐÚòÖ· ò§ÃºË⁄. ºÁÚºÈÙòÄÈòµ  ºÍ§òÄȺ‡·ºË⁄ ºÅÒòµ  òÜ®òéòÈÞòÞú¥… Œ®ÖŒ‚® ºÕ̺‹‚òÙÐ, óÕÚòÞà óÑÞóÚÃ(subexpression)Œ®Ò yards / 1760.0

yards / 1760.0

is calculated first. (See Appendix E, í ÞOperator precedence and Associativity.í µ) That value is added to the value of the variable miles to produce a value that is then multiplied by 1.609. This final value is then assigned to the variable kilometers.

òµ  º¤…ò©  Œ‡ÃòÁ®ºÐÚòÖ· ò§ÃºË⁄. Œ•‚ Œ®Òòµ  Œ•‚ºƒ‹Œ‡©ºÁÚòÃÞ 1.609òÙÐ òµÚóÕ· Œ‡–óÕÚòÙ‹ò§Ã Œ®ÒòµÃ òÁ®ò¶ÞóÕÚŒ‚® òÞÃóÕ· º‡ òÈÚ milesòµÚ Œ®ÒòÙÐ ºÍÔóÕÚòÙ‹ ò§ÃºË⁄. º§Èò§ º§É Œ®Òòµ  Œ•‚ºƒ‹Œ‡© ºÁÚòÃÞ º‡ òÈÚ kilometersòÙÐ óÕ©ºË„ºÐÚòÖ· ò§ÃºË⁄.

printf(í Þ\nA marathon is %f kilometers.\n\ní µ, kilometers);

This is a statement that invokes, or calls, the printf() function. The function printf() can have a variable number of arguments. The first argument is always a string, called the control string. The control string in this example is

òµ· º‹‚ò×¥òµ  printf()óÕ¤òÈÚº¥… Œ® ò©‚òÚ⁄Œ–®ºÁÚ º¶Èº÷‹ºÁ‚ºË⁄. óÕ¤òÈÚ printf()ºÊÔ ºÏĺƒ‰º‡ òÈÚºÓ⁄òµÚ º‡ òÈÚ òÈ«ò×к¥…(Œ•‚ºÂ¥ º‡ òÈÚº¥…òµ·ºµ…Œ‡© óÕ·òõóÕÚºÊÔ Œ†Âòµ· ºÍÔ ò¢ËòµÃ ºÓfl,a variable number) Œ® ò§È òÈÚ ò×ȺË⁄. ò†«º†Èò§‚ Œ‡©ò©Õº‡ òÈÚºÊÔ control stringòµ·ºµ… º¶Èºƒ‹ºÊÔ óÕÚºÁÚòµÚ º‹‚ò×ÐòÙ·òµ·ºË⁄. òµ· òÚÈòÙÐòÃÞ control stringòµ  òÕú×ÚòÛ  Œ®ÛºË⁄.


í Þ\nA marathon is %f kilometers. \n\ní µ

It is the first argument to the function printf(). Inside this string is the conversion specification, or format, %f. The formats in a control string, if any, are matched with the remaining arguments in the printf() function.

òµ·Œ†Âòµ  printf()óÕ¤òÈÚº¥… óÖ¥óÕÚºÊÔ ò†«º†Èò§‚ Œ‡©ò©Õº‡ òÈÚòµ·ºË⁄. òµ· º‹‚ò×ÐòÙ· òÕÈòÙкÊÔ ò©ÃóÛÚ,º‡ óÛÚòÁ‹óÕ›(conversion specification), óÚ„òµ  óÚÕò˵òµ· ºÐÚºÊÔ %f Œ®  ò×ȺË⁄. control string òÕÈòÙÐòÃÞòµÚ óÚÕò˵òµ (conversion specification, format, òÚȺ¥…ºÓ⁄º’· òµ· º‹‚ò×¥òÙÐòÃÞ %fºÊÔ), º§ÌòÕ‰ ò×ȺË⁄º’·, printf()óÕ¤òÈÚ òÕÈòÙÐòÃÞòµÚ ºÁ¤òÕÃò×ȺÊÔ Œ‡©ò©Õº‡ òÈÚºÓ⁄Œ‡… óÕ¤Œ»Ú òÖ·òÜ‚ºƒ®ºË⁄.

In this case, %f is matched with the argument kilometers. Its effect is to print the value of the variable kilometers as a floating-point number and insert it into the print stream where the format %f occurs.

òµ· Œ†‰òÜ®òÙÐòÃÞºÊÔ, %fºÊÔ Œ‡©ò©Õº‡ òÈÚ kilometersòÛ  óÕ¤Œ»Ú ò§µòµ· ºÐÞºË⁄.(òÖ·òÜ‚ºƒ®ºË⁄). Œ•‚Œ†ÂòµÚ Œ†®Œ‡…ºÊÔ, floating-point number òÛ  format %fŒ®  º®ÞòµóÕÚòÙ‹ print stream òÕÈòÞ…º¡Þ Œ•‚Œ†ÂòµÃ(floating-point numberº¥…) ò§ÑòÖ·ºÃ£ºÊÔ, º‡ òÈÚ kilometersòµÚ Œ®ÒòµÃ óÔúƒ®óÊ‚óÕÞºË⁄.
ºÓ–º¡µóÌÌòµ… òÙÅòµÌ

  òµ· ºƒÃ  òÕÔ óÚ‚  º‡·òÕÈò‰ÔºÓÞ  
º†ÈóÚ‚ò×ÑòÖò×кÁ©ò§Þò¡®óÜÌò©Þ    º“’
75GoodWolf2001-01-29646   NeXTòÙÐòÃÞ òÕÃóÌÌò„Úº×Ñ phpº×Ñ MysQL..òÖ·ºÖ»Œ†Ì óÕÚºÁÚòÙ‹?? (1)
74ò¦ÞòÊ„óµ‹2001-01-12687   ºÁÚò×ÚºÁÞ º§Îòµ· òµÑòÝÐóÕ· ò£…òÂòÜÔ (2)
73ò¦ÞòÊ„óµ‹2001-01-29646   òÕ…ºÏÛ Œ® ºÊÔŒ‚‚.... Œ•‚Œ†Âòµ· 200mºÁÚ ??
72ºÁÚòÚÀò†©2001-01-21632   ABookOnC º¥… òÚ‹ºƒ® òµ·òÞ©
71ºÁÚòÚÀò†©2001-01-21611   ABookOnC Chapter 6 - 6.2 Source code Œ„Ìò§ 
70ºÁÚòÚÀò†©2001-01-21494   ABookOnC Chapter 5 - 5.4
69ºÁÚòÚÀò†©2001-01-21902   ABookOnC Chapter 1 - 1.3
68onion2001-01-16654   [òסºË·] òÕÃòµ· XXóÌÔº©⁄ºµ…....ò”‹ò”‹ò”‹...
67ºÁÚòÚÀò†©2001-01-16660   òÛÞ ò©Þ Œ‚ òÙÐ ºÌ óÕÞ ºË¦òµ  òÕÈóÕ·ò£…òËÞºÁÚòÜÔ?? (1)
66ò¦ÞòÊ„óµ‹2001-01-16727   òË‹Œ®ÀóÕÞ Œ‡©ºfl…..òµ·Œ–‚ òÖ·ºÖ»Œ†Ì óÒ òÖ·òÕ… óÕÚò§ ? (1)
65onion2001-01-15769   [òסºË·] Œ®ÃºË¤óÕÚŒ†Ì óÕÞŒ‚  òÁ‹òÜ’º†ÕòµÃ ò©Þ óÛÈòÙÐ òÚ‹º©‚òʦºËȺË⁄.. (1)
64onion2001-01-14739   [ò§Èº‹‚] openstep òÙÐòÃÞ óÕÞŒ‚ òµ·òÜÔ... (11)
63ºÁÚòÚÀò†©2001-01-13759   ºÃ¥òÊ⁄óÊ‚òÙÐòÃÞ óÕÞŒ‚  óÏ®óÊ‚.... (2)
62ò¦ÞòÊ„óµ‹2001-01-10747   òÚ⁄ºÊÚ G400 ºÓ òÖ…óÙ⁄ºÓÞŒ®  ºÁÚòµÚ ºÜÞŒ»ÑòµÃ òÙ·ºË⁄... (4)
61Œ„ ò§ÃòÈÚ2001-01-12609   òʄ󵋺Ïà óÛÈóÎÚòµ·ò¥Ðº¥… º§ÌºÓÞºÊÔŒ¦‹ºÁÚ... (3)
íÙ  [41] [42] [43] [44] [45] [46] [47] [48] [49] [50] íÖ¶
[º“’º¡µº‡·Œ‚®] [Œ‚ òÓ®Œ‚®]