Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.

Similar presentations


Presentation on theme: "Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C."— Presentation transcript:

1 Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C program. ❏ To introduce the include preprocessor command. ❏ To be able to create good identifiers for objects in a program. ❏ To be able to list, describe, and use the C basic data types. ❏ To be able to create and use variables and constants. ❏ To understand input and output concepts. ❏ To be able to use simple input and output statements. Chapter 2 Chapter 2 Introduction to the C Language

2 Computer Science: A Structured Programming Approach Using C2 2-1 Background C is a structured programming language. It is considered a high-level language because it allows the programmer to concentrate on the problem at hand and not worry about the machine that the program will be using. That is another reason why it is used by software developers whose applications have to run on many different hardware platforms.

3 Computer Science: A Structured Programming Approach Using C3 2-2 C Programs It's time to write your first C program. Structure of a C Program Your First C Program Comments The Greeting Program Topics discussed in this section:

4 Computer Science: A Structured Programming Approach Using C4 FIGURE 2-2 Structure of a C Program

5 Computer Science: A Structured Programming Approach Using C5 FIGURE 2-3 The Greeting Program

6 Computer Science: A Structured Programming Approach Using C6 PROGRAM 2-1The Greeting Program

7 Computer Science: A Structured Programming Approach Using C7 FIGURE 2-4 Examples of Block Comments

8 Computer Science: A Structured Programming Approach Using C8 FIGURE 2-5 Examples of Line Comments

9 Computer Science: A Structured Programming Approach Using C9 FIGURE 2-6 Nested Block Comments Are Invalid

10 Computer Science: A Structured Programming Approach Using C10 2-3 Identifiers One feature present in all computer languages is the identifier. Identifiers allow us to name data and other objects in the program. Each identified object in the computer is stored at a unique address.

11 Computer Science: A Structured Programming Approach Using C11 Table 2-1Rules for Identifiers

12 Computer Science: A Structured Programming Approach Using C12 An identifier must start with a letter or underscore: it may not have a space or a hyphen. Note

13 Computer Science: A Structured Programming Approach Using C13 C is a case-sensitive language. Note

14 Computer Science: A Structured Programming Approach Using C14 Table 2-2Examples of Valid and Invalid Names

15 Computer Science: A Structured Programming Approach Using C15 2-4 Types A type defines a set of values and a set of operations that can be applied on those values. Void Type Integral Type Floating-Point Types Topics discussed in this section:

16 Computer Science: A Structured Programming Approach Using C16 FIGURE 2-7 Data Types

17 Computer Science: A Structured Programming Approach Using C17 FIGURE 2-8 Character Types

18 Computer Science: A Structured Programming Approach Using C18 FIGURE 2-9 Integer Types

19 Computer Science: A Structured Programming Approach Using C19 sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long) Note

20 Computer Science: A Structured Programming Approach Using C20 Table 2-3Typical Integer Sizes and Values for Signed Integers

21 Computer Science: A Structured Programming Approach Using C21 FIGURE 2-10 Floating-point Types

22 Computer Science: A Structured Programming Approach Using C22 sizeof (float) ≤ sizeof (double) ≤ sizeof (long double) Note

23 Computer Science: A Structured Programming Approach Using C23 Table 2-4Type Summary

24 Computer Science: A Structured Programming Approach Using C24 2-5 Variables Variables are named memory locations that have a type, such as integer or character, which is inherited from their type. The type determines the values that a variable may contain and the operations that may be used with its values. Variable Declaration Variable Initialization Topics discussed in this section:

25 Computer Science: A Structured Programming Approach Using C25 FIGURE 2-11 Variables

26 Computer Science: A Structured Programming Approach Using C26 Table 2-5Examples of Variable Declarations and Definitions

27 Computer Science: A Structured Programming Approach Using C27 FIGURE 2-12 Variable Initialization

28 Computer Science: A Structured Programming Approach Using C28 When a variable is defined, it is not initialized. We must initialize any variable requiring prescribed data when the function starts. Note

29 Computer Science: A Structured Programming Approach Using C29 PROGRAM 2-2Print Sum of Three Numbers

30 Computer Science: A Structured Programming Approach Using C30 PROGRAM 2-2Print Sum of Three Numbers (continued)

31 Computer Science: A Structured Programming Approach Using C31 PROGRAM 2-2Print Sum of Three Numbers (continued)

32 Computer Science: A Structured Programming Approach Using C32 2-6 Constants Constants are data values that cannot be changed during the execution of a program. Like variables, constants have a type. In this section, we discuss Boolean, character, integer, real, complex, and string constants. Constant Representation Coding Constants Topics discussed in this section:

33 Computer Science: A Structured Programming Approach Using C33 A character constant is enclosed in single quotes. Note

34 Computer Science: A Structured Programming Approach Using C34 Table 2-6Symbolic Names for Control Characters

35 Computer Science: A Structured Programming Approach Using C35 Table 2-7Examples of Integer Constants

36 Computer Science: A Structured Programming Approach Using C36 Table 2-8Examples of Real Constants

37 Computer Science: A Structured Programming Approach Using C37 FIGURE 2-13 Some Strings

38 Computer Science: A Structured Programming Approach Using C38 FIGURE 2-14 Null Characters and Null Strings

39 Computer Science: A Structured Programming Approach Using C39 Use single quotes for character constants. Use double quotes for string constants. Note

40 Computer Science: A Structured Programming Approach Using C40 PROGRAM 2-3Memory Constants

41 Computer Science: A Structured Programming Approach Using C41 PROGRAM 2-3Memory Constants (continued)


Download ppt "Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C."

Similar presentations


Ads by Google