c programming lecture 3. the three stages of compiling a program b the preprocessor is invoked the...

36
C Programming C Programming Lecture 3

Upload: kevin-owen

Post on 27-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

C ProgrammingC Programming

Lecture 3

Page 2: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

The Three Stages of The Three Stages of Compiling a ProgramCompiling a Program

The preprocessor is invoked• The source code is modified

The compiler itself is invoked• The modified source code is translated into object code (an object file)

The loader is invoked• The loader uses the object code to produce an executable file

Page 3: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Preprocessing Preprocessing DirectivesDirectives

Source code lines that begin with a # are called preprocessing directives.• Examples

#include <stdio.h>#define PI 3.14

Preprocessing directives indicate to the preprocessor that certain header files should be included with the source code and certain substitutions should be made.

Page 4: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

The Standard LibraryThe Standard Library

Some functions have been written for us as part of the C system.• These functions are compiled into object code and stored in libraries.

• The linker will link object code from the libraries with object code from the functions we write to produce the executable code.

The header files provide needed information about the library functions.

Page 5: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Example of a Library Example of a Library FunctionFunction

printf(“Hello, world!\n”);printf() is a library function. When we provide printf() with an argument such as the string “Hello, World!” the printf() function causes the argument to be displayed on the screen.• The header file stdio.h provides information needed by the printf() library function.

Page 6: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

VariablesVariables While a program is executing, it is usually necessary to store values that the program is using in RAM memory.• Values such as numbers or strings of characters that are being manipulated by our program.

In our program, we obtain the needed memory locations by declaring names for the locations. We refer to these locations and their names as variables.

Page 7: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Why “Variables”?Why “Variables”? We call these memory locations and the names we give them “variables” because we can (in our programs) cause the values there to be changed as the program executes.

We are now going to look at how values (numbers, characters, strings of characters, etc.) are stored in memory.

Page 8: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Number SystemsNumber Systems We are accustomed to using the decimal number system.• Computers use the binary number system.

Each digit in a decimal number has a value that equals the value of that digit times some power of ten.

Page 9: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Example of a Decimal Example of a Decimal NumberNumber

The digits in the decimal number 234 actually have values (starting from the right or low-order position):• 4 x 100 = 4 x 1 = 4• 3 x 101 = 3 x 10 = 30• 2 x 102 = 2 x 100 = 200 Total = 234

Page 10: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

The Binary Number The Binary Number SystemSystem

The only digits used in binary numbers are zero and one.•Each digit in a binary number has a value that equals the value of that digit times some power of two.

Page 11: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Example of a Binary Example of a Binary NumberNumber

The digits in the binary number 1011 actually have values (starting from the right or low-order position):• 1 x 20 = 1 x 1 = 1 (numbers• 1 x 21 = 1 x 2 = 2 on the• 0 x 22 = 0 x 4 = 0 right are• 1 x 23 = 1 x 8 = 8 decimal

Total = 11 numbers)

Page 12: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Binary Numbers in Binary Numbers in ComputersComputers

You can think of each location in RAM as having eight tiny switches that can be on (a one) or off (a zero).

We say that each of the “switches” represents a bit (binary digit). And all eight bits make up a byte.

Page 13: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Storing a Character in a Storing a Character in a ByteByte

To store a character such as ‘A’ or ‘B’, etc. in a byte of memory, we store a number that represents the character.

We encode characters using an established code, the American Standard Code for Information Interchange (ASCII)

Page 14: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

The ASCII CodeThe ASCII Code

Look on pages 782-785 of your text.• You will find the ASCII codes for characters on your keyboard and for what we call control characters.

• Note that the codes are given as decimal numbers, as hexadecimal numbers and as octal numbers in this chart.

Page 15: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

The ASCII Codes for ‘A’ The ASCII Codes for ‘A’ and ‘a’and ‘a’

On the chart we find that the ASCII code for ‘A’ is 65 and the code for ‘a’ is 97.

Expressed as binary numbers, 65 is 01000001 and 97 is 01100001.• Note that the ASCII code is a 7-bit code, but each byte is 8-bits, thus I have included the leading 0.

Page 16: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

The Non-Printable or Control The Non-Printable or Control CodesCodes

The first 32 ASCII codes represent non-printable characters known as control characters.• “Control Characters” because they can be used to do things like make the bell (beeper) on your computer sound (bel, 7 decimal or 00000111 binary) or make printed output go to the next line (nl, 10 decimal or 00001010 binary)

Page 17: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Storing Numbers in Storing Numbers in MemoryMemory

The ASCII code is used for storing characters (including the digits when they are stored as digits 0,1,2,3,4,5,6,7,8 9).

A number is stored as its binary equivalent in one or more bytes.

Page 18: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Representing Numbers in Representing Numbers in Memory Gets a Bit Memory Gets a Bit

ComplicatedComplicated

Exactly how we store the binary number in memory depends upon:• How big we allow the number to be.

– Maximum number of digits allowed (both before and after a decimal point).

• What kind of number.– Integer versus floating point.– Positive versus negative.

