Académique Documents
Professionnel Documents
Culture Documents
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
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
Data
expressions
Data Types
integer int, short, long real float, double boolean character char
Repetition Module
Implemented type
Data expression
constants variables string char*
*/
/*
=======
/*
----------
*/
========
*/
Ignored
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.
11
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.
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. )
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.
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
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
Computer Programming 20
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.
Note
When a variable is defined, it is not initialized. We must initialize any variable requiring prescribed data when the function starts.
PROGRAM 2-2
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.
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
Table 2-7
29
Table 2-8
30
31
Coding Constants
Literal constants : data value itself (ex: literal 5) A = b + 5;
Memory constants : C type qualifier const const type identifier = value; Ex: const float cPi = 3.14159;
32
PROGRAM 2-3
Memory Constants
cPi
33