Reading Notes: C Programming-modern methods

Source: Internet
Author: User
C Programming-modern method this C language book is a good C language book. The explanations are well organized, and the explanations behind each chapter really answer many of my questions.

Chapter 2 Basic concepts of C Language
1. In some C language books, the end of the main function is exit (0), rather than return 0. Are the two identical?
When it appears in the main function, the two statements are completely equivalent: Both terminate the program execution and return 0 to the operating system.
2. Why does a float type name come from?
Float is the abbreviation of floating-point. Some languages are called real

Chapter 4 formatting input/output
1. The conversion description % I can also be used to read and write integers. What is the difference between % I and % d?
When used in the printf format string, there is no difference between the two. however, in the scanf format, % d can only match an integer in decimal format, while % I can match octal, decimal, and hexadecimal.
2. If % is displayed in printf, % can be used.
3. If V has side effects, then V + = E and V = V + E are not equivalent.
For example:
A [I ++] + = 2 and a [I ++] = A [I ++] + 2
4. C language inherits ++ and -- operations from the B language of Ken Thompson in the early days. Thompson improved the BCPL language and created the B Language, he created the ++ operator because in the B Language compiler, ++ I can produce more concise translations than I = I + 1.

Chapter 1 Circulation
1. Which infinite loop format is more desirable, while (1) or (;;)?
C programmers traditionally like the efficiency of for (;), because early compilers often force the program to test condition 1 every time a while loop body is executed. but for modern compilers, there should be no difference in performance between the two infinite loops.
2. I heard that programmers should never use the continue statement. Is that true?
The continue statement is rarely used. However, the continue statement is sometimes very convenient.
Chapter 1 Basic Types
1. Why are floating-point constants stored in the double format instead of the float format?
For historical reasons, the C language is more inclined to use the double type, while the float type is regarded as a "second-class citizen ".
2. Why to use % lf to read values of the double type and display them with % F?
This is a very difficult question to answer. first, note that the scanf and prinf functions are common functions because they do not limit the function parameters to a fixed number. scanf and printf functions have variable-length parameter lists. when a function with a variable length parameter list is called, the compiler automatically converts the float parameter to the double type. The result is that the printf function cannot distinguish the float type from the double type. this explains why % F can be used to represent both float and double parameters in printf function calls.
On the other hand, the scanf function points to a variable through a pointer. % F tells the scanf function to store a float value on the passed address, while % lf tells the scanf function to store a double value on the address. here the difference between float and double is very important. if an incorrect conversion description is provided, the scanf function may store the number of wrong bytes (not mentioned, the float bit mode may be different from the double bit mode)

Chapter 2 array
1. The following program may cause infinite loops.
Int A [10], I;
For (I = 0; I <= 10; I ++)
A [I] = 0;
2. Is the initialization of this array legal?
Int A [] = {, [0] = };
Valid. working principle: when processing the initialization list, the compiler records the location of the next array element to be initialized. normally, the next element is the one after the initialized element. however, when the initialization type appears in the list, the next element is forced to be the corresponding element of the indicator, even if the element has been initialized.
The initialization effect is similar to: int A [] = {5, 7 };
The array length is 4.
3. How can I copy an array to another array?
(1) For Loop Method
For (I = 0; I <n; I ++)
A [I] = B [I];
(2) Use <string. h> the function memcpy in the header. this function is an underlying function that simply copies bytes from one place to another. to copy array B to array A, the format of the memcpy function is as follows:
Memcpy (a, B, sizeof ());
4. In function call f (a, B), how does the compiler know whether a comma is a punctuation mark or an operator number?
The actual parameter in a function call cannot be any expression, but must be a "value assignment expression ". in the value assignment expression, you cannot use a comma as an operator unless the comma is in parentheses. in other words, when the function calls f (,
B), the comma is the punctuation mark, and in F (a, B), the comma is the operator.

5. If several functions have the same return type, can they merge their declarations? For example, void print_pun (void), print_count (int n); is it legal?
It is legal. In fact, the C language can even combine the function declaration and variable declaration:
Double X, Y, average (double A, double B );
6. Why does the parameter of the first dimension in the array not be described, but other dimensions must be described?
First, you need to know how the C language transmits an array. When passing an array to a function, the pointer pointing to the first element of the array is given to the function.
Second, you need to know how the subscript operator works. the C language stores arrays in the Primary Order of rows. to calculate the pointer of the next element, you must know the size of an element. Therefore, you must provide the size of other dimensions to know the size of a row.
7. Indirect recursion: F1 calls F2 and F2 calls F1, which involves the following: declaring the function in advance.

Chapter 2 program structure
1. Local Variables
Features: (1) Automatic Storage Duration: the storage unit of a local variable is automatically allocated when the function containing the variable is called, and the allocation is withdrawn when the function returns, therefore, this variable has an automatic storage period.
(2) block Scope
Static local variables:
A variable with a static storage duration has a permanent storage unit, and the value of the variable is retained throughout the execution of the program. That is, the memory unit occupied by the variable remains unchanged during the execution of the program.
However, it is a block scope, that is, it is hidden from other functions, but it will retain the data for future calls of the same function.
Format parameters:
Formal parameters have the same nature as local variables, that is, Automatic Storage Period and block scope. in fact, the only real difference between a formal parameter and a local variable is that the formal parameter is automatically initialized during each function call.
2. external variables (global variables)
Features: (1) static storage duration:
(2) File Scope: from the declared point of the variable to the end of the file
3. What is the impact of local variables with a static storage duration on recursive functions?
When a function is a recursive function, it will generate a new copy of its automatic variables every time it is called. This is not the case with static variables. On the contrary, all recursive functions share a static variable.

Chapter 2 pointer
1. Const int * P;
Int const * P;
Int * const P;
The first two are the same, and they are not allowed to modify the value pointed to by P. The first 3rd are pointer P, which cannot be changed.

Chapter 2 string
1. Use the printf and puts functions to write strings.
The printf function writes characters in strings one by one until null characters are encountered. (If the null character is lost, the printf function will continue to write the string beyond the end, knowing that the empty character is found somewhere in the memory ).
====== Here you can perform an experiment, manually modify the data in the memory, and then ....
2. Use the scanf and gets functions to read strings.
(1) The gets function does not skip blank characters before starting to read the string (the scanf function will skip this step, and the string read by scanf will never contain blank characters)
(2) The gets function will continue to read until the line break is found (the scanf function will stop at any blank character ). in addition, the gets function ignores the line break and does not store it in the array. It replaces the line break with null characters.
3. String function library
Strcpy, strncpy, strlen, strcat, strcmp
4. Why not call a string literal as a "String constant "?
Because they are not necessarily constants. Because the string literal is accessed through a pointer, there is no way to prevent the program from modifying the characters in the string literal.
5. Some compilers try to save memory by storing only one copy of the same string literal.
Char * P = "ABC", * q = "ABC ";
The compiler may only store "ABC" once. Modifications may cause program problems.
Chapter 4 pre-processor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.