360 likes | 387 Views
Explore the three stages of compiling a program, including preprocessing, library functions, storage of variables, and number systems in C programming language. Understand ASCII codes, binary numbers, and memory storage for characters and numbers.
E N D
C Programming Lecture 3
The Three Stages of Compiling 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
Preprocessing Directives • 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.
The 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.
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.
Variables • 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.
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.
Number 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.
Example of a Decimal Number • 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
The Binary Number System • 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.
Example of a Binary Number • 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)
Binary Numbers in Computers • 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.
Storing a Character in a Byte • 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)
The 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.
The ASCII Codes for ‘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.
The Non-Printable or Control Codes • 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)
Storing Numbers in Memory • 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.
Representing Numbers in Memory Gets a Bit Complicated • 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.
Wreck 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.
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.
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; }
Some Things to Note aboutthe 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.
Hesperus Program Notes (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;
Hesperus Program Notes (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.
Hesperus Program Notes (5) • In the statement printf(“ %dfeet\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.
Math Operators Used in C • You have seen the multiplication operator *. • Other mathematical operators are: • + addition • - subtraction • / division • % modulus
What is the Modulus Operator • 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.
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.
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.
The Formats in the Control String 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.
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.
scanf ( ) 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.
Scanf ( )’s Other Arguments • 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”.
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
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.
Another scanf() Peculiarity • When reading in numbers, scanf() will skip white space characters (blanks, newlines, and tabs). • When reading characters, white space is not skipped.