Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
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?
1/28/2007 4:38:00 PM EDT
[#1]

Quoted:
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...



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.




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);

}




$ 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

1/28/2007 4:48:09 PM EDT
[#2]
Please check the return value from scanf().

Consider writing a safer function to perform prompted input with error handling.
1/28/2007 4:50:56 PM EDT
[#3]
ouch, that looks like C++...Im learning C. We we taught the first couple of weeks to code it all in 1 file (main.c), now they are teaching us functions and the use of the 3 files.

Im a beginner programmer.
1/28/2007 4:53:34 PM EDT
[#4]

Quoted:
Please check the return value from scanf().

Consider writing a safer function to perform prompted input with error handling.


yea, that sounds good. Err, I'll be right back. I gotta get in the book and lookup what all that means

thanks anyhow
1/28/2007 5:00:46 PM EDT
[#5]

Quoted:
ouch, that looks like C++...Im learning C. We we taught the first couple of weeks to code it all in 1 file (main.c), now they are teaching us functions and the use of the 3 files.

Im a beginner programmer.


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>

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();
}


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?

1/28/2007 5:02:02 PM EDT
[#6]
The syntax for basic functions is the same for C and C++ pretty much.

1/28/2007 5:02:22 PM EDT
[#7]
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++.
1/28/2007 5:04:23 PM EDT
[#8]
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.
1/28/2007 5:11:33 PM EDT
[#9]
thanks guys, I'll mash though it eventually.

oh, Im using Visual Studio 2005.
1/28/2007 8:27:33 PM EDT
[#10]

Quoted:

Quoted:
Please check the return value from scanf().

Consider writing a safer function to perform prompted input with error handling.


yea, that sounds good. Err, I'll be right back. I gotta get in the book and lookup what all that means

thanks anyhow


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.
1/28/2007 8:37:42 PM EDT
[#11]
Some advice - learn C++ instead. OOP is the way to go. And don't buy into any bullshit about "learn C first" or "C++ is basically C again". It's a whole different mindset, requiring entirely different design approaches.
1/28/2007 8:40:06 PM EDT
[#12]
Be sure to tell the compiler know that it needs to compile more files that just the one that has main().. it might be under project settings
1/28/2007 11:12:57 PM EDT
[#13]
I got er figgured out.  I had to re-read some parts of the book.

Thanks for all the help!
1/28/2007 11:31:58 PM EDT
[#14]

Quoted:
Some advice - learn C++ instead. OOP is the way to go. And don't buy into any bullshit about "learn C first" or "C++ is basically C again". It's a whole different mindset, requiring entirely different design approaches.

this man speaks truth.
1/28/2007 11:39:05 PM EDT
[#15]
C++ is a piece of shit.  That's all I'm going to say.
1/28/2007 11:42:06 PM EDT
[#16]
int function(void) {
...
printf("linear equation: y = ax + b = %d * %d + %d = %d\n", inputA, inputX, inputB, resultY);
}


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) {
...
printf("linear equation: y = ax + b = %d * %d + %d = %d\n", inputA, inputX, inputB, resultY);
return(0);
}

1/28/2007 11:44:30 PM EDT
[#17]

Quoted:
C++ is a piece of shit.  That's all I'm going to say.


I've been earning a living on that piece of shit for 15 years.

It has its place, especially for low-level and OS work.
1/28/2007 11:46:17 PM EDT
[#18]
And yet the majority of low level and operating system code is written in C.  But hey, I don't want to argue with you, personal preference I guess.  I've been programming in C for 10 years, and I don't think I'm going to move.
1/29/2007 3:40:07 AM EDT
[#19]
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.
1/29/2007 3:43:47 AM EDT
[#20]
I taught myself some C++ so I know about enough to grasp what you are trying to do.  sorry I could not help more.
1/29/2007 6:06:28 AM EDT
[#21]

Quoted:
int function(void) {
...
printf("linear equation: y = ax + b = %d * %d + %d = %d\n", inputA, inputX, inputB, resultY);
}


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) {
...
printf("linear equation: y = ax + b = %d * %d + %d = %d\n", inputA, inputX, inputB, resultY);
return(0);
}



It will return an warning if you compile with gcc and -Wall.