Re: caste Q

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 11/04/04


Date: Thu, 4 Nov 2004 10:26:37 -0500 (EST)


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



Relevant Pages

  • Re: How to print out
    ... an int once again but I doubt this will fix it. ... Sorry, but "try a cast, but I doubt that it will fix it" is bad advice. ... tell the compiler, "I know what I'm doing; ... We're missing some context. ...
    (comp.lang.c)
  • Re: Common misconceptions about C (C95)
    ... Cast it to suitable type instead. ... Include the header that declares the prototypes. ... In K&R 1 any function was returned int when nothing else was declared. ... and never ever cast the returnvalue of functions returning some other ...
    (comp.lang.c)
  • Re: Question about (double *)NULL
    ... int umfpack_di_symbolic ... const int Ap, ... Does the cast in NULL accomplish anything? ... If the package was originally written before C had function prototypes, ...
    (comp.lang.c)
  • Re: short res = shortA + shortB result in compiler error. Why?
    ... Cannot implicitly convert type 'int' to 'short'. ... An explicit conversion ... exists (are you missing a cast?) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: short res = shortA + shortB result in compiler error. Why?
    ... Cannot implicitly convert type 'int' to 'short'. ... An explicit conversion ... exists (are you missing a cast?) ...
    (microsoft.public.dotnet.languages.csharp)

Loading