Monday, May 24, 2010

Can't we use user defined methods of one program in another program using c language?

#include%26lt;stdio.h%26gt;


#include%26lt;Math.h%26gt;


float logor(int);


float rootof(int);


main(){


int n,res;


printf("Enter one number");


scanf("%d",%26amp;n);


printf("\nThe logorthem value of this number is :%f",logor(n));


printf("\nThe square root of this number is: %f",rootof(n));


getch();


}


float logor(int n){


return log(n);


}


float rootof(int n){


return sqrt(n);


}





In this program we have used two user defined functions. those are logor and the rest is rootof. can we use both of these two things in another program. If not why?. we r using predefined functions like log and sqrt. And these methods are also one of methods in the compiler. But they are being reused. But we can't use user defined methods. can anyone tell me the reason behind this logic?

Can't we use user defined methods of one program in another program using c language?
To use them in another program you have to put them away in the right place so the system can recover them. That file would be included like you have done with math.h


When I was teaching computer literacy, I would point to a floppy disk laying on the table and say, "You have to put that in the drive. The Russians are working on reading diskettes laying on the table, but we don't have it yet."


You are wanting the connection to be made to your function automatically by some kind of mind reading process and that don't happen (yet.)
Reply:It is actually common to share some functions among multiple programs. There are a few ways to do this. However, the best way is to create a library.





First, some explanation. When you make a C program, you have one or more .c files. These are your source files containing your C source code. These are compiled into object files by the compiler. The object files typically are .o or .obj files depending on the compiler. Files like main.c are used to create main.o. The object files contain machine language, which is the language of the CPU.





After the object files are created, they have to be combined to make the final executable. You see, the object files are not really complete. If one object file is trying to call a function in another object file, the compiler cannot really make that happen. So it puts a marker indicating what it wants to do and leaves that final resolution to the linker step.


The last phase is called linking. This is like doing connect the dots. The linker finds all the functions in all the object files then finds all the places where they are called and inserts the actual calls to the functions.





An object file can be used in more than one program by specifying it to the linker.





Programmers are always trying to make life easier. And because this sharing of code is common, they came up with a way to make this process easier. Object files can be combined into a library. At the link phase, instead of linking with the object files, the library is used.





There is nothing that special about a library. It isn't that much different than a zip file that contains multiple files. Using a library or set of object files directly is the same thing. In fact, a program can be built (and often is) from a number of object files and libraries.





All the functions provided by C come from libraries. The C programming language has no built-in functions. So when you write a C program and call printf, you are already using a library.





How you create and use libraries will differ from one compiler to another. Read Read Read.





Good luck.
Reply:If you want to use user defined functions in another program, create a library that contains the functions.


No comments:

Post a Comment