LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   2 programming questions (http://www.linuxquestions.org/questions/programming-9/2-programming-questions-284331/)

hk_michael 01-30-2005 11:52 PM

2 programming questions
 
Hi all!
My project is required to do the thing now:
the program continues to run until I press Ctrl+C to break it as the PC is acted as server. So, I wish to know how can I do so that such effect can be done?
Also, as my progmra has man many lines, in order to let my supervisor and even me to look clearer, I wish to break the program into few files. But my C profgram does not contain class. So, I wish to know that how can I seperate the program into few files and then create a make up file so that I can compile all files together?
Thanks

Libu 01-31-2005 12:36 AM

Quote:

Also, as my progmra has man many lines, in order to let my supervisor
and even me to look clearer, I wish to break the program into few files. But my
C profgram does not contain class. So, I wish to know that how can I seperate the
program into few files and then create a make up file so that I can compile all files
together?
What i do is, create a header file which contains the function prototypes of all the functions that Iam using in my project and then separate the function definition in separate .c files.

For e.g. if u have fucntion prime_num(int) :
I would the following in the custom header file:

Code:

#include <stdio.h>
..
int prime_num(int) /*Function Decalaration*/
..
.

Then I would create a .c file prime_num.c:
Code:

#include "custom_headerfile"

int prime_num (int x) {
/*Function Definition*/
}

Ur main file would be something like this:
Code:

#include....
#include "custom_headerfile"

int main () {
  int x;
.
.
prime_num(x);
..
..
}

As for makefiles, its quite simple actually. Hereis a good link
http://users.actcom.co.il/~choo/lupg...makefiles.html

csfalcon 01-31-2005 09:01 AM

if you program will be stopped by ctrl + C, then it can be a big infinit loop.

Code:

while (1)
{
}

This is how most servers are implemented.

As to make your program more readable/managable, you can pick out some "logical groups" of functions and put them in different file with .h and .c file. For examples, all the helper functions or functions that perform different parts of a complex calculation.

jonaskoelker 01-31-2005 01:35 PM

> int prime_num(int) /*Function Decalaration*/
not that there's a missing semicolon ('int prime_num(int);').

also, you might want to look at the library function 'signal' (signal.h), SIGINT and SIGTERM.

Best of luck with your project,

Jonas

Libu 02-01-2005 12:02 AM

Oops :o)!! ya, jonaskoelker, I missed that :) !!


All times are GMT -5. The time now is 12:08 AM.