Lecture Introduction to computing systems (2/e): Chapter 12 - Yale N. Patt, Sanjay J. Patel

Chapter 12 - Variables and operators. This chapter presents the following content: Basic C Elements, data types, variable names, literals, scope: global and local, operators, statement, assignment operator,.and other contents. | Chapter 12 Variables and Operators Basic C Elements Variables named, typed data items Operators predefined actions performed on data items combined with variables to form expressions, statements Rules and usage Implementation using LC-2 12- Data Types C has three basic data types int integer (at least 16 bits) double floating point (at least 32 bits) char character (at least 8 bits) Exact size can vary, depending on processor int is supposed to be "natural" integer size; for LC-2, that's 16 bits -- 32 bits for most modern processors 12- Variable Names Any combination of letters, numbers, and underscore (_) Case matters "sum" is different than "Sum" Cannot begin with a number usually, variables beginning with underscore are used only in special library routines Only first 31 characters are used 12- Examples Legal i wordsPerSecond words_per_second _green aReally_longName_moreThan31chars aReally_longName_moreThan31characters Illegal 10sdigit ten'sdigit done? double reserved keyword same identifier 12- Literals Integer 123 /* decimal */ -123 0x123 /* hexadecimal */ Floating point /* x 1023 */ 5E12 /* x 1012 */ Character 'c' '\n' /* newline */ '\xA' /* ASCII 10 (0xA) */ 12- Scope: Global and Local Where is the variable accessible? Global: accessed anywhere in program Local: only accessible in a particular region Compiler infers scope from where variable is declared programmer doesn't have to explicitly state Variable is local to the block in which it is declared block defined by open and closed braces { } can access variable declared in any "containing" block Global variable is declared outside all blocks 12- Example #include int itsGlobal = 0; main() { int itsLocal = 1; /* local to main */ printf("Global %d Local %d\n", itsGlobal, itsLocal); { int itsLocal = 2; /* local to this block */ itsGlobal = 4; /* change global variable */ printf("Global %d Local %d\n", itsGlobal, itsLocal); } printf("Global %d . | Chapter 12 Variables and Operators Basic C Elements Variables named, typed data items Operators predefined actions performed on data items combined with variables to form expressions, statements Rules and usage Implementation using LC-2 12- Data Types C has three basic data types int integer (at least 16 bits) double floating point (at least 32 bits) char character (at least 8 bits) Exact size can vary, depending on processor int is supposed to be "natural" integer size; for LC-2, that's 16 bits -- 32 bits for most modern processors 12- Variable Names Any combination of letters, numbers, and underscore (_) Case matters "sum" is different than "Sum" Cannot begin with a number usually, variables beginning with underscore are used only in special library routines Only first 31 characters are used 12- Examples Legal i wordsPerSecond words_per_second _green aReally_longName_moreThan31chars aReally_longName_moreThan31characters Illegal 10sdigit ten'sdigit done? double .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
25    82    2    22-05-2024
Đã 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.