Thinking in c volume 1 - 2nd edition - phần 3

Chúng tôi đã nhìn thấy các kết quả của việc có những thử nghiệm này được xây dựng vào ngôn ngữ: người đã có thể viết các hệ thống phức tạp hơn, và có được họ làm việc, với thời gian và nỗ lực ít hơn nhiều | . int scp2 scp2 visible here . scpl scp2 still visible here . int scp3 scpl scp2 scp3 visible here . -- scp3 destroyed here scp3 not available here scp1 scp2 still visible here . -- scp2 destroyed here scp3 scp2 not available here scp1 still visible here . -- scp1 destroyed here The example above shows when variables are visible and when they are unavailable that is when they go out of scope . A variable can be used only when inside its scope. Scopes can be nested indicated by matched pairs of braces inside other matched pairs of braces. Nesting means that you can access a variable in a scope that encloses the scope you are in. In the example above the variable scpl is available inside all of the other scopes while scp3 is available only in the innermost scope. Defining variables on the fly As noted earlier in this chapter there is a significant difference between C and C when defining variables. Both languages require that variables be defined before they are used but C and many other traditional procedural languages forces you to define all the variables at the beginning of a scope so that when the compiler creates a block it can allocate space for those variables. While reading C code a block of variable definitions is usually the first thing you see when entering a scope. Declaring all variables at 156 Thinking in C www. BruceEckel .com the beginning of the block requires the programmer to write in a particular way because of the implementation details of the language. Most people don t know all the variables they are going to use before they write the code so they must keep jumping back to the beginning of the block to insert new variables which is awkward and causes errors. These variable definitions don t usually mean much to the reader and they actually tend to be confusing because they appear apart from the context in which they are used. C not C allows you to define variables anywhere in a scope so you can define a variable right before you use it. In .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.