Programming in C - Doing the Math - Part II
#include <stdio.h>
int a;
int b;
void main()
If you typed this program in and tried to compile it, you should have run into a problem. I did this to you on purpose. The purpose of this course is to turn you into a debugger. You should be able to find and fix simple problems. In this case, we forgot an include statement right off the bat! Remember back in lesson 3, we said that the stdlib header file was needed to use the exit(0); command. The program can't exit, so your compiler WON'T compile it (hopefully).
Now that you've run into your first real troubleshooting problem, here's the code you should have typed in:
#include <stdlib.h>
#include <stdio.h>
int a;
int b;
void main()
Let's go through a line by line of this program just so you get the jist of what's going on here:
This command tells the linker to include the ANSI STANDARD LIBRARY file when putting the exe together. The program NEEDS this file, because the exit(0); command, and how it works, is defined in this library. Without the stdlib header file included, you computer wouldn't know what the exit(0); command does, and couldn't exit from the program (it would lock up if it actually compiled and ran).
This command makes the linker use the STANDARD INPUT/OUTPUT header file, which includes definitions for the function 'printf'.
Now that we have moved beyond our #includes, we begin declaring our variables.
These two lines basically say, "Hey, STUPID COMPUTER!! - I'm gonna use TWO variables within this program, and they will be called 'a' and 'b', so WATCH OUT for them!"
Now that the computer has it's eyes open, and is specifically watching for variables a and b, we can write the main part of the program, and hope that the computer doesn't get lost along the way. Keep in mind that computers are basically stupid, and you have to tell them every little baby step you want them to make, or they get lost along the way.
The first command, void main() tells the computer that this is the main part of the program, and that it will return a void. Everything within the curley braces '{ }' will be part of this program.
This tells the stupid computer that we are using a variable called 'a', that it is an integer, and that it's value will be 10 for now.
This tells the stupid computer that we are using a variable called 'b', that it is also an integer, and that it's value will depend on the value of 'a', because it will be 3 + whatever a is - in this case 10.
This is a pretty straightforward printf command. Note - no ' \n ' ( escape n ) to tell the computer to move to the next line... we want the answer to be on the same line as the problem.
Ok... this was kinda tricky... so pay attention:
For some dumb reason, the guys who invented C decided that you can't just tell the computer to print out a variable like you can in basic ( print A ), assembly put ( A, FF24 ) , or any other real language.
When using printf(), you have to tell it what type of format you are printing out. In this case, we used %d, which means integer ( go figure ). %f would be a float, %c would be a character, and %s would be a $tring (I'll explain later). For now, you need to know that the %d is important, and that it means integer, and that without it, you can't get printf to print a variable. The ' ,b ' portion of the line tells it what variable the %d represents.
And of course, we'll throw in a complimentary exit(0); statement, so we don't lock up the program.
Let's try another:
#include <stdlib.h>
#include <stdio.h>
float pi;
float x;
void main()
Did you catch what we did differently?
First, we used a different format type - a float instead of an integer. Floats are floating point decimals. Basically, they are any number that is a fraction of an integer, or has a decimal point somewhere in it.
Second, we used TWO letters ( pi ) for one of our variables. Variables in C can be complete names instead of just letters, so we can use variables like interest, Credit, or deBit. An important note here is that C is CaSe SeNsitivE ! Make sure your capitalization is correct, or at least that you do it the same every time, or you will run into problems.
Third, instead of adding the numbers, we are multiplying them. The answer, of course should be 2 times pi, or 6.283.
By the way... did you catch the error in the code?
If you typed the program in exactly as it is written, it should have compiled, linked, and run fine - with one small exception.... the ANSWER to the problem should have been wrong. 2 x 3.1415 is NOT 5.1415. Why was it wrong?
Time for another test:
Write a program that will multiply the numbers 5 and 9. Hint... the answer should be 45, but you shouldn't have to tell the computer the answer.... it's smart enough to figure it out on it's own.
On The Following Indicator...
(
GREEN
will indicate your current location)
|
|
1
|
2
|
3
|
4
|
5
|
|
6
|
7
|
8
|
9
|
10
|
[
ElectronicsTheory.Com
]
This Course was written by Ray Dall © All Rights Reserved.
This page and all it's content Copyright, Trademarks, Intellectual Properties
and other legal issues 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Ray Dall.
All Rights Reserved.
And for what it's worth... this page was last updated HexDate 01-11--7D1
VISITORS: