preprocessor is invoked The source code is modified
The compiler invoked itself is
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
Source code lines that begin with a # are called preprocessing directives.
Preprocessing directives indicate to the preprocessor that certain header files should be included with the source code and certain substitutions should be made.
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.
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 .
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 .
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.
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 .
The digits in the decimal number 234 actually have values (starting from the right or low-order position):
4 x 10 0
3 x 10 1
2 x 10 2 = 4 x 1 = 3 x 10 = 4 = 30 = 2 x 100 = 200 Total = 234
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 .
The digits in the binary number 1011 actually have values (starting from the right or low-order position):
1 x 2 0
1 x 2 1
0 x 2 2
1 x 2 3 = 1 x 1 = 1 x 2 = 0 x 4 = 1 x 8 = = = = 1 ( numbers 2 on the 0 right are 8 decimal Total = 11 numbers )
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 bits make up a byte .
bit (binary digit). And all eight
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 )
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 numbers and as hexadecimal octal numbers in this chart.
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 )
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.
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.
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.
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
When variables are declared you must indicate the variable case).
type (the kind of values that will be stored there -- integers in this
You can declare several variables of the same type by separating the variable names by commas.
The equals sign ‘ = ‘ is used as the C.
assignment operator in
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;
In the statement printf(“ %d feet\n”, feet ); the spaces before %d are printed on output because they are within the quotes. %d the printf() function to tells display the value stored in the variable feet as an integer.
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.
You have seen the multiplication operator * .
Other mathematical operators are:
a % b yields the remainder after a is divided by b .
13 % 5 yields 3
Note that ‘ % ’ is also used in a printf() statement to start a conversion specification.
printf() is used for printing output. When printf() is called it is passed a list of arguments of the form:
& other arguments
The arguments to printf() are separated by commas.
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 .
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.
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.
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.
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 ”.
Conversion How characters in the Character input stream are converted.
c Character d f lf Lf s decimal integer floating-pint number (float) floating-point number (double) floating-point number (long double) string
With printf() the is used to print float or a double.
%f format either a
With scanf() the format %f is used to read in a float , and %lf is used to read in a double .
When reading in numbers , scanf() will skip white space characters (blanks, newlines, and tabs).
When reading characters , white space is not skipped.