Thread: The Preprocessor; Using the #include statement

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    19

    The Preprocessor; Using the #include statement

    I am doin the 13.3 program in Programming in C by Stephen G. Kochran. I put out the program like it supposed to.

    Code:
    /* Program to illustrate the use of the #include statement
       Note: This program assumes that definitions are
       set up in a file called metric.h             */
    
    #include <stdio.h>
    #include "metric.h"
    
    int main (void)
    {
    	float liters, gallons;
    
    	printf ("*** Liters to gallons ***\n\n");
    	printf ("Enter the number of liters: ");
    	scanf  ("%f", &liters);
    
    	gallons = liters * QUARTS_PER_LITER / 4.0;
    	printf ("%g liters = %g gallons\n", liters, gallons);
    
    	return 0;
    }
    and I gettting the errors:


    1>------ Build started: Project: Program 13.3, Configuration: Debug Win32 ------
    1>Compiling...
    1>13-3.c
    1>f:\c programming\program 13.3\program 13.3\13-3.c(6) : fatal error C1083: Cannot open include file: 'metric.h': No such file or directory
    1>Build log was saved at "file://f:\C Programming\Program 13.3\Program 13.3\Debug\BuildLog.htm"
    1>Program 13.3 - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    I have Microsoft Visual C++ 2008 compiler, how come it doesn't know what #include "metric.h" is?

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Do you have metric.h in the same directory as .c source file?

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    19
    I don't know if I do, how do you know if its in the directory, its probably not in there, how do you insert metric.h in the same directory?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Where is your source file ( .c file) ? Is it :\c programming\program 13.3\program 13.3\13-3.c(6) :? Then the header file matric.h should be also in that folder.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is normal file system business we're talking about here. Like you have a directory where you store all your music, your pictures, your videos, whatever. Likewise your project is in a directory and your source file, as well. Put the .h file in the very same directory as the source file. Then it will work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    19

    The Preprocessor; Using the #include statement

    I added the metric.h in the source files like in the image. The program still doesn't work, what should I correct to make the program work?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It seems that QUARTS_PER_LITER is not defined anywhere. Considering this should be an example, it might help if you showed the contents of the header file metric.h?
    Otherwise you'd have to define QUARTS_PER_LITER manually. What it's supposed to be, however, I don't know.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    19
    I looked back in the book and it said I had to define the measurements in a attached file which I attached and had nothing in the file. In the metric.h file below I put in:

    Code:
    #define INCHES_PER_CENTIMETER 0.394
    #define CENTIMETERS_PER_INCH  1 / INCHES_PER_CENTIMETER
    
    #define QUARTS_PER_LITER   1.057
    #define LITERS_PER_INCH	   1 / QUARTS_PER_LITER
    
    #define OUNCES_PER_GRAM    0.035
    #define GRAMS_PER_OUNCE    1 / OUNCES_PER_GRAM
    Then I runned the program with start without debugging and got the program to work. Thank you Elysia so much , I am new to Preprocessors, giving me that clue to show the metric.h file gave me the idea which I looked back into the book and told me I had to put the #define statements in the metric.h file to have the program understand where the information was coming from.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hmm. Well, the point of headers are that you can put information inside them that can be shared between different source files. If two or more files need those defines, then it's a good idea to put them in the header. Including a header simply pastes the information inside it into the source file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem while constructing IP packet and sending using socket() system call
    By cavestine in forum Networking/Device Communication
    Replies: 10
    Last Post: 10-15-2007, 05:49 AM
  2. does not name a type ERROR
    By DarrenY in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2007, 04:54 AM
  3. MFC include BS
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 10-31-2005, 12:44 PM
  4. dont konw what to do next.
    By negevy in forum C Programming
    Replies: 29
    Last Post: 09-09-2005, 03:06 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM