ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm currently using a text to learn C programming, in which an instructive program (below) uses separate files, but talks only of Unix & DOS environments for how to compile them together. Wondering if anyone can explain how to "compile them together" in the Unix environment (using clang compiler)? All 3 files are in the same directory.
>>>>>>>>>>>>>>>
PROGRAM 9.9
Code:
/* usehotel.c -- room rate program */
/* compile with Listing 9.10 */
#include <stdio.h>
#include "hotel.h" /* defines constants, declares functions */
int main(void)
{
int nights;
double hotel_rate;
int code;
while ((code = menu()) != QUIT)
{
switch(code)
{
case 1 : hotel_rate = HOTEL1;
break;
case 2 : hotel_rate = HOTEL2;
break;
case 3 : hotel_rate = HOTEL3;
break;
case 4 : hotel_rate = HOTEL4;
break;
default: hotel_rate = 0.0;
printf("Oops!\n");
break;
}
nights = getnights();
showprice(hotel_rate, nights);
}
printf("Thank you and goodbye.");
return 0;
}
PROGRAM 9.10
Code:
/* hotel.c -- hotel management functions */
#include <stdio.h>
#include "hotel.h"
int menu(void)
{
int code, status;
printf("\n%s%s\n", STARS, STARS);
printf("Enter the number of the desired hotel:\n");
printf("1) Fairfield Arms 2) Hotel Olympic\n");
printf("3) Chertworthy Plaza 4) The Stockton\n");
printf("5) quit\n");
printf("%s%s\n", STARS, STARS);
while ((status = scanf("%d", &code)) != 1 ||
(code < 1 || code > 5))
{
if (status != 1)
scanf("%*s");
printf("Enter an integer from 1 to 5, please.\n");
}
return code;
}
int getnights(void)
{
int nights;
printf("How many nights are needed? ");
while (scanf("%d", &nights) != 1)
{
scanf("%*s");
printf("Please enter an integer, such as 2.\n");
}
return nights;
}
void showprice(double rate, int nights)
{
int n;
double total = 0.0;
double factor = 1.0;
for (n = 1; n <= nights; n++, factor *= DISCOUNT)
total += rate * factor;
printf("The total cost will be $%0.2f.\n", total);
}
PROGRAM 9.11
Code:
/* hotel.h -- constants and declarations for hotel.c */
#define QUIT 5
#define HOTEL1 80.00
#define HOTEL2 125.00
#define HOTEL3 155.00
#define HOTEL4 200.00
#define DISCOUNT 0.95
#define STARS "**********************************"
// shows list of choices
int menu(void);
// returns number of nights desired
int getnights(void);
// calculates price from rate, nights
// and displays result
void showprice(double rate, int nights);
What you have here are two .c files and a header file that are parts of the same program.
The third file is supposed to have the filename "hotel.h" - it even says as much in the comments. It is pulled in by the #include "hotel.h" statement in the first file.
The second file by itself is not a program at all, as it lacks the "main" function. It's pretty obvious that it belongs to the same program as the first file, as it contains functions that are used but never defined in the first file.
Either #include the second file in the first, or just join the two files together with cut and paste as the comment in the first file suggests.
Sorry, but that doesn't seem to jibe with the text discussion, where they speak of such things as a command line such as "gcc file1.c file2.c for Unix compilers.
In this case, there are 2 separate .c files (and the .h file). The text discusses the use of putting the functions related to the driver in a separate file, and then putting constants, etc in a separate header file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.