Title: Chapter 2. Introduction to C
1Chapter 2. Introduction to C
22.1 The Parts of a C Program
- C programs have parts and components that serve
specific purposes.
3Program 2-1
- //A simple C program
- include ltiostream.hgt
- void main (void)
-
- coutltlt Programming is great fun!
-
- Program Output
- Programming is great fun!
4Table 2-1
52.2 The cout Object
- Use the cout object to display information on the
computers screen. - The cout object is referred to as the standard
output object. - Its job is to output information using the
standard output device
6Program 2-2
- // A simple C program
- include ltiostream.hgt
- void main (void)
-
- coutltlt Programming is ltlt great fun!
-
- Output
- Programming is great fun!
7Program 2-3
- // A simple C program
- include ltiostream.hgt
- void main (void)
-
- coutltlt Programming is
- cout ltlt great fun!
-
- Output
- Programming is great fun!
8Program 2-4
- // An unruly printing program
- include ltiostream.hgt
- void main(void)
-
- cout ltlt "The following items were top sellers"
- cout ltlt "during the month of June"
- cout ltlt "Computer games"
- cout ltlt "Coffee"
- cout ltlt "Aspirin"
-
- Program Output
- The following items were top sellersduring the
month ofJuneComputer gamesCoffeeAspirin
9New lines
- cout does not produce a newline at the end of a
statement - To produce a newline, use either the stream
manipulator endl - or the escape sequence \n
10Program 2-5
- // A well-adjusted printing program
- include ltiostream.hgt
- void main(void)
-
- cout ltlt "The following items were top sellers"
ltlt endl - cout ltlt "during the month of June" ltlt endl
- cout ltlt "Computer games" ltlt endl
- cout ltlt "Coffee" ltlt endl
- cout ltlt "Aspirin" ltlt endl
11Program Output
- The following items were top sellers
- during the month of June
- Computer games
- Coffee
- Aspirin
12Program 2-6
- // Another well-adjusted printing program
- include ltiostream.hgt
- void main(void)
-
- cout ltlt "The following items were top sellers"
ltlt endl - cout ltlt "during the month of June" ltlt endl
- cout ltlt "Computer games" ltlt endl ltlt "Coffee"
- cout ltlt endl ltlt "Aspirin" ltlt endl
13Program Output
- The following items were top sellers
- during the month of June
- Computer games
- Coffee
- Aspirin
14Program 2-7
- // Yet another well-adjusted printing program
- include ltiostream.hgt
- using namespace std
- void main(void)
-
- cout ltlt "The following items were top
sellers\n" - cout ltlt "during the month of June\n"
- cout ltlt "Computer games\nCoffee"
- cout ltlt "\nAspirin\n"
15Program Output
- The following items were top sellers
- during the month of June
- Computer games
- Coffee
- Aspirin
16Table 2-2
172.3 The include Directive
- The include directive causes the contents of
another file to be inserted into the program - Preprocessor directives are not C statements
and do not require semicolons at the end
182.5 Variables and Constants
- Variables represent storage locations in the
computers memory. Constants are data items
whose values do not change while the program is
running. - Every variable must have a declaration.
19Program 2-8
- include ltiostream.hgt
- void main(void)
-
- int value
- value 5
- cout ltlt The value is ltlt value ltlt endl
-
- Program Output
- The value is 5
20Assignment statements
- Value 5 //This line is an assignment
statement. - The assignment statement evaluates the expression
on the right of the equal sign then stores it
into the variable named on the left of the equal
sign - The data type of the variable was in integer, so
the data type of the expression on the right
should evaluate to an integer as well.
21Constants
- A variable is called a variable because its
value may be changed. A constant, on the other
hand, is a data item whose value does not change
during the programs execution.
22Program 2-10
- include ltiostream.hgt
- void main (void)
-
- int apples
- apples 20
- coutltlt Today we sold ltlt apples ltlt
bushels\n - cout ltlt of apples.\n
23Program Output
- Today we sold 20 bushels
- of apples.
- Where are the constants in program 2-10?
24Constants from Program 2-10
252.6 Focus on Software Engineering Identifiers
- Must not be a key word
- Should be mnemonic (give an indication of what
the variable is used for) - The first character must be a letter or an
underscore - The remaining may be letters, digits, or
underscores - Upper and lower case letters are distinct
26Table 2-3 The C Key Words
27Table 2-4 Some Variable Names
282.7 Integer Data Types
- There are many different types of data.
Variables are classified according to their data
type, which determines the kind of information
that may be stored in them. Integer variables
only hold whole numbers.
29Table 2-5
30Program 2-11
- // This program has variables of several of the
integer types. - include ltiostream.hgt
- void main(void)
-
- int checking
- unsigned int miles
- long days
- checking -20
- miles 4276
- days 184086
- cout ltlt "We have made a long journey of " ltlt
miles - cout ltlt " miles.\n"
- cout ltlt "Our checking account balance is " ltlt
checking - cout ltlt "\nExactly " ltlt days ltlt " days ago
Columbus " - cout ltlt "stood on this spot.\n"
31Program Output
- We have made a long journey of 4276 miles.
- Our checking account balance is -20
- Exactly 184086 days ago Columbus stood on this
spot.
32Program 2-12
- // This program shows three variables declared on
the same - // line.
- include ltiostream.hgt
-
- void main(void)
-
- int floors,rooms,suites
-
- floors 15
- rooms 300
- suites 30
- cout ltlt "The Grande Hotel has " ltlt floors ltlt "
floors\n" - cout ltlt "with " ltlt rooms ltlt " rooms and " ltlt
suites - cout ltlt " suites.\n"
33Program Output
- The Grande Hotel has 15 floors
- with 300 rooms and 30 suites.
34Hexadecimal and Octal Constants
- Hexadecimal numbers are preceded by 0x
- Hexadecimal F4 would be expressed in C as 0xF4
- Octal numbers are preceded by a 0
- Octal 31 would be written as 031
352.8 The char Data Type
- Usually 1 byte long
- Internally stored as an integer
- ASCII character set shows integer representation
for each character - A 65, B 66, C 67, etc.
- Single quotes denote a character, double quotes
denote a string
36Program 2-13
- // This program demonstrates the close
relationship between - // characters and integers.
- include ltiostream.hgt
- void main(void)
-
- char letter
-
- letter 65
- cout ltlt letter ltlt endl
- letter 66
- cout ltlt letter ltlt endl
37Program Output
38Program 2-14
- // This program uses character constants
- include ltiostream.hgt
- void main(void)
-
- char letter
-
- letter 'A'
- cout ltlt letter ltlt endl
- letter 'B'
- cout ltlt letter ltlt endl
39Program Output
40C-Strings
- C-Strings are consecutive sequences of characters
and can occupy several bytes of memory. - C-Strings always have a null terminator at the
end. This marks the end of the string. - Escape sequences are always stored internally as
a single character.
41Program 2-15
- // This program uses character constants
- include ltiostream.hgt
- void main(void)
-
- char letter
-
- letter 'A'
- cout ltlt letter ltlt '\n'
- letter 'B'
- cout ltlt letter ltlt '\n'
42Program Output
43Review key points regarding characters, strings,
and C-strings
- Printable characters are internally represented
by numeric codes. Most computers use ASCII codes
for this purpose. - Characters occupy a single byte of memory.
- C-Strings are consecutive sequences of characters
and can occupy several bytes of memory. - C-Strings always have a null terminator at the
end. This marks the end of the C-string. - Character constants are always enclosed in single
quotation marks. - String constants are always enclosed in double
quotation marks. - Escape sequences are always stored internally as
a single character.
442.9 The C string Class
- The string class is an abstract data type.
- It provides capabilities that make working with
strings easy and intuitive. - You must us the include ltstringgt statement.
452.10 Floating Point Data Types
- Floating point data types are used to declare
variables that can hold real numbers
46Table 2-7
47Program 2-17
- // This program uses floating point data types
- include ltiostream.hgt
- void main(void)
-
- float distance
- double mass
- distance 1.495979E11
- mass 1.989E30
- cout ltlt "The Sun is " ltlt distance ltlt "
kilometers away.\n" - cout ltlt "The Sun\'s mass is " ltlt mass ltlt "
kilograms.\n"
48Program Output
- The Sun is 1.4959e11 kilometers away.
- The Sun's mass is 1.989e30 kilograms.
49Floating Point Constants
- May be expressed in a variety of ways
- E notation
- decimal notation
- (no commas)
502.11 The bool Data Type
- Boolean variables are set to either true or false
51Program 2-18
- include ltiostream.hgt
- void main (void)
-
- bool boolValue
- boolValue true
- cout ltlt boolValue ltlt endl
- boolValue false
- cout ltlt boolValue ltlt endl
52Program Output
- 1
- 0
- Internally, true is represented as the number 1
and false is represented by the number 0.
532.12 Focus on Software Engineering Determining
the Size of a Data Type
- The sizeof operator may be used to determine the
size of a data type on any system. - cout ltlt The size of an integer is ltlt
sizeof(int)
54Program 2-19
- include ltiostream.hgt
- void main (void)
-
- long double apple
- cout ltlt The size of an integer is ltlt
sizeof(int) - cout ltlt bytes.\n
- cout ltlt The size of a long integer is ltlt
sizeof(long) - cout ltlt bytes.\n
- cout ltlt An apple can be eaten in ltlt
sizeof(apple) - cout ltlt bytes!\n
55Program Output
- The size of an integer is 4 bytes.
- The size of a long integer is 4 bytes.
- An apple can be eaten in 8 bytes!
562.13 Focus on Software Engineering Variable
Assignment and Initialization
- An assignment operation assigns, or copies, a
value into a variable. When a value is assigned
to a variable as part of the variables
declaration, it is called an initialization.
57Program 2-20
- include ltiostream.hgt
- void main (void)
-
- int month 2, days 28
- cout ltlt Month ltlt month ltlt has ltlt days ltlt
days.\n -
- Program output
- Month 2 has 28 days.
582.14 Focus on Software Engineering Scope
- A variables scope is the part of the program
that has access to the variable. - A variable must be declared before it is used.
59Program 2-21
- // This program can't find its variable
- include ltiostream.hgt
- void main(void)
-
- cout ltlt Value
-
- int Value 100
-
602.15 Arithmetic Operators
- There are many operators for manipulating numeric
values and performing arithmetic operations. - Generally, there are 3 types of operators unary,
binary, and ternary. - Unary operators operate on one operand
- Binary operators require two operands
- Ternary operators need three operands
61Table 2-8
62Program 2-22
- // This program calculates hourly wages
- // The variables in function main are used as
follows - // regWages holds the calculated regular wages.
- // basePay holds the base pay rate.
- // regHours holds the number of hours worked
less overtime. - // otWages holds the calculated overtime wages.
- // otPay holds the payrate for overtime hours.
- // otHours holds the number of overtime hours
worked. - // totalWages holds the total wages.
-
- include ltiostream.hgt
- void main(void)
-
- float regWages, basePay 18.25, regHours
40.0 - float otWages, otPay 27.78, otHours 10
- float totalWages
-
- regWages basePay regHours
632.16 Comments
- Comments are notes of explanation that document
lines or sections of a program. - Comments are part of a program, but the compiler
ignores them. They are intended for people who
may be reading the source code. - Commenting the C Way
- //
- Commenting the C Way
- / /
64Program 2-23
- // PROGRAM PAYROLL.CPP
- // Written by Herbert Dorfmann
- // This program calculates company payroll
- // Last modification 3/30/96
-
- include ltiostream.hgt
-
- void main(void)
-
- float payRate // holds the hourly pay rate
- float hours // holds the hours worked
- int empNum // holds the employee number
-
- (The remainder of this program is left out.)
65Program 2-24
- // PROGRAM PAYROLL.CPP
- // Written by Herbert Dorfmann
- // Also known as "The Dorfmiester"
- // Last modification 3/30/96
- // This program calculates company payroll.
- // Payroll should be done every Friday no later
than - // 1200 pm. To start the program type PAYROLL
and - // press the enter key.
-
- include ltiostream.hgt // Need the iostream file
because - // the program uses cout.
- void main(void) // This is the start of
function main. - // This is the opening brace for main.
- float payRate // payRate is a float variable.
- // It holds the hourly pay rate.
- float hours // hours is a float variable too.
- // It holds the hours worked.
- int empNum // empNum is an integer.
- // It holds the employee number.
66Program 2-25
- /
- PROGRAM PAYROLL.CPP
- Written by Herbert Dorfmann
- This program calculates company payroll
- Last modification 3/30/96
- /
- include ltiostream.hgt
- void main(void)
-
- float payRate / payRate holds hourly pay rate
/ - float hours / hours holds hours worked
/ - int empNum / empNum holds employee number
/ -
- (The remainder of this program is left out.)
67Program 2-26
- /
- PROGRAM PAYROLL.CPP
- Written by Herbert Dorfmann
- This program calculates company payroll
- Last modification 3/30/96
- /
- include ltiostream.hgt
- void main(void)
-
- float payRate // payRate holds the hourly pay
rate - float hours // hours holds the hours worked
- int empNum // empNum holds the employee number
-
- (The remainder of this program is left out.)
682.17 Focus on Software Engineering Programming
Style
- Program style refers to the way a programmer uses
identifiers, spaces, tabs, blank lines, and
punctuation characters to visually arrange a
programs source code. - Generally, C ignores white space.
- Indent inside a set of braces.
- Include a blank line after variable declarations.
69Program 2-27
- include ltiostream.hgt
- void main(void)float shares220.0float
avgPrice14.67cout ltlt"There were "ltltsharesltlt"
shares sold at "ltltavgPriceltlt" per share.\n" - Program Output
- There were 220 shares sold at 14.67 per share.
70Program 2-28
- // This example is much more readable than
Program 2-26. - include ltiostream.hgt
- void main(void)
-
- float shares 220.0
- float avgPrice 14.67
-
- cout ltlt "There were " ltlt shares ltlt " shares sold
at " - cout ltlt avgPrice ltlt " per share.\n"
-
- Program Output
- There were 220.0 shares sold at 14.67 per share.