Vous êtes sur la page 1sur 33

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. 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.
Computer Science: A Structured Programming Approach Using C 1

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. While many languages claim to be machine independent, C is one of the closest to achieving that goal. That is another reason why it is used by software developers whose applications have to run on many different hardware platforms.
Computer Science: A Structured Programming Approach Using C 2

FIGURE 2-1 Taxonomy of the C Language


Computer Science: A Structured Programming Approach Using C 3

2-2 C Programs
It's time to write your first C program! This section will take you through all the basic parts of a C program so that you will be able to write it. Topics discussed in this section:
Elements of a C Program Structure of a C Program Your First C Program Comments The Greeting Program
Computer Science: A Structured Programming Approach Using C 4

Elements of Programming Languages


Algorithm
Statements

Data

expressions

Data Types

assignment (=) input/output (scanf/printf) Selection making decisions if, switch

arithmetic operators comparison operators logical operators

Derived Data Types


arrays structures pointers union enumerated

integer int, short, long real float, double boolean character char

Repetition Module

while, for, do-while functions

Implemented type
Data expression
constants variables string char*

Computer Science: A Structured Programming Approach Using C

FIGURE 2-2 Structure of a C Program


Computer Science: A Structured Programming Approach Using C 6

FIGURE 2-3 The Greeting Program


Computer Science: A Structured Programming Approach Using C 7

FIGURE 2-4 Examples of Block Comments


Computer Science: A Structured Programming Approach Using C 8

/* Nested Block Comments are Invalid


Inner Comment not Allowed Closing Token

*/

/*

=======

/*

----------

*/

========

*/

Ignored

Left On Its Own

FIGURE 2-5 Examples of Line Comments


Computer Science: A Structured Programming Approach Using C 9

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. If we didnt have identifiers that we could use to symbolically represent data locations, we would have to know and use objects addresses. Instead, we simply give data identifiers and let the compiler keep track of where they are physically located.
Computer Science: A Structured Programming Approach Using C 10

Rules of Identifiers:
First character must be alphabetic character or underscore. Must consist only of alphabetic characters, digits, or underscore. First 63 characters of an identifier are significant Cannot duplicate a keyword.
(See Appendix B for the list of keyword)

It is case-sensitive.

Computer Science: A Structured Programming Approach Using C

11

Table 2-2 Examples of Valid and Invalid Names

Computer Science: A Structured Programming Approach Using C

12

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

Topics discussed in this section:


Void Type Integral Type Floating-Point Types
Computer Science: A Structured Programming Approach Using C 13

boolean, imaginary, complex are supported in C99 void type has no values and no operations, but is a very useful data type. (function with no return value, generic pointer type, etc. )

FIGURE 2-7 Data Types


Computer Science: A Structured Programming Approach Using C 14

Character Type
Type name : char 8bit / 1byte to store the char data types. Most computers use the American Standard Code for Information Interchange (ASCII) alphabet.
Refer to Appendix A.2 ASCII. Can be considered and used like 1byte integer data.

Expression of character a, b, 1, $ Expression of control characters

Computer Programming 15

Note
sizeof (short) sizeof (int) sizeof (long) sizeof (long long)
FIGURE 2-9 Integer Types
Computer Science: A Structured Programming Approach Using C 16

Table 2-3 Typical Integer Sizes and Values for Signed / Default Integers
Each integer size can be a signed or an unsigned integer.
Unsigned integer unsigned short (0 ~ 65535) unsigned int (0 ~ 232 - 1) Signed integer: Refer Appendix D (Numbering Systems) 1s and 2s complements 1s complement : -5 ~(0101) 1010 The leftmost bit contains the sign: 0 for positive, 1 for negative There are two zeros: +0 (0000) and -0 (1111) To flip the sign of a number, flip each individual bit.
Computer Science: A Structured Programming Approach Using C 17

2s complement
2s complement bit Corresponding representation (ex:4bit) decimal values 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1
18
4. 3. 2.

2s complement property
1.

Left most bit contains the sign of the number: 0 for positive, 1 for negative There is only one zero in this method. + positive : same as bit representation of decimal value -negative : (complement of positive) +1

Computer Science: A Structured Programming Approach Using C

Note
sizeof (float) sizeof (double) sizeof (long double)
FIGURE 2-10 Floating-point Types : Real Type
Computer Science: A Structured Programming Approach Using C 19

Floating Point Types


floating-point types: float, double, long double float (4byte) -1038 ~ 1038 Precision down to 6 digits below the decimal point double (8byte) - default -10308 ~ 10308 Precision down to 15 digits below the decimal point floating-point Real type normal representation : consist of a integral and fractional part 0.5, +3.14, -1.4, -2.0f, 3.1415926536L exponential representation 0.314E1, 1.5E-200 bit representation of floating-point numbers IEEE 754 - Refer Appendix D(Numbering Systems)

Computer Programming 20

Table 2-4 Type Summary

Computer Science: A Structured Programming Approach Using C

21

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.

Topics discussed in this section:


Variable Declaration Variable Initialization
Computer Science: A Structured Programming Approach Using C 22

FIGURE 2-11 Variables


Computer Science: A Structured Programming Approach Using C 23

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

FIGURE 2-12 Variable Initialization


Computer Science: A Structured Programming Approach Using C 24

PROGRAM 2-2

Print Sum of Three Numbers

Computer Science: A Structured Programming Approach Using C

25

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.

Topics discussed in this section:


Constant Representation Coding Constants
Computer Science: A Structured Programming Approach Using C 26

Constants
Character Constants: Most computer uses ASCII character set (Appendix A). It is enclosed in single quotes. Integer Constants: default is int type There is no short int constant. Need to use suffix L, LU, LL, LLU for large integer constants. Real Constants:
Default is double Need to use suffix F (float), L (long double) for large real constants.

String Constants:
It is enclosed in double quotes.
Computer Science: A Structured Programming Approach Using C 27

Table 2-6

Symbolic Names for Control Characters


28

Computer Science: A Structured Programming Approach Using C

Table 2-7

Examples of Integer Constants

Computer Science: A Structured Programming Approach Using C

29

Table 2-8

Examples of Real Constants

Computer Science: A Structured Programming Approach Using C

30

FIGURE 2-13 Some String Constants

FIGURE 2-14 Null Character and Null String

Computer Science: A Structured Programming Approach Using C

31

Coding Constants
Literal constants : data value itself (ex: literal 5) A = b + 5;

Defined constants : preprocessor command define #define SALES_TAX_RATE .0825

Memory constants : C type qualifier const const type identifier = value; Ex: const float cPi = 3.14159;

Computer Science: A Structured Programming Approach Using C

32

PROGRAM 2-3

Memory Constants

cPi

Computer Science: A Structured Programming Approach Using C

33

Vous aimerez peut-être aussi