Thread: Trouble with linking files

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    45

    Trouble with linking files

    I'm trying to learn how to link two files together so that I can send a floating point number from one file to the other. I either get a compiler error or the value sent is simply 0, not the number I entered.

    I just began learning about linking files, so this is probably a very easy fix. Please let me know what I'm doing wrong.

    File 1:
    Code:
    #include<stdio.h>
    
    int main(float num)
    {
    	values();
    	printf("\n...So you like %lf.", num);
    	
    	return 0;
    }
    File 2:
    Code:
    #include<stdio.h>
    
    float values()
    {
    	#include<stdio.h>
    
    float values()
    {
    	float num;
    	
    	printf("\nWhat's your favorite decimal number?\n");
    	scanf("%lf", &num);
    	
    }
    	
    
    
    Thanks.
    
    	
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And where's your return statement in values? And where do you assign the value that should be returned in main?

    [Think about calling something from the math library, like say sqrt. If you say sqrt(foo), the value is returned but doesn't go anywhere. You need to put the answer somewhere, if you want to have it later on -- as in bar=sqrt(foo).]

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    The value that is returned to main is being assigned using scanf...isn't it?

    Code:
    scanf("%lf", &num);
    The same exact code works with integer values, but not with char or float.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Uhh, no.
    You need to learn how functions work first.
    Then you can put them in different source files.
    The problem is that you haven't learned how to use functions correctly, not that the functions are placed in different source files.
    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.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Please don't get frustrated with my questions. I know I'm not skilled at programming.

    How about this...why is this not working properly? I can make it work if I make it a single file with two functions, but I can't make it work as two files.

    File 1:
    Code:
    #include<stdio.h>
    
    int main()
    {
    	float i;
    	i = values(0.5,0.4);
    	printf("Num is %f.\n", i);	
    
    }
    File 2:
    Code:
    #include<stdio.h>
    
    float values(float a, float b)
    {
    	float n;
            n = a-b;
    
    	return n;
    }
    The values a and b aren't being sent properly. I don't understand why. Can someone explain why instead of telling me that I'm stupid and don't understand functions...I already know that much.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think that's because you use C and it's stupid ability to implicitly call functions.
    If you haven't done so, create a header file for your 2nd source file and add a prototype for values. Include header in main source file.
    Last edited by Elysia; 06-17-2008 at 03:37 PM.
    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.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by magda3227 View Post
    Please don't get frustrated with my questions. I know I'm not skilled at programming.

    How about this...why is this not working properly? I can make it work if I make it a single file with two functions, but I can't make it work as two files.

    File 1:
    Code:
    #include<stdio.h>
    
    int main()
    {
    	float i;
    	i = values(0.5,0.4);
    	printf("Num is %f.\n", i);	
    
    }
    File 2:
    Code:
    #include<stdio.h>
    
    float values(float a, float b)
    {
    	float n;
            n = a-b;
    
    	return n;
    }
    The values a and b aren't being sent properly. I don't understand why. Can someone explain why instead of telling me that I'm stupid and don't understand functions...I already know that much.

    How do you know? Do you not get 0.1 (or the nearest float equivalent)? What error/other output are you getting?

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Quote Originally Posted by tabstop View Post
    How do you know? Do you not get 0.1 (or the nearest float equivalent)? What error/other output are you getting?
    I used
    Code:
    printf("%f", a);
    in the second file to check if a was sent properly, but it output 0.

    The overall output of the program is something large negative number like -17332847.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, if you're not using prototypes (i.e., if what you've typed is actually what you've got) then you're getting loads of warnings about that. You need to tell main that the function values exists (normally you would put the prototype in a header file, like values.h, which you can then #include), otherwise the floats will get passed as ints (the default "I don't know what it is" variable type in C).

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Thanks. Using the prototypes worked.

    I hate to ask another question, but how would you have a function return an array so that it can be stored into a different array in the main function? For example...

    file2 creates an array A that is m integers long. How would I return A[m] to the main function and store it as an array B[m] in which I can access each member of the array? Basically, I want to copy the array from one file to the other. How would this be done?

    I don't understand much about pointers, but have a feeling that they would be used here. If so, does anyone know any good resources for learning more about pointers? Every one I find is confusing. The more examples, the better for me. I learn visually.
    Thank you for all the help.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't return arrays.

    You can return pointers to memory, so you would have to grab some memory with malloc (you can "pretend" it's an array, for example, by using foo[i] notation) and pass it around.

    However, the general method is to pass the array in and out as an argument, rather than returning it.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    An array isn't really an array in C. It's one big block of memory instead.
    The compiler just uses information provided by the code to calculate an offset into the memory where data is stored and read.
    The formula is:
    n + dimensions * dimension_width

    This, in turn, makes it possible to allocate dynamic memory using malloc (don't forget to free) and treat the pointer as an array (malloc returns a pointer).
    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. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. Linking multiple source files: Undefiled Reference to...
    By Inquirer in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2003, 05:47 PM
  4. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM