Top Banner
Computer Science: A Structured Programming Approach Using C 1 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. Chapter 2 Chapter 2 Introduction to the C Language Introduction to the C Language
44

Objectives

Jan 21, 2016

Download

Documents

haruki

Chapter 2 Introduction to the C Language. 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. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Objectives

Computer Science: A Structured Programming Approach Using C 1

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. ❏ To understand the software engineering role.

Chapter 2Chapter 2Introduction to the C LanguageIntroduction to the C Language

Page 2: Objectives

Computer Science: A Structured Programming Approach Using C 2

2-1 Background

C is a structured programming language. It is C is a structured programming language. It is considered a high-level language because it allows the considered a high-level language because it allows the programmer to concentrate on the problem at hand programmer to concentrate on the problem at hand and not worry about the machine that the program and not worry about the machine that the program will be using. While many languages claim to be will be using. While many languages claim to be machine independent, C is one of the closest to machine independent, C is one of the closest to achieving that goal. That is another reason why it is achieving that goal. That is another reason why it is used by software developers whose applications have used by software developers whose applications have to run on many different hardware platforms.to run on many different hardware platforms.

Page 3: Objectives

Computer Science: A Structured Programming Approach Using C 3

FIGURE 2-1 Taxonomy of the C Language

Page 4: Objectives

Computer Science: A Structured Programming Approach Using C 4

2-2 C Programs

It's time to write your first C program! This section It's time to write your first C program! This section will take you through all the basic parts of a C will take you through all the basic parts of a C program so that you will be able to write it.program so that you will be able to write it.

Structure of a C ProgramYour First C ProgramCommentsThe Greeting Program

Topics discussed in this section:Topics discussed in this section:

Page 5: Objectives

Computer Science: A Structured Programming Approach Using C 5

FIGURE 2-2 Structure of a C Program

Page 6: Objectives

Computer Science: A Structured Programming Approach Using C 6

FIGURE 2-3 The Greeting Program

Page 7: Objectives

Computer Science: A Structured Programming Approach Using C 7

FIGURE 2-4 Examples of Block Comments

Page 8: Objectives

Computer Science: A Structured Programming Approach Using C 8

FIGURE 2-5 Examples of Line Comments

Page 9: Objectives

Computer Science: A Structured Programming Approach Using C 9

FIGURE 2-6 Nested Block Comments Are Invalid

Page 10: Objectives

Computer Science: A Structured Programming Approach Using C 10

PROGRAM 2-1 The Greeting Program

Page 11: Objectives

Computer Science: A Structured Programming Approach Using C 11

2-3 Identifiers

One feature present in all computer languages is the One feature present in all computer languages is the identifier. Identifiers allow us to name data and other identifier. Identifiers allow us to name data and other objects in the program. Each identified object in the objects in the program. Each identified object in the computer is stored at a unique address. If we didn’t computer is stored at a unique address. If we didn’t have identifiers that we could use to symbolically have identifiers that we could use to symbolically represent data locations, we would have to know and represent data locations, we would have to know and use object’s addresses. Instead, we simply give data use object’s addresses. Instead, we simply give data identifiers and let the compiler keep track of where identifiers and let the compiler keep track of where they are physically located.they are physically located.

Page 12: Objectives

Computer Science: A Structured Programming Approach Using C 12

Table 2-1 Rules for Identifiers

Page 13: Objectives

Computer Science: A Structured Programming Approach Using C 13

An identifier must start with a letter or underscore: it may not have a space or a hyphen.

NoteNote

Page 14: Objectives

Computer Science: A Structured Programming Approach Using C 14

C is a case-sensitive language.

NoteNote

Page 15: Objectives

Computer Science: A Structured Programming Approach Using C 15

Table 2-2 Examples of Valid and Invalid Names

Page 16: Objectives

Computer Science: A Structured Programming Approach Using C 16

2-4 Types

A type defines a set of values and a set of operations A type defines a set of values and a set of operations that can be applied on those values. For example, a that can be applied on those values. For example, a light switch can be compared to a computer type. It light switch can be compared to a computer type. It has a set of two values, on and off. Only two has a set of two values, on and off. Only two operations can be applied to a light switch: turn-on operations can be applied to a light switch: turn-on and turn-off.and turn-off.

Void TypeIntegral TypeFloating-Point Types

Topics discussed in this section:Topics discussed in this section:

Page 17: Objectives

Computer Science: A Structured Programming Approach Using C 17

