caste Q

  • Thread starter Merrill & Michele
  • Start date
M

Merrill & Michele

#include <stdio.h>
#include <stdlib.h>

int main(){

int rnd;
long widernumber;

srand(time));

rnd=rand();
widernumber=(long)rnd;

printf=("wider number is %ld\n",widernumber)
return 0
}
Q1) Just so that I don't get ahead of myself, does the above code cast what
is necessarily an integer as a long?

Q2) What do the style people think about casts in general? MPJ
 
A

Arthur J. O'Dwyer

#include <stdio.h>
#include <stdlib.h>

int main(){

int main(void) is better style, IMNSHO. Provide prototypes for your
functions in C whenever possible. (In C++, I'd use the empty-parens
style, but C is not C++.)
int rnd;
long widernumber;

srand(time));

Missing (NULL, and IIRC also a missing prototype for 'time'.
rnd=rand();
widernumber=(long)rnd;

Lose the spurious cast.
printf=("wider number is %ld\n",widernumber)

Missing semicolon, bogus '=' sign after 'printf'.

Missing semicolon.
}
Q1) Just so that I don't get ahead of myself, does the above code cast what
is necessarily an integer as a long?

No, it's full of mistakes and outright errors. Learn to type correctly
before moving on to actual programming; it'll save you lots of grief in
the long run.
Q2) What do the style people think about casts in general? MPJ

Casts are evil. There are maybe 3 or 4 places to use casts in C, and
none of them ought to appear in your average C program.

-Arthur
 
E

Eric Sosman

Merrill said:
#include <stdio.h>
#include <stdlib.h>

int main(){

int rnd;
long widernumber;

srand(time));

No definition of `time', and too many parentheses.
rnd=rand();
widernumber=(long)rnd;

printf=("wider number is %ld\n",widernumber)

Missing a semicolon.

Missing a semicolon.
}
Q1) Just so that I don't get ahead of myself, does the above code cast what
is necessarily an integer as a long?

After corrections, yes: `rnd' is an `int', and the cast
converts it to a `long'. The assignment would have converted
it anyhow, so the cast is not required.
Q2) What do the style people think about casts in general? MPJ

There are a few situations in which casts are necessary
or at the very least useful. However, they are greatly over-
used, and as a practical matter I tend to view each cast as
"guilty until proven innocent." For example, the cast in
your example is entirely unnecessary, and I take this as
evidence that you don't know what you're doing (which is all
right; that's why you're asking questions, after all).

"GUPI" also applies even when the cast is required,
because it suggests that the programmer is (ab)using C as a
kind of high-level assembly language, writing code that deals
with the representations of things rather than with their
values. Such code can (and frequently does) work as intended,
but tends to be non-portable: When the programmer "knows"
something the language doesn't promise, writes code that
exploits that "knowledge," and uses casts to get the compiler
to accept it, the code will misbehave if it's ever moved to
a system where the "knowledge" turns out to be false.

Casts are in a class with global variables: Sometimes
necessary, sometimes expedient, but always to be viewed
with suspicion.
 
R

Rich Gibbs

Merrill & Michele said the following, on 11/04/04 09:43:
#include <stdio.h>
#include <stdlib.h>

int main(){

int rnd;
long widernumber;

srand(time));

If what you intend here is to invoke the time() function, then the above
line should be:
srand( time(NULL) );
and you need to #include <time.h>. (The function takes an argument of
type "pointer to time_t"; it stores the time in the pointed-to location
as well as returning it, if the argument is not NULL.)

rnd=rand();
widernumber=(long)rnd;

The cast is superfluous.
printf=("wider number is %ld\n",widernumber)
return 0

Both these statements need terminating ';'s , and the '=' in the first
is an error.
}
Q1) Just so that I don't get ahead of myself, does the above code cast what
is necessarily an integer as a long?

Yes, and that conversion will be done with or without the explicit cast.
Q2) What do the style people think about casts in general? MPJ

I think casts tend to be used unnecessarily, or incorrectly, more often
than they are used appropriately. One way of looking at a cast is as a
message to the compiler along the lines of, "Don't worry, I know what
I'm doing." In practice, this often reflects the programmer relying on
some property of a particular platform or implementation that is _not_
part of the standard. What happens when the program is tried on a
different platform, or with a different compiler, is left as an unhappy
surprise for some other hapless person. (It's not nice to fool the
compiler.)

I tend to regard casts in general with a jaundiced eye, and put them in
the same mental category as global variables, or 'goto's. There are a
few situations where they are needed, or at least convenient, but they
are more often than not misused.
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top