Posted: 1/28/2007 4:09:12 PM EDT
|
Im learning C. Im fine with just one file programming (main.c) but its bad style, so Im learning functions now, and I dont have a damn clue what to do with the 3 files (main.c, functions.c, functions.h). The function and function prototypes are about as clear as mud to me... If I can figure out how to convert this into a function I'll be ok. This is my "one file code" that I need to convert to a function, "3 files". //linear equation { int inputA, inputX, inputB, //stores values from users resultY; //stores product from equation //Prompt for input printf("Please enter 3 integer values with no commas, use only spaces (x, a, and b) for use in a linear equation:"); //Get input scanf ("%d",&inputA); scanf ("%d",&inputX); scanf ("%d",&inputB); //Perform calc y = ax + b (x, y, a, and b are integer numbers) resultY = inputA * inputX + inputB; //Output result printf("linear equation: y = ax + b = %d * %d + %d = %d\n", inputA, inputX, inputB, resultY); } Any help? |
I'm a bit uncertain of what you really want. The .h file should be in the same directory as the rest of the .c files and you should include it in your programs with a #include <"functions.h"> header.
$ cat foo.c #include <stdio.h> int function(void) { int inputA, inputX, inputB, //stores values from users resultY; //stores product from equation //Prompt for input printf("Please enter 3 integer values with no commas, use only spaces (x, a, and b) for use in a linear equation:"); //Get input scanf ("%d",&inputA); scanf ("%d",&inputX); scanf ("%d",&inputB); //Perform calc y = ax + b (x, y, a, and b are integer numbers) resultY = inputA * inputX + inputB; //Output result printf("linear equation: y = ax + b = %d * %d + %d = %d\n", inputA, inputX, inputB, resultY); } int main(void) { function(); } $ gcc -o foo foo.c $ ./foo Please enter 3 integer values with no commas, use only spaces (x, a, and b) for use in a linear equation:1 2 3 linear equation: y = ax + b = 1 * 2 + 3 = 5 |
the quantity of files is irrelevant, you could use one file with a million lines of code or a million files with one line of code. your file shouldn't need specific names (you would in java). if you call your file "arfcom.c" it will work just as well as "main.c" - the solution posted above is correct (at least it appears to be to me). in C++, things are more object oriented. to output the variable x, it would be something like: cout << x; julenissen posted the way to use it with a function: #include <stdio.h> and compiled it and viewed it with gcc compiler. eta: what compiler are you using? Borland? Visual Studio? It's not for some special embedded microchip is it (like a PIC or something)? eta2: maybe your teacher wants all of your functions in one file, and the main function in another? |
|
Let me see if I can figure out what you're trying to do... You already have the code you need to run in brackets, you just need to make it a function by adding, say, void Function(void) { //code } This creates a function called Function that doesn't accept or return any variables. Whenever you create a function, you have to make a prototype of it. This is simply the first line with a semicolon added at the end at the beginning of the file. For the above function, it would be like: void Function(void); And this has to be before the code that calls Function. The actual function code can be wherever you want. Once it's written properly, you call it like this: Function(); That's the basics. julenissen's post is direct output from writing, compiling, and running the code, no C++. |
|
This is what you should do. The functions.h should contain the function declarations and maybe global variable names. The functions.c file should contain the function definitions and local variable names. The main.c file contains the main{} function which makes calls to all the functions defined in the functions.c file and uses global variables. Since it's coming from ar15.com General Discussion, take it with a grain of salt. Have a great day. |
He's talking about making sure your program can handle anything that you might put into it. As it is now, putting the wrong input in could make the program crash or do other strange things, like if you tried to type "hello" where it asks for a number. A final program that's ready to be used by actual users should check all of it's inputs and handle any unexpected imputs gracefully. But there's no need for it in a simple training program like yours, especially when you're just learning about functions. |
int function(void) {The above is technically correct I THINK in old K&R style C, but most modern compilers will report an error if you do not explicitly return a value as below. int function(void) { |
|
function prototype is needed only if main.c is compiled before function.c. You want to declare function prototype in function.h and include that with #include statement in your main.c It tells the compiler not to puke out if function.c has not been compiled yet. Ah, this bring me back some sweet memories (like 15 years ago, sniff sniff). Now I am just a lazy java guy. |
It will return an warning if you compile with gcc and -Wall. |
