Function & header files

K

Keith Thompson

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
[...]

The above two paragraphs are an uncredited direct quote from a
Wikipedia article,
<http://en.wikipedia.org/wiki/C_(programming_language)>.

There's nothing wrong with quoting from outside sources, and the
quoted material happens to be correct, but failing to provide
attribution is rude and dishonest.
 
K

Keith Thompson

Umesh said:
LOOKS FINE BUT IF FAILS TO WORK FOR n>12
WHAT ABOUT USING LINKED LISTS?
[...]

Don't shout. (Writing in all-caps represents shouting.)

Don't top-post. We've explained this to you numerous times; you've
shown no evidence so far of being intelligent enough to understand it,
but we're prepared to be pleasantly surprised.

Be less obnoxious.
 
K

Keith Thompson

Army1987 said:
"Keith Thompson" <[email protected]> ha scritto nel messaggio
Here's another program that uses a macro to compute factorials:
[code snipped]

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?

Well, sufficiently difficult than I was unwilling to take the time to
do it.
 
K

Keith Thompson

Until you build it on a system whose int is 512 bit ;-)

If you really need to compute factorials, chances are you'll need them
for arguments that won't fit in any integer type, even if you have
512-bit integers.
 
A

Army1987

Mark McIntyre said:
(snip code)


Remember, you're callling this via a macro.
#include <errno.h>
#define factorial(n) \
do { \
int n_ = n; \
long f = 1; \
int i; \
errno = 0; \
if (n_ < 0) \
printf("%d is negative, so it has no factorial\n", n_);\
else { \
for (i = 1; i <= n_; i++) { \
if (f > LONG_MAX / i) { \
printf("%d is too large for its factorial "\
"to be computed\n", n_);\
errno = ERANGE; \
break; \
} \
else \
f *= i; \
} \
if (errno != ERANGE) \
printf("Factorial of %d is %ld\n", n_, f);\
} \
} while (0) /*I'm not saying that it doesn't suck...*/
 
A

Army1987

Walter Roberson said:
Until you build it on a system whose int is 512 bit ;-)

#include <limits.h>
#include <errno.h>
long factorial(int n)
{
static long table[13] = { 1, 1, 2, 6, 24, 120, 720, 5040,
40320L, 362880L, 3628800L, 39916800L, 479001600L };
int i;
long f = table[12];
if (n < 0) {
errno = EDOM;
return n % 2 ? LONG_MIN : LONG_MAX;
}
if (n < 13) {
errno = 0;
return table[n];
}
for (i = 13; i <= n; i++) {
if (f > LONG_MAX / i) {
errno = ERANGE;
return LONG_MAX;
} else
f *= i;
}
errno = 0;
return f;
} /*rewrite this eliminating multiple returns if you feel like it*/
 
A

Army1987

Keith Thompson said:
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
[...]

The above two paragraphs are an uncredited direct quote from a
Wikipedia article,
<http://en.wikipedia.org/wiki/C_(programming_language)>.

There's nothing wrong with quoting from outside sources, and the
quoted material happens to be correct,
and it happens to be relevant to the point, and the poster happens
to understand why it is and what it means...
 
K

Keith Thompson

Keith Thompson said:
If you really need to compute factorials, chances are you'll need them
for arguments that won't fit in any integer type, even if you have
512-bit integers.

Sorry, I meant that the result, not the argumement, won't necessarily
fit in any integer type.
 
N

Net Nanny

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)

You seem to have learnt the horrible habit of posting 9-line double
signatures.

Please do not post 9-line double signatures. Your signature should be at
most 4 lines long.
 
M

Mark McIntyre

#include <errno.h>
#define factorial(n) \

(snip macro that could suck bowling balls through a drinking straw)
} while (0) /*I'm not saying that it doesn't suck...*/

*phew*
--
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
 
A

Army1987

Mark McIntyre said:
(snip macro that could suck bowling balls through a drinking straw)


*phew*

Here is why I don't usually ever use the do { } while (0) trick.
If I cannot write a macro that expands to an expression, not even
abusing commas, &&s, ||s, ? :s and parentheses, then I write a
function. (And if I cannot write a macro that does not evalue each
argument exactly once, where each argument and the whole expression
is surrounded by as many parentheses as possibly ever needed, which
does not try to assign to or use the address of arguments etc.,
then I don't use lowercase in its name.)
Here I wanted to show that writing a macro that writes the
factorial of a number to stdout (other than
#define PRINT_FACT(n) (printf("Factorial of %d is %ld," (n),\
factorial(n)))
) albeit possible, isn't worth the trouble.
 
O

osmium

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

Umesh, if you want to hold a conversation with someone via Usenet instead of
E-mail, you and your correspondent should both post to
alt.anonymous.messages, that's what the group is *for*. Doing so will avoid
the need for the rest of us to be subjected to the fragmentary, disorganized
thoughts you have from time to time.

Do you understand that "you" refers to one particular person, and not a
group of people collectively? Do you realize that you have not identified
that person?
 
R

Richard Harter

Incorrect. "You" may be a single person, or it may be a group of
people being addressed collectively, or it may be a generic "somebody"
(though I believe that "one" is the english-pedant-correct word to use
for that last one).

In formal usage you should use "one" but in informal usage one should use
"you".
 
D

Dave Vandervies

osmium said:
Do you understand that "you" refers to one particular person, and not a
group of people collectively?

Incorrect. "You" may be a single person, or it may be a group of
people being addressed collectively, or it may be a generic "somebody"
(though I believe that "one" is the english-pedant-correct word to use
for that last one).


dave
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top