版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、A First Book of ANSI CFourth EditionChapter 2Getting Started in C ProgrammingA First Book of ANSI C, Fourth Edition2Objectives Introduction to C Programming Programming Style Data Types Arithmetic OperationsA First Book of ANSI C, Fourth Edition3Objectives (continued) Variables and Declarations Case
2、 Study: Temperature Conversion Common Programming and Compiler ErrorsA First Book of ANSI C, Fourth Edition4Introduction to C ProgrammingA First Book of ANSI C, Fourth Edition5Introduction to C Programming (continued) C provides a comprehensive set of functions Stored in a set of files known as the
3、standard library The standard library consists of 15 header filesA First Book of ANSI C, Fourth Edition6Introduction to C Programming (continued)A First Book of ANSI C, Fourth Edition7Introduction to C Programming (continued)IdentifiersA First Book of ANSI C, Fourth Edition8Identifiers Identifiers i
4、n C consist of three types: Reserved words Standard identifiers Programmer-created identifiersA First Book of ANSI C, Fourth Edition9Identifiers (continued) Reserved word: word that is predefined by the programming language for a special purpose and can only be used in a specified manner for its int
5、ended purpose Also referred to as keywords in CA First Book of ANSI C, Fourth Edition10Identifiers (continued)A First Book of ANSI C, Fourth Edition11Identifiers (continued) Standard identifiers: words predefined in C Most of the standard identifiers are the names of functions that are provided in t
6、he C standard library It is good programming practice to use standard identifiers only for their intended purposeA First Book of ANSI C, Fourth Edition12Identifiers (continued)A First Book of ANSI C, Fourth Edition13Identifiers (continued) Programmer-created identifiers: selected by the programmer A
7、lso called programmer-created names Used for naming data and functions Must conform to Cs identifier rules Can be any combination of letters, digits, or underscores (_) subject to the following rules: First character must be a letter or underscore (_) Only letters, digits, or underscores may follow
8、the initial character Blank spaces are not allowed Cannot be a reserved wordA First Book of ANSI C, Fourth Edition14Identifiers (continued) Examples of invalid C programmer-created names: 4ab7 calculate total while All uppercase letters used to indicate a constant A function name must be followed by
9、 parentheses An identifier should be descriptive: degToRadians() Bad identifier choices: easy, duh, justDoIt C is a case-sensitive language TOTAL, and total represent different identifiersA First Book of ANSI C, Fourth Edition15The main() FunctionSometimes referred to as a driver functionA First Boo
10、k of ANSI C, Fourth Edition16The main() Function (continued)Function header line Executable statementsA First Book of ANSI C, Fourth Edition17The printf() Function printf() formats data and sends it to the standard system display device (i.e., the monitor) Inputting data or messages to a function is
11、 called passing data to the function printf(Hello there world!); Syntax: set of rules for formulating statements that are “grammatically correct” for the language Messages are known as strings in C A string of characters is surrounded by double quotesprintf(Hello there world!);A First Book of ANSI C
12、, Fourth Edition18The printf() Function (continued)Function argumentsA First Book of ANSI C, Fourth Edition19The printf() Function (continued) CommentPreprocessor commandHeader fileInvoking or calling the printf() functionA First Book of ANSI C, Fourth Edition20The printf() Function (continued)Outpu
13、t is:Computers, computers everywhereas far as I can CNewline escape sequenceA First Book of ANSI C, Fourth Edition21Programming Style: Indentation Except for strings, function names, and reserved words, C ignores all white space White space: any combination of one or more blank spaces, tabs, or new
14、lines In standard form: A function name is placed, with the parentheses, on a line by itself starting at the left-hand corner The opening brace follows on the next line, under the first letter of the function name The closing function brace is placed by itself at the start of the last line of the fu
15、nctionA First Book of ANSI C, Fourth Edition22Programming Style: Indentation (continued) Within the function itself, all program statements are indented two spaces Indentation is another sign of good programming practice, especially if the same indentation is used for similar groups of statements Do
16、nt do this:intmain()printf(Hello there world!);return 0;A First Book of ANSI C, Fourth Edition23Programming Style: Comments Comments help clarify what a program does, what a group of statements is meant to accomplish, etc. The symbols /*, with no white space between them, designate the start of a co
17、mment; the symbols */ designate the end of a comment/* this is a comment */ Comments can be placed anywhere within a program and have no effect on program execution Under no circumstances may comments be nested/* this comment is /* always */ invalid */A First Book of ANSI C, Fourth Edition24Programm
18、ing Style: Comments (continued)A First Book of ANSI C, Fourth Edition25Data Types Data type: set of values and a set of operations that can be applied to these values Built-in data type: is provided as an integral part of the language; also known as primitive typeA First Book of ANSI C, Fourth Editi
19、on26Data Types (continued)A First Book of ANSI C, Fourth Edition27Data Types (continued) A literal is an acceptable value for a data type Also called a literal value or constant 2, 3.6, 8.2, and Hello World! are literal values because they literally display their valuesA First Book of ANSI C, Fourth
20、 Edition28Data Types (continued)A First Book of ANSI C, Fourth Edition29Integer Data TypesA First Book of ANSI C, Fourth Edition30Integer Data Types (continued) int: whole numbers (integers) For example: 0, -10, 253, -26351 Not allowed: commas, decimal points, special symbols char: stores individual
21、 characters (ASCII) For example: A, $, b, !A First Book of ANSI C, Fourth Edition31Integer Data Types (continued)A First Book of ANSI C, Fourth Edition32Integer Data Types (continued)A First Book of ANSI C, Fourth Edition33Integer Data Types (continued)A First Book of ANSI C, Fourth Edition34Floatin
22、g-Point Data Types A floating-point value (real number) can be the number zero or any positive or negative number that contains a decimal point For example: +10.625, 5., -6.2, 3251.92, +2 Not allowed: commas, decimal points, special symbols float: single-precision number double: double-precision num
23、ber Storage allocation for each data type depends on the compiler (use sizeof()A First Book of ANSI C, Fourth Edition35Floating-Point Data Types (continued) float literal is indicated by appending an f or F long double is created by appending an l or L 9.234 indicates a double literal 9.234f indicat
24、es a float literal 9.234L indicates a long double literalA First Book of ANSI C, Fourth Edition36Floating-Point Data Types (continued)A First Book of ANSI C, Fourth Edition37Exponential Notation In numerical theory, the term precision typically refers to numerical accuracyA First Book of ANSI C, Fou
25、rth Edition38Exponential Notation (continued)A First Book of ANSI C, Fourth Edition39Arithmetic Operations Arithmetic operators: operators used for arithmetic operations: Addition + Subtraction - Multiplication * Division / Modulus Division % Binary operators require two operands An operand can be e
26、ither a literal value or an identifier that has a value associated with itA First Book of ANSI C, Fourth Edition40Arithmetic Operations (continued) A simple binary arithmetic expression consists of a binary arithmetic operator connecting two literal values in the form: literalValue operator literalV
27、alue 3 + 7 12.62 - 9.8 .08 * 12.2 12.6 / 2. Spaces around arithmetic operators are inserted for clarity and can be omitted without affecting the value of the expressionA First Book of ANSI C, Fourth Edition41Displaying Numerical Values Arguments are separated with commas printf(The total of 6 and 15
28、 is %d, 6 + 15); First argument of printf() must be a string A string that includes a conversion control sequence, such as %d, is termed a control string Conversion control sequences are also called conversion specifications and format specifiers printf() replaces a format specifier in its control s
29、tring with the value of the next argument In this case, 21A First Book of ANSI C, Fourth Edition42Displaying Numerical Values (continued) printf(The total of 6 and 15 is %d, 6 + 15); The total of 6 and 15 is 21 printf (The sum of %f and %f is %f, 12.2, 15.754, 12.2 + 15.754); The sum of 12.200000 an
30、d 15.754000 is 27.954000A First Book of ANSI C, Fourth Edition43Displaying Numerical Values (continued)A First Book of ANSI C, Fourth Edition44Displaying Numerical Values (continued)A First Book of ANSI C, Fourth Edition45Displaying Numerical Values (continued)A First Book of ANSI C, Fourth Edition4
31、6Expression Types Expression: any combination of operators and operands that can be evaluated to yield a value Integer expression: contains only integer operands; the result is an integer Floating-point expression: contains only floating-point operands; the result is a double-precision In a mixed-mo
32、de expression the data type of each operation is determined by the following rules: If both operands are integers, result is an integer If one operand is real, result is double-precisionA First Book of ANSI C, Fourth Edition47Integer Division 15/2 = 7 Integers cannot contain a fractional part Remain
33、der is truncated % is the modulus or remainder operator 9 % 4 is 1 17 % 3 is 2 14 % 2 is 0A First Book of ANSI C, Fourth Edition48Negation A unary operator is one that operates on a single operand, e.g., negation (-) The minus sign in front of a single numerical value negates (reverses the sign of)
34、the numberA First Book of ANSI C, Fourth Edition49Negation (continued)A First Book of ANSI C, Fourth Edition50Operator Precedence and Associativity Two binary arithmetic operator symbols must never be placed side by side Parentheses may be used to form groupings Expressions in parentheses are evalua
35、ted first Parentheses may be enclosed by other parentheses Parentheses cannot be used to indicate multiplicationA First Book of ANSI C, Fourth Edition51Operator Precedence and Associativity (continued) Three levels of precedence:1.All negations are done first2.Multiplication, division, and modulus o
36、perations are computed next; expressions containing more than one of these operators are evaluated from left to right as each operator is encountered3.Addition and subtraction are computed last; expressions containing more than one addition or subtraction are evaluated from left to right as each ope
37、rator is encounteredA First Book of ANSI C, Fourth Edition52Operator Precedence and Associativity (continued) Example:8 + 5 * 7 % 2 * 4 = 8 + 35 % 2 * 4 = 8 + 1 * 4 = 8 + 4 = 12A First Book of ANSI C, Fourth Edition53Operator Precedence and Associativity (continued)A First Book of ANSI C, Fourth Edi
38、tion54Variables and Declarations Variables are names given by programmers to computer storage Variable name usually limited to 255 characters Variable names are case sensitiveA First Book of ANSI C, Fourth Edition55Variables and Declarations (continued)A First Book of ANSI C, Fourth Edition56Variabl
39、es and Declarations (continued)num1 = 45;num2 = 12;total = num1 + num2; Assignment statementsA First Book of ANSI C, Fourth Edition57Variables and Declarations (continued)A First Book of ANSI C, Fourth Edition58Declaration Statements Naming and specifying the data type that can be stored in each var
40、iable is accomplished using declaration statements Declaration statements within a function appear immediately after the opening brace of a functionfunction name() declaration statements; other statements; Definition statements define or tell the compiler how much memory is needed for data storageA
41、First Book of ANSI C, Fourth Edition59Declaration Statements (continued)A First Book of ANSI C, Fourth Edition60Declaration Statements (continued)A First Book of ANSI C, Fourth Edition61Declaration Statements (continued)A First Book of ANSI C, Fourth Edition62Declaration Statements (continued)A Firs
42、t Book of ANSI C, Fourth Edition63Declaration Statements (continued)You can omit the f and let the compiler convert the double precision value into a float value when the assignment is madeA First Book of ANSI C, Fourth Edition64Selecting Variable Names Make variable names descriptive Limit variable
43、 names to approximately 20 characters Start the variable name with a letter, rather than an underscore (_) In a variable name consisting of several words, capitalize the first letter of each word after the firstA First Book of ANSI C, Fourth Edition65Selecting Variable Names (continued) Use variable
44、 names that indicate what the variable corresponds to, rather than how it is computed Add qualifiers, such as Avg, Min, Max, and Sum to complete a variables name where appropriate Use single-letter variable names, such as i, j, and k, for loop indexesA First Book of ANSI C, Fourth Edition66Initializ
45、ation Declaration statements can be used to store an initial value into declared variables int numOne = 15; When a declaration statement provides an initial value, the variable is said to be initialized Literals, expressions using only literals such as 87.0 + 12 2, and expressions using literals and
46、 previously initialized variables can all be used as initializers within a declaration statementA First Book of ANSI C, Fourth Edition67Case Study: Temperature Conversion A friend of yours is going to Spain, where temperatures are reported using the Celsius temperature scale. She has asked you to pr
47、ovide her with a list of temperatures in degrees Fahrenheit, and the equivalent temperature in degrees Celsius. The formula relating the two temperatures is Celsius = 5/9(Fahrenheit 32). Initially, you are to write and test a program that correctly converts the Fahrenheit temperature of 75 degrees i
48、nto its Celsius equivalent.A First Book of ANSI C, Fourth Edition68Case Study: Temperature Conversion (continued)A First Book of ANSI C, Fourth Edition69Common Programming Errors Omitting the parentheses, (), after main Omitting or incorrectly typing the opening brace, , that signifies the start of
49、a function body Omitting or incorrectly typing the closing brace, , that signifies the end of a function Misspelling the name of a function; for example, typing print() instead of printf() Forgetting to close a string passed to printf() with a double quote symbolA First Book of ANSI C, Fourth Editio
50、n70Common Programming Errors (continued) Omitting the semicolon at the end of each executable statement Forgetting to include n to indicate a new line Forgetting to declare all the variables used in a program Storing an incorrect data type in a declared variable Using a variable in an expression before a value has been assigned to the variableA First Book of ANSI C, Fourth Edition71Common Programming Errors (continued) Dividing integer values incorrectly Mixing data typ
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 初三生活指南模板
- 財務(wù)風(fēng)險管理報告模板
- 家屬追悼會致辭范文六篇
- 課程設(shè)計營銷
- 2024年幼兒園中班語言教案含反思
- 二零二五年度面包磚施工安全生產(chǎn)責(zé)任合同4篇
- 2024年心理咨詢師題庫及完整答案(易錯題)
- 二零二五年社區(qū)圖書館圖書采購合同2篇
- 二零二五年度在線教育平臺學(xué)員免責(zé)協(xié)議書范本4篇
- 高分子防水卷材施工方案
- 2024年醫(yī)銷售藥銷售工作總結(jié)
- GB/T 44888-2024政務(wù)服務(wù)大廳智能化建設(shè)指南
- 2023-2024學(xué)年江西省萍鄉(xiāng)市八年級(上)期末物理試卷
- 四則混合運算100道題四年級上冊及答案
- 四川省高職單招電氣技術(shù)類《電子基礎(chǔ)》歷年考試真題試題庫(含答案)
- 2024年江西生物科技職業(yè)學(xué)院單招職業(yè)技能測試題庫帶解析答案
- 橋本甲狀腺炎-90天治療方案
- (2024年)安全注射培訓(xùn)課件
- 2024版《建設(shè)工程開工、停工、復(fù)工安全管理臺賬表格(流程圖、申請表、報審表、考核表、通知單等)》模版
- 酒店人防管理制度
- 油田酸化工藝技術(shù)
評論
0/150
提交評論