版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、A First Book of ANSI CFourth Edition,Chapter 2 Getting Started in C Programming,A First Book of ANSI C, Fourth Edition,2,Objectives,Introduction to C Programming Programming Style Data Types Arithmetic Operations,A First Book of ANSI C, Fourth Edition,3,Objectives (continued),Variables and Declarati
2、ons Case Study: Temperature Conversion Common Programming and Compiler Errors,A First Book of ANSI C, Fourth Edition,4,Introduction to C Programming,A First Book of ANSI C, Fourth Edition,5,Introduction to C Programming (continued),C provides a comprehensive set of functions Stored in a set of files
3、 known as the standard library The standard library consists of 15 header files,A First Book of ANSI C, Fourth Edition,6,Introduction to C Programming (continued),A First Book of ANSI C, Fourth Edition,7,Introduction to C Programming (continued),A First Book of ANSI C, Fourth Edition,8,Identifiers,I
4、dentifiers in C consist of three types: Reserved words Standard identifiers Programmer-created identifiers,A First Book of ANSI C, Fourth Edition,9,Identifiers (continued),Reserved word: word that is predefined by the programming language for a special purpose and can only be used in a specified man
5、ner for its intended purpose Also referred to as keywords in C,A First Book of ANSI C, Fourth Edition,10,Identifiers (continued),A First Book of ANSI C, Fourth Edition,11,Identifiers (continued),Standard identifiers: words predefined in C Most of the standard identifiers are the names of functions t
6、hat are provided in the C standard library It is good programming practice to use standard identifiers only for their intended purpose,A First Book of ANSI C, Fourth Edition,12,Identifiers (continued),A First Book of ANSI C, Fourth Edition,13,Identifiers (continued),Programmer-created identifiers: s
7、elected by the programmer Also 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,
8、 or underscores may follow the initial character Blank spaces are not allowed Cannot be a reserved word,A First Book of ANSI C, Fourth Edition,14,Identifiers (continued),Examples of invalid C programmer-created names: 4ab7 calculate total while All uppercase letters used to indicate a constant A fun
9、ction name must be followed by parentheses An identifier should be descriptive: degToRadians() Bad identifier choices: easy, duh, justDoIt C is a case-sensitive language TOTAL, and total represent different identifiers,A First Book of ANSI C, Fourth Edition,15,The main() Function,A First Book of ANS
10、I C, Fourth Edition,16,The main() Function (continued),Function header line,Executable statements,A First Book of ANSI C, Fourth Edition,17,The 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 c
11、alled 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 quotes printf(Hello there world!);,A First Book of ANSI C
12、, Fourth Edition,18,The printf() Function (continued),Function arguments,A First Book of ANSI C, Fourth Edition,19,The printf() Function (continued),Comment,Preprocessor command,Header file,Invoking or calling the printf() function,A First Book of ANSI C, Fourth Edition,20,The printf() Function (con
13、tinued),Output is: Computers, computers everywhere as far as I can C,Newline escape sequence,A First Book of ANSI C, Fourth Edition,21,Programming Style: Indentation,Except for strings, function names, and reserved words, C ignores all white space White space: any combination of one or more blank sp
14、aces, tabs, or new 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
15、last line of the function,A First Book of ANSI C, Fourth Edition,22,Programming 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 g
16、roups of statements Dont do this: int main ( )printf (Hello there world! );return 0;,A First Book of ANSI C, Fourth Edition,23,Programming 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 th
17、em, designate the start of a comment; 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 o
18、f ANSI C, Fourth Edition,24,Programming Style: Comments (continued),A First Book of ANSI C, Fourth Edition,25,Data 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 t
19、ype,A First Book of ANSI C, Fourth Edition,26,Data Types (continued),A First Book of ANSI C, Fourth Edition,27,Data 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 displ
20、ay their values,A First Book of ANSI C, Fourth Edition,28,Data Types (continued),A First Book of ANSI C, Fourth Edition,29,Integer Data Types,A First Book of ANSI C, Fourth Edition,30,Integer Data Types (continued),int: whole numbers (integers) For example: 0, -10, 253, -26351 Not allowed: commas, d
21、ecimal points, special symbols char: stores individual characters (ASCII) For example: A, $, b, !,A First Book of ANSI C, Fourth Edition,31,Integer Data Types (continued),A First Book of ANSI C, Fourth Edition,32,Integer Data Types (continued),A First Book of ANSI C, Fourth Edition,33,Integer Data T
22、ypes (continued),A First Book of ANSI C, Fourth Edition,34,Floating-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 s
23、ymbols float: single-precision number double: double-precision number Storage allocation for each data type depends on the compiler (use sizeof(),A First Book of ANSI C, Fourth Edition,35,Floating-Point Data Types (continued),float literal is indicated by appending an f or F long double is created b
24、y appending an l or L 9.234 indicates a double literal 9.234f indicates a float literal 9.234L indicates a long double literal,A First Book of ANSI C, Fourth Edition,36,Floating-Point Data Types (continued),A First Book of ANSI C, Fourth Edition,37,Exponential Notation,In numerical theory, the term
25、precision typically refers to numerical accuracy,A First Book of ANSI C, Fourth Edition,38,Exponential Notation (continued),A First Book of ANSI C, Fourth Edition,39,Arithmetic Operations,Arithmetic operators: operators used for arithmetic operations: Addition + Subtraction - Multiplication * Divisi
26、on / Modulus Division % Binary operators require two operands An operand can be either a literal value or an identifier that has a value associated with it,A First Book of ANSI C, Fourth Edition,40,Arithmetic Operations (continued),A simple binary arithmetic expression consists of a binary arithmeti
27、c operator connecting two literal values in the form: literalValue operator literalValue 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 expression,A First Book of ANSI C, Fourth Edition,41,Displ
28、aying Numerical Values,Arguments are separated with commas printf(The total of 6 and 15 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 spe
29、cifications and format specifiers printf() replaces a format specifier in its control string with the value of the next argument In this case, 21,A First Book of ANSI C, Fourth Edition,42,Displaying Numerical Values (continued),printf(The total of 6 and 15 is %d, 6 + 15); The total of 6 and 15 is 21
30、 printf (The sum of %f and %f is %f, 12.2, 15.754, 12.2 + 15.754); The sum of 12.200000 and 15.754000 is 27.954000,A First Book of ANSI C, Fourth Edition,43,Displaying Numerical Values (continued),A First Book of ANSI C, Fourth Edition,44,Displaying Numerical Values (continued),A First Book of ANSI
31、C, Fourth Edition,45,Displaying Numerical Values (continued),A First Book of ANSI C, Fourth Edition,46,Expression 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-p
32、oint expression: contains only floating-point operands; the result is a double-precision In a mixed-mode 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-precision,A First Boo
33、k of ANSI C, Fourth Edition,47,Integer Division,15/2 = 7 Integers cannot contain a fractional part Remainder is truncated % is the modulus or remainder operator 9 % 4 is 1 17 % 3 is 2 14 % 2 is 0,A First Book of ANSI C, Fourth Edition,48,Negation,A unary operator is one that operates on a single ope
34、rand, e.g., negation (-) The minus sign in front of a single numerical value negates (reverses the sign of) the number,A First Book of ANSI C, Fourth Edition,49,Negation (continued),A First Book of ANSI C, Fourth Edition,50,Operator Precedence and Associativity,Two binary arithmetic operator symbols
35、 must never be placed side by side Parentheses may be used to form groupings Expressions in parentheses are evaluated first Parentheses may be enclosed by other parentheses Parentheses cannot be used to indicate multiplication,A First Book of ANSI C, Fourth Edition,51,Operator Precedence and Associa
36、tivity (continued),Three levels of precedence: All negations are done first Multiplication, division, and modulus operations are computed next; expressions containing more than one of these operators are evaluated from left to right as each operator is encountered Addition and subtraction are comput
37、ed last; expressions containing more than one addition or subtraction are evaluated from left to right as each operator is encountered,A First Book of ANSI C, Fourth Edition,52,Operator Precedence and Associativity (continued),Example: 8 + 5 * 7 % 2 * 4 = 8 + 35 % 2 * 4 = 8 + 1 * 4 = 8 + 4 = 12,A Fi
38、rst Book of ANSI C, Fourth Edition,53,Operator Precedence and Associativity (continued),A First Book of ANSI C, Fourth Edition,54,Variables and Declarations,Variables are names given by programmers to computer storage Variable name usually limited to 255 characters Variable names are case sensitive,
39、A First Book of ANSI C, Fourth Edition,55,Variables and Declarations (continued),A First Book of ANSI C, Fourth Edition,56,Variables and Declarations (continued),num1 = 45; num2 = 12; total = num1 + num2;,Assignment statements,A First Book of ANSI C, Fourth Edition,57,Variables and Declarations (con
40、tinued),A First Book of ANSI C, Fourth Edition,58,Declaration Statements,Naming and specifying the data type that can be stored in each variable is accomplished using declaration statements Declaration statements within a function appear immediately after the opening brace of a function function nam
41、e() declaration statements; other statements; Definition statements define or tell the compiler how much memory is needed for data storage,A First Book of ANSI C, Fourth Edition,59,Declaration Statements (continued),A First Book of ANSI C, Fourth Edition,60,Declaration Statements (continued),A First
42、 Book of ANSI C, Fourth Edition,61,Declaration Statements (continued),A First Book of ANSI C, Fourth Edition,62,Declaration Statements (continued),A First Book of ANSI C, Fourth Edition,63,Declaration Statements (continued),You can omit the f and let the compiler convert the double precision value i
43、nto a float value when the assignment is made,A First Book of ANSI C, Fourth Edition,64,Selecting Variable Names,Make variable names descriptive Limit variable names to approximately 20 characters Start the variable name with a letter, rather than an underscore (_) In a variable name consisting of s
44、everal words, capitalize the first letter of each word after the first,A First Book of ANSI C, Fourth Edition,65,Selecting Variable Names (continued),Use variable names that indicate what the variable corresponds to, rather than how it is computed Add qualifiers, such as Avg, Min, Max, and Sum to co
45、mplete a variables name where appropriate Use single-letter variable names, such as i, j, and k, for loop indexes,A First Book of ANSI C, Fourth Edition,66,Initialization,Declaration statements can be used to store an initial value into declared variables int numOne = 15; When a declaration statemen
46、t 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 previously initialized variables can all be used as initializers within a declaration statement,A First Book of ANSI C, Fourth Edition
47、,67,Case 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 provide her with a list of temperatures in degrees Fahrenheit, and the equivalent temperature in degrees Celsius. The formula relating
48、 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 into its Celsius equivalent.,A First Book of ANSI C, Fourth Edition,68,Case Study: Temperature Conversion (continued),A First Book of
49、 ANSI C, Fourth Edition,69,Common Programming Errors,Omitting the parentheses, (), after main Omitting or incorrectly typing the opening brace, , that signifies the start of a function body Omitting or incorrectly typing the closing brace, , that signifies the end of a function Misspelling the name
50、of a function; for example, typing print() instead of printf() Forgetting to close a string passed to printf() with a double quote symbol,A First Book of ANSI C, Fourth Edition,70,Common Programming Errors (continued),Omitting the semicolon at the end of each executable statement Forgetting to inclu
51、de 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 variable,A First Book of ANSI C, Fourth Edition,71,Common Programming Errors (continued),Dividing integer values incorrectly Mixing data
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026上半年云南水利水電職業(yè)學院招聘3人參考考試試題附答案解析
- 2026年中國郵政儲蓄銀行股份有限公司普洱市分行招聘見習人員(10人)備考考試試題附答案解析
- 民品生產(chǎn)基礎檔案制度
- 2026年上半年黑龍江事業(yè)單位聯(lián)考省營商環(huán)境建設監(jiān)督局招聘6人備考考試試題附答案解析
- 2026山東濟寧市東方圣地人力資源開發(fā)有限公司招聘輔助服務人員5人備考考試試題附答案解析
- 2026廣東深圳大學深圳醫(yī)療保障研究院誠聘輔助管理1名參考考試題庫附答案解析
- 2026年鄭州大學智能集群系統(tǒng)教育部工程研究中心招聘非事業(yè)編制(勞務派遣)工作人員科研助理1名備考考試題庫附答案解析
- 集體安全課件
- 2026廣東省城鄉(xiāng)規(guī)劃設計研究院科技集團股份有限公司招聘(馬向明大師工作室)備考考試題庫附答案解析
- 2025年天津市河北區(qū)輔警人員招聘考試試題解析及答案
- 2026年河南農(nóng)業(yè)職業(yè)學院高職單招職業(yè)適應性考試參考題庫含答案解析
- 2024–2025學年度第一學期期末卷 八年級歷史(試題)
- 城市軌道交通服務員(城市軌道交通站務員)考核要素細目表與考核內(nèi)容結構表
- JBT 12530.4-2015 塑料焊縫無損檢測方法 第4部分:超聲檢測
- 江西省吉安市初中生物七年級期末下冊高分預測題詳細答案和解析
- 《中國心力衰竭診斷和治療指南2024》解讀(總)
- DZ∕T 0033-2020 固體礦產(chǎn)地質(zhì)勘查報告編寫規(guī)范(正式版)
- 瀝青拌合站方案
- (汪曉贊)運動教育課程模型
- GB/T 42677-2023鋼管無損檢測無縫和焊接鋼管表面缺欠的液體滲透檢測
- 輪機英語題庫
評論
0/150
提交評論