Since we just had a fresh look at working with multiple modules, here’s a quick exercise to make it even more familiar for you! Write a program that prompts the user for an integer value and displays the value in base eight (octal) and base sixteen (hexadecimal):
Please enter a base ten integer: 59
The value in octal is 073
The value in hexadecimal is 0x3b
A very simple program, but we’ll complicate it a bit by splitting everything into multiple files. Here’s what you should write:
main.c — This gets the integer from the user and passes it as an argument to the DispOct and DispHex functions.
dispoct.c — This contains the implementation of the DispOct function, which receives an integer value from the caller and displays it to stdout in octal.
dispoct.h — This contains the prototype of the DispOct function.
disphex.c — This contains the implementation of the DispHex function, which receives an integer value from the caller and displays it to stdout in hexadecimal.
disphex.h — This contains the prototype of the DispHex function.
Once you’ve written the source code, you should build each module by hand one at a time using the -c switch with gcc. (Don’t forget to #include the header files into the appropriate .c files so that any function prototypes that are needed in any module are available!)
The commands to build the individual object files are:
Confirm that you have all three object files by producing a directory listing (i.e., ls -l). Then link all three object files together to produce a.out. (BTW, you don’t compile header files because they normally don’t contain any code that allocates space in memory, they only contain code that is information for the compiler.) Then you can link all three object files into a single executable with this command:
If all goes well, that should produce the final executable a.out, which you can then run from the command line as usual.
This program isn’t necessarily rocket science, I’m more interested in getting you closer to the build process, one file at a time to see how the pieces fit together. In the near future we’ll look at less tedious ways to handle multi-module projects. So leave all of your files in a directory named HW09 so I can collect them later.
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.Read more
Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.Read more
Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.Read more
Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.Read more
By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.Read more