Function & header files

Discussion in 'C Programming' started by Umesh, Jun 12, 2007.

  1. Umesh

    Umesh Guest

    how to convert a program to a function/macro and put it in a header
    file? is there any shortcut method for this?
    thanks.
     
    Umesh, Jun 12, 2007
    #1
    1. Advertising

  2. Umesh said:

    > how to convert a program to a function/macro and put it in a header
    > file?


    Don't even try. Instead, find out how to use libraries.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Jun 12, 2007
    #2
    1. Advertising

  3. Umesh

    Guest

    On 12 Jun, 14:05, Richard Heathfield <> wrote:
    > Umesh said:
    >
    > > how to convert a program to a function/macro and put it in a header
    > > file?

    >
    > Don't even try. Instead, find out how to use libraries.


    Now you've done it. If he picks up on this (he may not, as he
    generally wants to be spoonfed a direct answer to his question), he'll
    now ask "how to use libraries?" and we'll have a heap of "off-topic"
    responses...
     
    , Jun 12, 2007
    #3
  4. Umesh

    Umesh Guest

    function -> macro

    How can I convert a function to a macro in general? e.g.

    void factorial(int n)
    {
    long int x=1;
    for (int i=1;i<=n;i++)
    x*=i;
    printf("Factorial of %d = %ld\n",n,x);
    }
     
    Umesh, Jun 12, 2007
    #4
  5. Umesh

    Morris Dovey Guest

    Re: function -> macro

    Umesh wrote:

    | How can I convert a function to a macro in general? e.g.
    |
    | void factorial(int n)
    | {
    | long int x=1;
    | for (int i=1;i<=n;i++)
    | x*=i;
    | printf("Factorial of %d = %ld\n",n,x);
    | }

    Sorry - there is no "general solution". Please re-read Richard's
    article.

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto/
     
    Morris Dovey, Jun 12, 2007
    #5
  6. Re: function -> macro

    Umesh said:

    > How can I convert a function to a macro in general? e.g.
    >
    > void factorial(int n)
    > {
    > long int x=1;
    > for (int i=1;i<=n;i++)
    > x*=i;
    > printf("Factorial of %d = %ld\n",n,x);
    > }


    You'd be better off asking "how can I avoid risking the generation of
    incorrect results?"

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Jun 12, 2007
    #6
  7. Re: function -> macro

    Richard Heathfield <> writes:
    > Umesh said:
    >> How can I convert a function to a macro in general? e.g.
    >>
    >> void factorial(int n)
    >> {
    >> long int x=1;
    >> for (int i=1;i<=n;i++)
    >> x*=i;
    >> printf("Factorial of %d = %ld\n",n,x);
    >> }

    >
    > You'd be better off asking "how can I avoid risking the generation of
    > incorrect results?"


    He'd be *far* better off asking "How do I use this Usenet thing
    without looking like a parasite?".

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 12, 2007
    #7
  8. Umesh

    pete Guest

    Re: function -> macro

    Umesh wrote:
    >
    > How can I convert a function to a macro in general? e.g.
    >
    > void factorial(int n)
    > {
    > long int x=1;
    > for (int i=1;i<=n;i++)
    > x*=i;
    > printf("Factorial of %d = %ld\n",n,x);
    > }


    A function that declares local variables,
    is a poor candidate for conversion to a macro.

    /* BEGIN new.c */

    #include <stdio.h>

    #define factorial(N) \
    do { \
    n = N; \
    x = i = 1; \
    while (n >= i) { \
    x *= i++; \
    } \
    printf("Factorial of %d = %ld\n", n, x); \
    } while (0)

    void (factorial)(int n);

    int main(void)
    {
    long int x = 1, i = 1, n;

    factorial(5);
    (*factorial)(5);
    return 0;
    }

    void (factorial)(int n)
    {
    long int x = 1, i = 1;

    factorial(n);
    }

    /* END new.c */


    --
    pete
     
    pete, Jun 12, 2007
    #8
  9. Umesh

    Umesh Guest

    Re: function -> macro

    // factorial by macro

    #include<stdio.h>
    #define factorial(n) \
    long int x=1; \
    for(int i=1;i<=n;i++) \
    x*=i; \
    printf("Factorial of %d = %ld\n",n,x);\

    main()
    {
    int n;
    printf("Enter No.- ");
    scanf("%d",&n);
    factorial(n);
    return 0;
    }
     
    Umesh, Jun 13, 2007
    #9
  10. Re: function -> macro

    Umesh said:

    > // factorial by macro
    >
    > #include<stdio.h>
    > #define factorial(n) \
    > long int x=1; \
    > for(int i=1;i<=n;i++) \
    > x*=i; \
    > printf("Factorial of %d = %ld\n",n,x);\
    >
    > main()
    > {
    > int n;
    > printf("Enter No.- ");
    > scanf("%d",&n);
    > factorial(n);
    > return 0;
    > }


    foo.c:9: warning: return-type defaults to `int'
    foo.c:9: warning: function declaration isn't a prototype
    foo.c: In function `main':
    foo.c:13: parse error before `long'
    foo.c:13: parse error before `int'
    foo.c:13: `i' undeclared (first use in this function)
    foo.c:13: (Each undeclared identifier is reported only once
    foo.c:13: for each function it appears in.)
    foo.c:13: warning: statement with no effect
    foo.c:13: parse error before `)'
    foo.c:13: `x' undeclared (first use in this function)
    make: *** [foo.o] Error 1

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Jun 13, 2007
    #10
  11. Re: function -> macro

    Richard Heathfield <> writes:
    > Umesh said:
    >
    >> // factorial by macro
    >>
    >> #include<stdio.h>
    >> #define factorial(n) \
    >> long int x=1; \
    >> for(int i=1;i<=n;i++) \
    >> x*=i; \
    >> printf("Factorial of %d = %ld\n",n,x);\
    >>
    >> main()
    >> {
    >> int n;
    >> printf("Enter No.- ");
    >> scanf("%d",&n);
    >> factorial(n);
    >> return 0;
    >> }

    >
    > foo.c:9: warning: return-type defaults to `int'
    > foo.c:9: warning: function declaration isn't a prototype
    > foo.c: In function `main':
    > foo.c:13: parse error before `long'
    > foo.c:13: parse error before `int'

    [snip]

    Umesh is obviously using a compiler that implements some C99 features.
    Using such features isn't wrong, merely non-portable. When I compile
    with "gcc -std=c99 -pedantic -Wall -W -O3", the only diagnostic I get
    is "warning: return type defaults to `int'".

    Of course, the program is horribly bad for other reasons. The use of
    a macro is entirely gratuitous, and the macro unaccountably prints the
    result rather than yielding it as a result.

    Here's another program that uses a macro to compute factorials:

    #include <stdio.h>

    static unsigned long factorial_func(unsigned n)
    {
    unsigned long f = 1;
    unsigned i;
    for (i = 2; i <= n; i ++) {
    f *= i;
    }
    return f;
    }

    #define factorial(n) factorial_func(n)

    int main(void)
    {
    unsigned n;
    printf("Enter number: ");
    scanf("%u", &n);
    printf("factorial(%u) = %lu\n", n, factorial(n));
    return 0;
    }

    The use of a macro is equally gratuitous, but at least this one
    operates more usefully. On a system with 32-bit longs, either program
    works only for arguments up to 12; using unsigned long rather than
    long didn't add enough range to change that. With 64-bit longs, it
    works for arguments up to 20. Neither version checks for overflow
    (which would be fairly difficult to do), but my version uses unsigned
    arithmetic and therefore avoids undefined behavior. The use of scanf
    for input is dangerous, as I've discussed here before, but I was too
    lazy to correct it.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 13, 2007
    #11
  12. Umesh

    Umesh Guest

    Re: function -> macro

    Which compiler do you use?
    It's working fine in TC++ 3/4.5 & VC++ 6
     
    Umesh, Jun 14, 2007
    #12
  13. Re: function -> macro

    Umesh said:

    > Which compiler do you use?


    When writing C programs, I am perfectly content to use *any* compiler
    that supports either ISO/IEC 9899:1990 when invoked in conforming mode
    or ISO/IEC 9899:1999 when invoked in conforming mode.

    > It's working fine in TC++ 3/4.5 & VC++ 6


    Neither of those compilers claims to conform to ISO/IEC 9899:1999. They
    do, however, claim to conform to ISO/IEC 9899:1990, so that's the
    standard by which programs written for them should be judged. By that
    standard, your program is invalid for the reasons stated.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Jun 14, 2007
    #13
  14. Umesh

    Umesh Guest

    Re: function -> macro

    WHICH COMPILER(S) DO YOU USE?

    GCC and other C compilers now support many of the new features of C99.
    However, there has been less support from vendors such as Microsoft
    and Borland that have mainly focused on C++, since C++ provides
    similar functionality improvement.

    GCC, despite its extensive C99 support, is still not a completely
    compliant implementation; several key features are missing or don't
    work correctly

    Richard Heathfield wrote:

    > Umesh said:
    >
    > > Which compiler do you use?

    >
    > When writing C programs, I am perfectly content to use *any* compiler
    > that supports either ISO/IEC 9899:1990 when invoked in conforming mode
    > or ISO/IEC 9899:1999 when invoked in conforming mode.
    >
    > > It's working fine in TC++ 3/4.5 & VC++ 6

    >
    > Neither of those compilers claims to conform to ISO/IEC 9899:1999. They
    > do, however, claim to conform to ISO/IEC 9899:1990, so that's the
    > standard by which programs written for them should be judged. By that
    > standard, your program is invalid for the reasons stated.
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at the above domain, - www.
     
    Umesh, Jun 15, 2007
    #14
  15. Umesh

    Army1987 Guest

    Re: function -> macro

    "Umesh" <> ha scritto nel messaggio
    news:...
    > WHICH COMPILER(S) DO YOU USE?
    >
    > GCC and other C compilers now support many of the new features of C99.
    > However, there has been less support from vendors such as Microsoft
    > and Borland that have mainly focused on C++, since C++ provides
    > similar functionality improvement.
    >
    > GCC, despite its extensive C99 support, is still not a completely
    > compliant implementation; several key features are missing or don't
    > work correctly

    Well, I see that at least you know how to copy and paste.

    >
    > Richard Heathfield wrote:
    >
    >> Umesh said:
    >>
    >> > Which compiler do you use?

    >>
    >> When writing C programs, I am perfectly content to use *any* compiler
    >> that supports either ISO/IEC 9899:1990 when invoked in conforming mode
    >> or ISO/IEC 9899:1999 when invoked in conforming mode.
    >>
    >> > It's working fine in TC++ 3/4.5 & VC++ 6

    >>
    >> Neither of those compilers claims to conform to ISO/IEC 9899:1999. They
    >> do, however, claim to conform to ISO/IEC 9899:1990, so that's the
    >> standard by which programs written for them should be judged. By that
    >> standard, your program is invalid for the reasons stated.
    >>
    >> --
    >> Richard Heathfield
    >> "Usenet is a strange place" - dmr 29/7/1999
    >> http://www.cpax.org.uk
    >> email: rjh at the above domain, - www.

    >
     
    Army1987, Jun 15, 2007
    #15
  16. Umesh

    Army1987 Guest

    Re: function -> macro

    "Keith Thompson" <> ha scritto nel messaggio
    news:...
    > Richard Heathfield <> writes:
    >> Umesh said:
    >>
    >>> // factorial by macro
    >>>
    >>> #include<stdio.h>
    >>> #define factorial(n) \
    >>> long int x=1; \
    >>> for(int i=1;i<=n;i++) \
    >>> x*=i; \
    >>> printf("Factorial of %d = %ld\n",n,x);\
    >>>
    >>> main()
    >>> {
    >>> int n;
    >>> printf("Enter No.- ");
    >>> scanf("%d",&n);
    >>> factorial(n);
    >>> return 0;
    >>> }

    >>
    >> foo.c:9: warning: return-type defaults to `int'
    >> foo.c:9: warning: function declaration isn't a prototype
    >> foo.c: In function `main':
    >> foo.c:13: parse error before `long'
    >> foo.c:13: parse error before `int'

    > [snip]
    >
    > Umesh is obviously using a compiler that implements some C99 features.
    > Using such features isn't wrong, merely non-portable. When I compile
    > with "gcc -std=c99 -pedantic -Wall -W -O3", the only diagnostic I get
    > is "warning: return type defaults to `int'".
    >
    > Of course, the program is horribly bad for other reasons. The use of
    > a macro is entirely gratuitous, and the macro unaccountably prints the
    > result rather than yielding it as a result.
    >
    > Here's another program that uses a macro to compute factorials:
    >
    > #include <stdio.h>
    >
    > static unsigned long factorial_func(unsigned n)
    > {
    > unsigned long f = 1;
    > unsigned i;
    > for (i = 2; i <= n; i ++) {
    > f *= i;
    > }
    > return f;
    > }
    >
    > #define factorial(n) factorial_func(n)
    >
    > int main(void)
    > {
    > unsigned n;
    > printf("Enter number: ");
    > scanf("%u", &n);
    > printf("factorial(%u) = %lu\n", n, factorial(n));
    > return 0;
    > }
    >
    > The use of a macro is equally gratuitous, but at least this one
    > operates more usefully. On a system with 32-bit longs, either program
    > works only for arguments up to 12; using unsigned long rather than
    > long didn't add enough range to change that. With 64-bit longs, it
    > works for arguments up to 20. Neither version checks for overflow
    > (which would be fairly difficult to do)


    #include <limits.h>
    #include <errno.h>
    long factorial(int n)
    {
    long f = 1;
    int i;
    if (n < 0) {
    errno = EDOM;
    return n % 2 ? LONG_MIN : LONG_MAX;
    }
    for (i = 1; i <= n; i++) {
    if (f > LONG_MAX / i) {
    errno = ERANGE;
    return LONG_MAX
    } else
    f *= i;
    }
    errno = 0;
    return f;
    }
    Fairly difficult?
     
    Army1987, Jun 15, 2007
    #16
  17. Re: function -> macro

    On Fri, 15 Jun 2007 17:33:40 +0200, in comp.lang.c , "Army1987"
    <> wrote:

    >
    >"Keith Thompson" <> ha scritto nel messaggio
    >news:...
    >>
    >> The use of a macro is equally gratuitous, but at least this one
    >> operates more usefully.

    ....
    >>Neither version checks for overflow
    >> (which would be fairly difficult to do)

    >

    (snip code)

    >Fairly difficult?


    Remember, you're callling this via a macro.

    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Jun 15, 2007
    #17
  18. Umesh

    CBFalconer Guest

    Re: function -> macro

    Army1987 wrote:
    > "Umesh" <> ha scritto:
    >
    >> WHICH COMPILER(S) DO YOU USE?
    >>
    >> GCC and other C compilers now support many of the new features
    >> of C99. However, there has been less support from vendors such
    >> as Microsoft and Borland that have mainly focused on C++, since
    >> C++ provides similar functionality improvement.
    >>
    >> GCC, despite its extensive C99 support, is still not a completely
    >> compliant implementation; several key features are missing or
    >> don't work correctly

    >
    > Well, I see that at least you know how to copy and paste.


    He also seems to have learnt the horrible habit of top-posting.

    Please do not top-post. Your answer belongs after (or intermixed
    with) the quoted material to which you reply, after snipping all
    irrelevant material. See the following links:

    --
    <http://www.catb.org/~esr/faqs/smart-questions.html>
    <http://www.caliburn.nl/topposting.html>
    <http://www.netmeister.org/news/learn2quote.html>
    <http://cfaj.freeshell.org/google/> (taming google)
    <http://members.fortunecity.com/nnqweb/> (newusers)


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jun 15, 2007
    #18
  19. Re: function -> macro

    Keith Thompson <> writes:

    > Richard Heathfield <> writes:
    >> Umesh said:
    >>
    >>> // factorial by macro
    >>>

    <code and compiler diagnostics snipped>
    > Of course, the program is horribly bad for other reasons. The use of
    > a macro is entirely gratuitous, and the macro unaccountably prints the
    > result rather than yielding it as a result.
    >
    > Here's another program that uses a macro to compute factorials:
    >
    > #include <stdio.h>
    >
    > static unsigned long factorial_func(unsigned n)
    > {
    > unsigned long f = 1;
    > unsigned i;
    > for (i = 2; i <= n; i ++) {
    > f *= i;
    > }
    > return f;
    > }
    >
    > #define factorial(n) factorial_func(n)
    >
    > int main(void)
    > {
    > unsigned n;
    > printf("Enter number: ");
    > scanf("%u", &n);
    > printf("factorial(%u) = %lu\n", n, factorial(n));
    > return 0;
    > }
    >
    > The use of a macro is equally gratuitous, but at least this one
    > operates more usefully. On a system with 32-bit longs, either program
    > works only for arguments up to 12; using unsigned long rather than
    > long didn't add enough range to change that. With 64-bit longs, it
    > works for arguments up to 20. Neither version checks for overflow
    > (which would be fairly difficult to do),


    It has just struck me that these two statements are at odds. If one
    really needed an absolutely cast-iron, overflow-safe factorial
    function, one would just use a look up table. Even in a future C with
    256 bit integers, you'd only need 57 entries. It would have the added
    bonus of being quite fast.

    --
    Ben.
     
    Ben Bacarisse, Jun 15, 2007
    #19
  20. Re: function -> macro

    In article <>,
    Ben Bacarisse <> wrote:

    >If one
    >really needed an absolutely cast-iron, overflow-safe factorial
    >function, one would just use a look up table. Even in a future C with
    >256 bit integers, you'd only need 57 entries. It would have the added
    >bonus of being quite fast.


    Until you build it on a system whose int is 512 bit ;-)
    --
    Programming is what happens while you're busy making other plans.
     
    Walter Roberson, Jun 15, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    4
    Views:
    738
    lilburne
    Nov 2, 2003
  2. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    633
    Jack Klein
    Jul 24, 2004
  3. Replies:
    3
    Views:
    1,348
    Walter Roberson
    May 1, 2006
  4. mlt
    Replies:
    2
    Views:
    892
    Jean-Marc Bourguet
    Jan 31, 2009
  5. alanxx

    Function header files

    alanxx, Feb 16, 2011, in forum: C Programming
    Replies:
    33
    Views:
    1,292
    Ian Collins
    Feb 17, 2011
Loading...

Share This Page