473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

caste Q

#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
Nov 14 '05 #1
4 1669

On Thu, 4 Nov 2004, Merrill & Michele wrote:

#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'.
#include <time.h>
srand(time(0));
rnd=rand();
widernumber=(long)rnd;
Lose the spurious cast.
printf=("wider number is %ld\n",widernumber)
Missing semicolon, bogus '=' sign after 'printf'.
return 0
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
Nov 14 '05 #2
Merrill & Michele wrote:
#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.
return 0
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.

--
Er*********@sun.com

Nov 14 '05 #3
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.

--
Rich Gibbs
rg****@alumni.princeton.edu

Nov 14 '05 #4
rnd=rand();
widernumber=(long)rnd;


The cast is superfluous.

Thanks all for replies. MPJ
-----
Casting: only when bone is broken.
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

188
by: Ilias Lazaridis | last post by:
I'm a newcomer to python: - E01: The Java Failure - May Python Helps? http://groups-beta.google.com/group/comp.lang.python/msg/75f0c5c35374f553 - I've download (as suggested) the python...
15
by: F. Da Costa | last post by:
Hi all, Following two sniperts of code I'm using and getting very interesting results from. ..html <tr id="1" class="segment" open="false"> This is the segment under 'investigation' ..js
10
by: shiva | last post by:
Inorder to handle large files over 100 MB, is there a way in dotnet to open only certain bytes of the file by buffer ? It is shaky if the entire file is opened and splitted. Instead is there a...
1
by: andrej | last post by:
hi, ich habe eine anwendung, welche ein xml document erstellt. um festzustellen, ob ein element bereits vorhanden ist, verwende ich die funktion selectsinglenode( ....) diese funktion...
4
by: ranjeet.gupta | last post by:
Dear All Please check the below code: UINT8 MsgLength = 0; MsgLength = strlen((char *)msg);
1
by: pchadha20 | last post by:
How to single numeric values from a field in a table which has multiple numeric value in a field of a table.And that table contains thousands of records. Suppose we have a table customer having...
1
by: rhepsi | last post by:
HII all, im working on postgresql database where i want to copy the data from one table to other table.... when im trying to write the sql query.. ERROR: 42601: syntax error at or near...
105
by: Keith Thompson | last post by:
pereges <Broli00@gmail.comwrites: These types already have perfectly good names already. Why give them new ones? If you must rename them for some reason, use typedefs, not macros. --
2
by: write2ashokkumar | last post by:
Hi, i have the table like this, +------+-------+-----+ | city | caste | cnt | +------+-------+-----+ | 1 | 1 | 2 | | 1 | 2 | 3 | | 2 | 1 | 1 |
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.