FIGURE 2-7 Data Types

Page 18: Objectives

Computer Science: A Structured Programming Approach Using C 18

FIGURE 2-8 Character Types

Page 19: Objectives

Computer Science: A Structured Programming Approach Using C 19

FIGURE 2-9 Integer Types

Page 20: Objectives

Computer Science: A Structured Programming Approach Using C 20

sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long)

NoteNote

Page 21: Objectives

Computer Science: A Structured Programming Approach Using C 21

Table 2-3 Typical Integer Sizes and Values for Signed Integers

Page 22: Objectives

Computer Science: A Structured Programming Approach Using C 22

FIGURE 2-10 Floating-point Types

Page 23: Objectives

Computer Science: A Structured Programming Approach Using C 23

sizeof (float) ≤ sizeof (double) ≤ sizeof (long double)

NoteNote

Page 24: Objectives

Computer Science: A Structured Programming Approach Using C 24

Table 2-4 Type Summary

Page 25: Objectives

Computer Science: A Structured Programming Approach Using C 25

2-5 Variables

Variables are named memory locations that have a type, Variables are named memory locations that have a type, such as integer or character, which is inherited from such as integer or character, which is inherited from their type. The type determines the values that a variable their type. The type determines the values that a variable may contain and the operations that may be used with may contain and the operations that may be used with its values.its values.

Variable DeclarationVariable Initialization

Topics discussed in this section:Topics discussed in this section:

Page 26: Objectives

Computer Science: A Structured Programming Approach Using C 26

FIGURE 2-11 Variables

Page 27: Objectives

Computer Science: A Structured Programming Approach Using C 27

Table 2-5 Examples of Variable Declarations and Definitions

Page 28: Objectives

Computer Science: A Structured Programming Approach Using C 28

FIGURE 2-12 Variable Initialization

Page 29: Objectives

Computer Science: A Structured Programming Approach Using C 29

When a variable is defined, it is not initialized. We must initialize any variable requiring prescribed data when the function starts.

NoteNote

Page 30: Objectives

Computer Science: A Structured Programming Approach Using C 30

PROGRAM 2-2 Print Sum of Three Numbers

Page 31: Objectives

Computer Science: A Structured Programming Approach Using C 31

PROGRAM 2-2 Print Sum of Three Numbers (continued)

Page 32: Objectives

Computer Science: A Structured Programming Approach Using C 32

PROGRAM 2-2 Print Sum of Three Numbers (continued)

Page 33: Objectives

Computer Science: A Structured Programming Approach Using C 33

2-6 Constants

Constants are data values that cannot be changed Constants are data values that cannot be changed during the execution of a program. Like variables, during the execution of a program. Like variables, constants have a type. In this section, we discuss constants have a type. In this section, we discuss Boolean, character, integer, real, complex, and string Boolean, character, integer, real, complex, and string constants.constants.

Constant RepresentationCoding Constants

Topics discussed in this section:Topics discussed in this section:

Page 34: Objectives

Computer Science: A Structured Programming Approach Using C 34

A character constant is enclosed in single quotes.

NoteNote

Page 35: Objectives

Computer Science: A Structured Programming Approach Using C 35

Table 2-6 Symbolic Names for Control Characters

Page 36: Objectives

Computer Science: A Structured Programming Approach Using C 36

Table 2-7 Examples of Integer Constants

Page 37: Objectives

Computer Science: A Structured Programming Approach Using C 37

Table 2-8 Examples of Real Constants

Page 38: Objectives

Computer Science: A Structured Programming Approach Using C 38

The two components of a complex constant must be of the same precision, that is, if the real part is type double,

then the imaginary part must also be type double.

NoteNote

Page 39: Objectives

Computer Science: A Structured Programming Approach Using C 39

Table 2-9 Examples of Complex Constants

Page 40: Objectives

Computer Science: A Structured Programming Approach Using C 40

FIGURE 2-13 Some Strings

Page 41: Objectives

Computer Science: A Structured Programming Approach Using C 41

FIGURE 2-14 Null Characters and Null Strings

Page 42: Objectives

Computer Science: A Structured Programming Approach Using C 42

Use single quotes for character constants. Use double quotes for string constants.

NoteNote

Page 43: Objectives

Computer Science: A Structured Programming Approach Using C 43

PROGRAM 2-3 Memory Constants

Page 44: Objectives

Computer Science: A Structured Programming Approach Using C 44

PROGRAM 2-3 Memory Constants (continued)