Function & header files

U

Umesh

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

mark_bluemel

Umesh said:


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...
 
U

Umesh

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

Morris Dovey

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.
 
R

Richard Heathfield

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?"
 
K

Keith Thompson

Richard Heathfield said:
Umesh said:

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?".
 
P

pete

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

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 */
 
U

Umesh

// 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;
}
 
R

Richard Heathfield

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
 
K

Keith Thompson

Richard Heathfield said:
Umesh said:


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.
 
R

Richard Heathfield

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.
 
U

Umesh

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
 
A

Army1987

Umesh said:
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.
 
A

Army1987

Keith Thompson said:
Richard Heathfield said:
Umesh said:


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?
 
M

Mark McIntyre

(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
 
C

CBFalconer

Army1987 said:
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)
 
B

Ben Bacarisse

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.
 
W

Walter Roberson

Ben Bacarisse said:
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 ;-)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top