Programming in C - Doing the Math - Part III
#include <stdlib.h>
#include <stdio.h>
int X;
int Y;
int Z;
void main()
int X = 5;
int Y = 9;
int Z = X * Y;
printf ("%d", X);
printf(" * ");
printf ("%d", Y);
printf(" = ");
printf ("%d", Z);
There were a couple of different ways to accomplish this same task. Keep in mind that there is ALWAYS more than one right way to do things in computers. In the past, the method that used the least lines of code was generally considered the "Best" way to do it. This is because memory and storage space was at a minimum. With the advent of 1/2 Terabyte hard drives and 512 MB sticks of memory, this is no longer an issue. If it works, it works ok? You'll get better as you go along. Note to self - do TRY to keep the code clean and concise though. It makes it easier to understand down the road.
With that thought in mind, I think you've noticed that the programs are getting longer. We're just adding 2 numbers together, and we are already at 16 lines of code. (Interesting side note - programming languages like BASIC used to require you to have "Line Numbers" notating the order of operation of the program. Not necessary in C) How do you figure out where you are in a 10,000 line program? It would be easy to get lost no? YES! Because of this, programmers in the past have invented REMARK statements. The basis of a remark statement, is to have a way to identify different parts of a program, and keep them seperate from one another. For those of you who learned BASIC these were called "REM" statements. In C, however, they are displayed as follows:
/* ----------------------- This is a Remark -------------------- Please ignore everything between the slash/asterisk combinations ---------------------- */
Ok, that's fine and dandy, but how is it used in the real world?
/* ------------------------------ Here is a good place for the Name / Title of the program
--------------------------------- This Program Multiplies 5 * 9 and comes up with a
--------------------------------- Solution of 45
----*/
#include <stdlib.h>
#include <stdio.h>
/*@@@@@ END OF THE HEADER FILES @@@@@*/
int X;
int Y;
int Z;
/*======== END OF THE INTEGER SECTION ======*/
void main()
int X = 5;
int Y = 9;
int Z = X * Y;
/*_*_*_*_* Don't Forget To Add the "allow for input of numbers from keyboard" Section Here Later _*_*_*_*/
/******************** Printout Begins Here ************************************/
printf ("%d", X);
printf(" * ");
printf ("%d", Y);
printf(" = ");
printf ("%d", Z);
/******************** End of the Printout **************************************/
Notice the kind of notes and documentation that can be put here. Memo's for later upgrades, separators from one section to the other, remarks that a certain subroutine gets injected here, etc. It is strictly for the programmer, and will never be seen by the user of the program down the road. Don't believe me? Compile the program, Link it & Run it, and you'll see. These "remarks" are strictly "eyes only" for the programmer alone to see.
Keep in mind though, that the PURPOSE of remark statements is to make it EASIER to read and understand the source code, not to confuscate it. So you want them to be visable, but not completely overpowering the actual code.
While we are talking about making code easier to read, once again I can't stress the importance of keeping it short. The fewer command there are to accomplish the same task, the easier it is to read the source code. With that in mind, let me teach you a shortcut to something we've been doing the long way:
#include <stdlib.h>
#include <stdio.h>
int X, Y, Z;
void main()
int X = 5;
int Y = 9;
int Z = X * Y;
printf ("%d", X);
printf(" * ");
printf ("%d", Y);
printf(" = ");
printf ("%d", Z);
Can you tell the difference between this file, and the one on the very top of the page? We shortened up the area where we declared the integers:
Before:
After:
This can also be done with the "float" data type as follows:
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: