Function & header files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Umesh

    Function & header files

    how to convert a program to a function/macro and put it in a header
    file? is there any shortcut method for this?
    thanks.

  • Richard Heathfield

    #2
    Re: Function & header files

    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

    email: rjh at the above domain, - www.

    Comment

    • mark_bluemel@pobox.com

      #3
      Re: Function & header files

      On 12 Jun, 14:05, Richard Heathfield <r...@see.sig.i nvalidwrote:
      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...

      Comment

      • Umesh

        #4
        function -&gt; 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("Factori al of %d = %ld\n",n,x);
        }

        Comment

        • Morris Dovey

          #5
          Re: function -&gt; 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("Factori al 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



          Comment

          • Richard Heathfield

            #6
            Re: function -&gt; 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("Factori al 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

            email: rjh at the above domain, - www.

            Comment

            • Keith Thompson

              #7
              Re: function -&gt; macro

              Richard Heathfield <rjh@see.sig.in validwrites:
              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("Factori al 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_Keit h) kst-u@mib.org <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"

              Comment

              • pete

                #8
                Re: function -&gt; 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("Factori al 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("Factori al 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

                Comment

                • Umesh

                  #9
                  Re: function -&gt; macro

                  // factorial by macro

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

                  main()
                  {
                  int n;
                  printf("Enter No.- ");
                  scanf("%d",&n);
                  factorial(n);
                  return 0;
                  }

                  Comment

                  • Richard Heathfield

                    #10
                    Re: function -&gt; 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("Factori al 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

                    email: rjh at the above domain, - www.

                    Comment

                    • Keith Thompson

                      #11
                      Re: function -&gt; macro

                      Richard Heathfield <rjh@see.sig.in validwrites:
                      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("Factor ial of %d = %ld\n",n,x);\
                      >>
                      >main()
                      >{
                      >int n;
                      >printf("Ente r 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("factori al(%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_Keit h) kst-u@mib.org <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"

                      Comment

                      • Umesh

                        #12
                        Re: function -&gt; macro

                        Which compiler do you use?
                        It's working fine in TC++ 3/4.5 & VC++ 6

                        Comment

                        • Richard Heathfield

                          #13
                          Re: function -&gt; 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

                          email: rjh at the above domain, - www.

                          Comment

                          • Umesh

                            #14
                            Re: function -&gt; 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

                            email: rjh at the above domain, - www.

                            Comment

                            • Army1987

                              #15
                              Re: function -&gt; macro


                              "Umesh" <fraternitydisp osal@gmail.comh a scritto nel messaggio
                              news:1181912917 .184583.62090@z 28g2000prd.goog legroups.com...
                              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.
                              >

                              Comment

                              Working...