It uses an array to hold a character string.
用數組(array)儲存字符串(character string)。
It uses the %s conversion specification to handle the input and output of the string.
使用%s轉換說明來處理字符串的輸入和輸出。
A character string is a series of one or more characters. Double quotation marks inform the compiler that they enclose a string, just as single quotation marks identify a character.
字符串(character string)是一個或多個字符的序列。雙引號僅告知編譯器它括起來的是字符串,正如單引號用於標識單個字符一樣。
Strings in C are always stored with this terminating null character. The presence of the null character means that the array must have at least one more cell than the number of characters to be stored.
C中的字符串一定以空字符結束,這意味著數組的容量必須至少比待存儲字符串中的字符數多1。
An array is an ordered sequence of data elements of one type.
數組是同類型數據元素的有序序列。
char name[40];
The brackets after name identify it as an array. The 40 within the brackets indicates the number of elements in the array. The char identifies the type of each element.
char name[40];
name後面的方括號表明這是一個數組,方括號中的40表明該數組中的元素數量。char表明每個元素的類型。
After scanf() starts to read input, it stops reading at the first whitespace (blank, tab, or newline) it encounters. In general, scanf() is used with %s to read only a single word, not a whole phrase, as a string.
scanf() 在遇 到第1個空白(空格、制表符或換行符)時就不再讀取輸入。一般而言,根據%s轉換說明,scanf()只會讀取字符串中的一個單詞,而不是一整句。
The strlen() function gives the length of a string in characters. It takes one byte to hold one character. The sizeof operator gives the size of things in bytes.
strlen() 函數給出字符串中的字符長度。1 字節儲存一個字符。sizeof運算符,以字節為單位給出對象的大小。
The string.h file contains function prototypes for several string-related functions, including strlen().
string.h頭文件包含多個與字符串相關的函數原型,包括strlen()。
strlen() gives you the exact number of characters(including spaces and punctuation) in the string. The sizeof operator gives you a number one larger because it also counts the invisible null character used to end the string.
用strlen()得出的是字符串中的字符數(包括空格和標點符號)。然而,sizeof運算符給出的數更大,因為它把字符串末尾不可見的空字符也計算在內。
The C99 and C11 standards use a %zd specifier for the type used by the sizeof operator. This also applies for type returned by strlen().
C99 和 C11 標準專門為 sizeof 運算符的返回類型添加 了%zd 轉換說明,這對於strlen()同樣適用。
sizeof(char)
sizeof(float)
sizeof name
sizeof 6.28
Whether you use parentheses depends on whether you want the size of a type or the size of a particular quantity. Parentheses are required for types but are optional for particular quantities.
圓括號的使用與否取決於運算對象是類型還是特定量。運算對象是類型時,圓括號必不可少,但是對於特定量,可有可無。
symbolic constant 符號常量
Suppose you have used a constant in several places, and it becomes necessary to change its value. Then you only need to alter the definition of the symbolic constant, rather than find and change every occurrence of the constant in the program.
假設程序中的多處使用一個常量,有時需要改變它的值。如果程序使用符號常量,則只需更改符號常量的定義,不用在程序中查找使用常量的地方,然後逐一修改。
#define TAXRATE 0.015
When your program is compiled, the value 0.015 will be substituted everywhere you have used TAXRATE. This is called a compile-time substitution. Such defined constants are often termed manifest constants.
編譯程序時,程序中所有的TAXRATE都會被替換成0.015。這一過程被稱為編譯時替換(compile-time substitution)。這樣定義的常量也稱為明示常量 (manifestconstant)。
#define NAME value
No semicolon is used because this is a substitution mechanism handled by the preprocessor, not a C statement. Why isTAXRATE capitalized? It is a sensible C tradition to type constants in uppercase. Then, when you encounter one in the depths of a program, you know immediately that it is a constant, not a variable.
末尾不用加分號,因為這是一種由預處理器處理的替換機制。為什麼 TAXRATE 要用大寫?用大寫表示符號常量是 C 語言一貫的傳統。這樣,在程序中看到全大寫的名稱就立刻明白這是一個符號常量,而非變量。
C90 added a second way to create symbolic constants—using the const keyword to convert a declaration for a variable into a declaration for a constant:
const int MONTHS = 12; // MONTHS a symbolic constant for 12
This makes MONTHS into a read-only value.
C90標準新增了const關鍵字,用於限定一個變量為只讀。
The C header files limits.h and float.h supply detailed information about the size limits of integer types and floating types, respectively. Each file defines a series of manifest constants that apply to your implementation.
C頭文件limits.h和float.h分別提供了與整數類型和浮點類型大小限制相關的詳細信息。每個頭文件都定義了一系列供實現使用的明示常量。
Although printf() is an output function and scanf() is an input function, both work much the same, each using a control string and a list of arguments.
雖然printf()是輸出函數,scanf()是輸入函數,但是它們的工作原理幾乎相同。兩個函數都使用格式字符串和參數列表。
The instructions you give printf() when you ask it to print a variable depend on the variable type. We have used the %d notation when printing an integer and the %c notation when printing a character. These notations are called conversion specifications because they specify how the data is to be converted into displayable form.
請求printf()函數列印數據的指令要與待列印數據的類型相匹配。列印整數時使用%d,列印字符時使用%c。這些符號被稱為轉換說明(conversion specification),它們指定了如何把數據轉換成可顯示的形式。
You can modify a basic conversion specification by inserting modifiers between the % and the defining conversion character.
在%和轉換字符之間插入修飾符可修飾基本的轉換說明。
the field width 欄位寬度
the number of digits to the right of the decimal 小數點後面的位數
If you use scanf() to read a value for one of the basic variable types, precede the variable name with an & .If you use scanf() to read a string into a character array, don’t use an &.
如果用scanf()讀取基本變量類型的值,在變量名前加上一個&;如果用scanf()把字符串讀入字符數組中,不要使用&。
The * serves quite a different purpose for scanf(). When placed between the % and the specifier letter, it causes that function to skip over corresponding input.
scanf()中*的用法與printf()不同。把*放在%和轉換字符之間時,會使得scanf()跳過相應的輸入項。