Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 C Programming Lecture 3

2 The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked The modified source code is translated into object code (an object file) b The loader is invoked The loader uses the object code to produce an executable file

3 Preprocessing Directives b Source code lines that begin with a # are called preprocessing directives. Examples #include #define PI 3.14 b Preprocessing directives indicate to the preprocessor that certain header files should be included with the source code and certain substitutions should be made.

4 The Standard Library b 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. b The header files provide needed information about the library functions.

5 Example of a Library Function 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.

6 Variables b 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. b 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.

7 Why “Variables”? b 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. b We are now going to look at how values (numbers, characters, strings of characters, etc.) are stored in memory.

8 Number Systems b We are accustomed to using the decimal number system. Computers use the binary number system. b Each digit in a decimal number has a value that equals the value of that digit times some power of ten.

9 Example of a Decimal Number b The digits in the decimal number 234 actually have values (starting from the right or low-order position): 4 x 10 0 = 4 x 1 = 4 3 x 10 1 = 3 x 10 = 30 2 x 10 2 = 2 x 100 = 200 Total = 234

10 The Binary Number System b 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.

11 Example of a Binary Number b The digits in the binary number 1011 actually have values (starting from the right or low-order position): 1 x 2 0 = 1 x 1 = 1 (numbers 1 x 2 1 = 1 x 2 = 2 on the 0 x 2 2 = 0 x 4 = 0 right are 1 x 2 3 = 1 x 8 = 8 decimal Total = 11 numbers)

12 Binary Numbers in Computers b You can think of each location in RAM as having eight tiny switches that can be on (a one) or off (a zero). b We say that each of the “switches” represents a bit (binary digit). And all eight bits make up a byte.

13 Storing a Character in a Byte b To store a character such as ‘A’ or ‘B’, etc. in a byte of memory, we store a number that represents the character. b We encode characters using an established code, the American Standard Code for Information Interchange (ASCII)

14 The ASCII Code b 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.

15 The ASCII Codes for ‘A’ and ‘a’ b On the chart we find that the ASCII code for ‘A’ is 65 and the code for ‘a’ is 97. b 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.

16 The Non-Printable or Control Codes b 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)

17 Storing Numbers in Memory b 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). b A number is stored as its binary equivalent in one or more bytes.

18 Representing Numbers in Memory Gets a Bit Complicated b 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. b We will wait until later in the course to discuss specifics.

19 Wreck of the Hesperus b 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.

20 An Algorithm for Converting Fathoms to Feet 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.

21 Convert the Algorithm to C Code #include 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; }

22 Some Things to Note about the Hesperus Program b 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.

23 Hesperus Program Notes (2) b 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;

24 Hesperus Program Notes (3) b 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.

25 Hesperus Program Notes (5) b In the statement printf(“ %d feet\n”, feet); the word feet inside quotes is simply printed, it is not the variable feet. b %d is a conversion specification that says display feet as an integer (no decimal point). b \n, the linefeed character, says to start the next output on the next line.

26 Math Operators Used in C b You have seen the multiplication operator *. b Other mathematical operators are: + addition - subtraction / division % modulus

27 What is the Modulus Operator b a % b yields the remainder after a is divided by b. b Example 13 % 5 yields 3 b Note that ‘%’ is also used in a printf() statement to start a conversion specification.

28 Use of printf ( ) b printf() is used for printing output. When printf() is called it is passed a list of arguments of the form: control string & other arguments b The arguments to printf() are separated by commas.

29 printf ( ) Example printf(“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.

30 The Formats in the Control String printf(“Get set: %d %s %f %c%c\n”, 1, “two”, 3.33, ‘G’, ‘O’); b %d Print 1 as a decimal number b %s Print “two” as a string –“string” means a sequence of characters. b %f Print 3.33 as a float –decimal or floating-point number b %c Print ‘G’ & ‘0’ as characters.

31 Use of scanf() b 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.

32 scanf ( ) Arguments b 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.

33 Scanf ( )’s Other Arguments b After the control string, the other arguments are addresses. b 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”.

34 scanf ( ) Conversion Conversion How characters in the Character 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

35 A Peculiarity of scanf ( ) b With printf() the %f format is used to print either a float or a double. b With scanf() the format %f is used to read in a float, and %lf is used to read in a double.

36 Another scanf() Peculiarity b When reading in numbers, scanf() will skip white space characters (blanks, newlines, and tabs). b When reading characters, white space is not skipped.


Download ppt "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."

Similar presentations


Ads by Google