• Choice of representation– One’s complement, two’s complement, etc.

We will wait until later in the course to discuss specifics.

Page 19: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Wreck of the HesperusWreck of the Hesperus

How deep in the ocean is the wreck of the Hesperus?• We are told that it is 7 fathoms deep, but we want to know how deep that would be in feet or how deep in inches.

Page 20: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

An Algorithm for An Algorithm for Converting Fathoms to Converting Fathoms to Feet and to InchesFeet and to Inches

Assign the number of fathoms to a variable.

Convert fathoms to feet and store in a variable.

Convert feet to inches and store in a variable.

Print the different units of measure neatly on the screen.

Page 21: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Convert the Algorithm to C Code#include <stdio.h>

int main(void){ /* declaration of integer variables */ int inches, feet, fathoms; /* initialization using the ‘=‘ assignment operator*/ fathoms = 7; /* we were given this */ /* calculations using expressions and assignments */ feet = 6 * fathoms; inches = 12 * feet; /* Display the output on the computer’s screen. */ printf(“Wreck of the Hesperus:\n”); printf(“Its depth at sea in different units:\n”); printf(“ %d fathoms\n”, fathoms); printf(“ %d feet\n”, feet); printf(“ %d inches\n”, inches); return 0;}

Page 22: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Some Things to Note Some Things to Note aboutabout

the Hesperus Programthe Hesperus Program

When variables are declared you must indicate the variable type (the kind of values that will be stored there -- integers in this case).• You can declare several variables of the same type by separating the variable names by commas.

Page 23: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Hesperus Program Notes Hesperus Program Notes (2)(2)

The equals sign ‘=‘ is used as the assignment operator in C.• The value calculated in the expression on the right of ‘=‘ is placed in the memory location named on the left side of ‘=‘

• Example: feet = 6 * fathoms;

Page 24: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Hesperus Program Notes Hesperus Program Notes (3)(3)

In the statement

printf(“ %d feet\n”, feet);

the spaces before %d are printed on output because they are within the quotes. %d tells the printf() function to display the value stored in the variable feet as an integer.

Page 25: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Hesperus Program Notes Hesperus Program Notes (5)(5)

In the statement printf(“ %d feet\n”, feet);

the word feet inside quotes is simply printed, it is not the variable feet.

%d is a conversion specification that says display feet as an integer (no decimal point).

\n, the linefeed character, says to start the next output on the next line.

Page 26: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Math Operators Used in CMath Operators Used in C

You have seen the multiplication operator *.

Other mathematical operators are:• + addition• - subtraction• / division• % modulus

Page 27: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

What is the Modulus What is the Modulus OperatorOperator

a % b yields the remainder after a is divided by b.

Example• 13 % 5 yields 3

Note that ‘%’ is also used in a printf() statement to start a conversion specification.

Page 28: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Use of printf ( )Use of printf ( )

printf() is used for printing output. When printf() is called it is passed a list of arguments of the form:

control string & other arguments The arguments to printf() are separated by commas.

Page 29: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

printf ( ) Exampleprintf ( ) Exampleprintf(“Get set: %d %s %f %c%c\n”, 1, “two”, 3.33, ‘G’, ‘O’);

The first argument is the control string “Get set: %d %s %f %c%c\n”

The formats in the control string are matched (in order of occurrence) with the other arguments.

Page 30: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

The Formats in the Control The Formats in the Control StringString

printf(“Get set: %d %s %f %c%c\n”,

1, “two”, 3.33, ‘G’, ‘O’); %d Print 1 as a decimal number %s Print “two” as a string

–“string” means a sequence of characters.

%f Print 3.33 as a float–decimal or floating-point number

%c Print ‘G’ & ‘0’ as characters.

Page 31: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Use of scanf()Use of scanf()

scanf() is analogous to printf(), but is used for input rather than output.•scanf()in a program stops the execution of the program while you type something in from the keyboard.

Page 32: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

scanf ( ) Argumentsscanf ( ) Arguments

The first argument is a control string with formats similar to those used with printf().• The formats determine how characters in the input stream (what you are typing) will be interpreted so they can be properly stored in memory.

Page 33: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Scanf Scanf ( )( )’s Other ’s Other ArgumentsArguments

After the control string, the other arguments are addresses.

Example: assume x is declared as an integer variable.scanf(“%d”, &x);The & is the address operator. It says “store the value entered at the address of the memory location named x”.

Page 34: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

scanf ( ) Conversionscanf ( ) ConversionConversion How characters in theCharacter input stream are converted.

c

d

f

lf

Lf

s

Character

decimal integer

floating-pint number (float)

floating-point number (double)

floating-point number (long double)

string

Page 35: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

A Peculiarity of scanf A Peculiarity of scanf ( )( )

With printf() the %f format is used to print either a float or a double.

With scanf() the format %f is used to read in a float, and %lf is used to read in a double.

Page 36: C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked

Another scanf() PeculiarityAnother scanf() Peculiarity

When reading in numbers, scanf() will skip white space characters (blanks, newlines, and tabs).

When reading characters, white space is not skipped.