We got the code to compile, but if we run it, nothing happens. The answer is calculated, but the program does not yet show us the result.
We have already seen how we can print information to the screen in the hello.c program using printf(). We could put statements in this program like:
printf("You have 57 shares of XYZ Corp.\n");
But this is rather inefficient. Suppose we buy more shares of XYZ stock. We must modify our program to change the number of shares we have AND we must remember to change the value in both places: the assignment, and the printf().
The string we are passing to printf() is called a format string. We can put whatever words we want to appear on the output there, as well as special characters to control the screen, such as the '\n'.
We can also put a conversion specifier into the format string to tell printf() to substitute a value at that point in the output. We must also, then, give printf() the value to be substituted. For example, to convert an integer to be printed, the conversion specifier is "%d", so the above printf() would be written:
printf("You have %d shares of XYZ Corp.\n",shares);
To convert a float value to be printed, the specifier is "%f".
We can make these changes in a new file called stock1.c
Going back to step 5, we try to compile and test the program again.
Something Went Wrong AGAIN!?
When we compile, we get the error:
stock1.c: In function āmainā:
stock1.c:27: warning: incompatible implicit declaration of built-in function āprintfā
/tmp/cc4Y2Z9O.o(.text+0xa3): In function `main':
stock1.c: undefined reference to `prinf'
collect2: ld returned 1 exit status
What does this mean?
This is called a link time error. The program compiled succesfully (we spoke correct C), but when the object file was linked, there was no function called prinf() in either our file or any library file.
We spelled printf() incorrectly.
Fixing that, the program compiles to an executable file, a.out. We run the program and get:
You have 57 shares of stock
selling at $3 8/130
The value of your portfolio is $627.000000
This time the program compiled but look at the selling price.
Something is still wrong.
The first thing we notice is that the output echoing the data values is wrong.
You have 57 shares of stock
selling at $3 8/130
The value of your portfolio is $627.000000
The stock price says 3 8/130, where is should say 11 3/8.
Look at the code in stock1.c.
We did not supply enough data to printf() - we have 3 conversion specifiers in the format string, but only 2 variable values being passed to printf(). This is a case where we have used syntacticallycorrect C (the compiler understands all of the statements), but not semantically correct - we did not use some statement correctly.
Fixing that, we get:
You have 57 shares of stock
selling at $11 3/8
The value of your portfolio is $627.000000
The output looks better - but the answer is still WRONG!
The errors we have made up till now have simply been mistakes, this is a bug.
The first technique we can use to debug a program, is to manually trace the execution of the code. Here we play computer, and execute the program step by step.
Recall, when a program begins execution, is starts in main(), then executes functions and statements in order one at a time. Trace through the code, while keeping track of variable values to find the bug.
While tracing, we should keep in mind Precedence and Associativity of Operators.