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.
file? is there any shortcut method for this?
thanks.
how to convert a program to a function/macro and put it in a header
file?
Umesh said:
Don't even try. Instead, find out how to use libraries.
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);
}
Richard Heathfield said:Umesh said:
You'd be better off asking "how can I avoid risking the generation of
incorrect results?"
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);
}
// 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;
}
[snip]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'
Which compiler do you use?
It's working fine in TC++ 3/4.5 & VC++ 6
Well, I see that at least you know how to copy and paste.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
Keith Thompson said:[snip]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'
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)
Fairly difficult?
Army1987 said:Well, I see that at least you know how to copy and paste.
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),
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.
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.