Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




2.7. A Larger (non)Program

The small amount of programming we have shown so far isn't enough to make a decent interactive program. To keep it simple, we will write just a skeleton program so you can see the structure and usage of header files without getting bogged down in new concepts. In Chapter 3 we will write a full version of this program. The code here can be compiled and run but it will not ask the user for any input or calculate the price.

First we have main.c, this will only contain the function main(). main() will call some of the functions we define in other files. Note that main.c doesn't have a line #include <stdio.h> as it does not use any of the functions in the Standard Device I/O library.

Example 2-4. main.c

#include "display.h"
#include "prices.h"


  return 0;

Next we have display.c. This contains two functions, both of which are called from main() and so we put there declarations in a header file display.h.

Example 2-5. display.c

#include <stdio.h>

  printf("Welcome to the pizza parlor\n");
  printf("What size pizza would you like? (in inches)");

  return 0;

  printf("Your pizza will cost 0.00\n");

  return 0;

Example 2-6. display.h

/* header file just contains function declarations, an file that wants
 * to use either of these functions just has to #include this file */
int display_options(void);
int display_price(void);

Finally we have prices.c which contains the functions for getting input from the user and calculating the total cost of the pizza. Only one of these functions is called from main(), the declarations for the other two are therefore put at the top of the file. We'll fill in the code for these functions in Chapter 3.

Example 2-7. prices.c

int get_size(void);
int get_toppings(void);

  /* insert code here.  Will call get_size() and get_toppings(). */
  return 0;

  /* insert code here */
  return 0;

int get_toppings()
  /* insert code here */
  return 0;

Example 2-8. prices.h

int calculate_price(void);

This can then be compiled with the command: gcc -Wall -o pizza_program main.c prices.c display.c. When run, it will display a greeting and announce that your pizza costs "�0.00".

  Published under the terms of the GNU General Public License Design by Interspire