funtion reuse

This is a discussion on funtion reuse within the C Programming forums, part of the General Programming Boards category; Hi everyone, I've just started learning C. I'm trying to figure out one thing, if I have a program, say ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    funtion reuse

    Hi everyone, I've just started learning C.
    I'm trying to figure out one thing, if I have a program, say myprogram.c with a main and a bunch of functions in it, how could I take out function1 save it in a file say myfunction.c and have myprogram.c see function1?

    I know this is a very basic question, I'm just starting out, thank you for any reply.

    Regards,
    Luca.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You would use a .h file...

    1) Save the function(s) to their separate C page.
    2) Create a new .h file with function prototypes in it
    3) Include it into your main source page.

    Code:
    // functions.c
    
    int add(int x, int y)
      { return x +  y; }
    
    int multiplay (int x, int y)
      { return x * y; }
    
    ////////////////////////////////////////////////////////////////////////
    // functions.h
    
    int add(int x, int y);    // ---- Note the semicolon.
    int multiply(int x, int y);
    
    
    //////////////////////////////////////////////////////////////////////////
    // main.c
    
    #include "functions.h"   // --- note quotes not brackets
    
    int x, y;
    
    x = add(7,4);
    
    y = multiply(3,2);

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    ... and somehow coax the IDE to add the new .c source to the project.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    ... and somehow coax the IDE to add the new .c source to the project.
    Thanks... I forgot to mention that.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    hi

    I tried to follow the steps but I'm getting this error
    "undefined reference to 'WinMain@16'" while I try to compile stringutils.c, here's the code

    stringutils.c

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAXLEN 1000 /* max length of any input line */
    int getline(char *, int);
    char *alloc(int);
    
    /* readlines: read input lines */
    int readlines(char *lineptr[], int maxlines)
    {
      int len, nlines;
      char *p, line[MAXLEN];
      
      nlines = 0;
      while((len = getline(line, MAXLEN)) > 0)
        if(nlines >= maxlines || (p = alloc(len)) == NULL)
          return -1;
        else
        {
          line[len-1] = '\0'; /* delete newline */
          strcpy(p, line);
          lineptr[nlines++] = p;  
        }  
      return nlines;    
    }
    
    /* writelines: write output lines */
    void writelines(char *lineptr[], int nlines)
    {
      int i;
      
      for(i = 0; i < nlines; i++)
        printf("%s\n", lineptr[i]);  
    }
    
    /* getline: get line into s, return length */
    int getline(char s[], int lim)
    {
      int c, i;
    
      i = 0;
      while(--lim > 0 && (c = getchar()) != EOF && c != '\n')
        s[i++] = c;
      if(c == '\n')  
        s[i++] = c;
      s[i] = '\0';
      return i;
    } 
    
    char * alloc(int n)
    {
    }
    here is the header file

    stringutils.h

    Code:
    int readlines(char *lineptr[], int nlines);
    void writelines(char *lineptr[], int nlines);
    I'm using Dev-C++ to edit and compile the source files

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > "undefined reference to 'WinMain@16'" while I try to compile stringutils.c, here's the code
    You created a GUI project, but you should have created a "Win32 Console" project.

    You might be able to change the project type, but it's usually simpler just to create a new project with the right type (console), and then add your single source file to that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    hi

    That's fine now.
    Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parseString funtion (stream I/O)
    By dhardin in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2010, 02:38 PM
  2. what design patter let me reuse this class?
    By patiobarbecue in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2009, 01:44 PM
  3. reuse a socket connection
    By radeberger in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-18-2009, 11:38 AM
  4. locate funtion
    By DanC in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2006, 02:21 PM
  5. Funtion pointer in structure
    By Xzyx987X in forum C Programming
    Replies: 1
    Last Post: 07-03-2004, 03:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21