What is the difference between these expressions?

Asked

Viewed 93 times

3

In the srand’s handbook man srand says srand has as a parameter a unsigned int, but when using without cast the compiler does not complain. It has some possibility of going wrong if not using with cast (unsigned int) ? If yes, what possible errors ?

An example, comparing the two lines of code below:

srand(time(NULL));
srand((unsigned int)time(NULL));
  • 1

    Compile with Warning connected. Warning It’s an error that allows you to compile but it’s still a mistake. It is an error that you don’t have to worry about in any compilation, but when you finish the program it is an error that cannot exist.

  • @Bigown I always compile using gcc -Wall -pedantic -std=c99 -o saida entrada.c and never leave my codes finished with Warning, except in very extreme cases. But in this case, the compiler did not accuse any Warning, that’s what I meant in "...but when using without cast the compiler does not complain...", complains, in case, it would be Warning, sorry not to have explained this.

2 answers

2


He’s doing the cast implicitly, which has no problems in some cases.

For example:

int x = 3;
double y = 4.5;
x = x + y;

What happens in these cases and the following:

1. Na soma de `x` com `y`, `x` e promovido para `double` e a soma acontece como se ambos fossem desse tipo;
2. O resultado da soma, 7.5, e convertido de volta para `int`, ja que `x` e do tipo inteiro.

Usually, the compiler does these operations implicitly. You can also do them explicitly, for example:

int x = 65;
printf("%c\n", (char)x);

Now we play ourselves x as char to print, then its value A.

There are, however, cases where these implicit conversions go wrong, mainly with pointers.

  • But what if instead of time(NULL) I had a variable like int that had in its content a negative value ?

  • He would simply interpret the value as positive. remember that in the PC there are no negative values, it is just another interpretation of the binary value.

  • For it to interpret as positive values, it will use which conversion method ? It would complement the 2 or something else specific ?

  • Well, I hope it fits here. A number on the computer always has a binary representation, right? Let’s assume that our computer has a representation with 8 bits only, ie 8 0 or 1. For example, the number 7 would be: 00000111. To represent the negatives we use a maracutaia: the leftmost bit, in this case the 8th, will be 1 if the number is negative. Soon, -7 and 10000111. I hope I’ve made myself clear.

  • So if it is 8 bits to represent the integers, would it go from -128 to 127 ? When the complement to 2 is used ?

  • that, perfect. the complement of 2 of a number is like his negative.

Show 1 more comment

1

When you add the header that contains the function declaration srand() (the header stdlib.h) compiler knows what type of parameter.

Then it converts the type you used to the required type, if possible. If it is not possible give a message.

#include <stdlib.h>
int main(void) {
    srand(42); // ok
    srand(4.2); // ok, conversao de 4.2 para 4
    srand(srand); // NOT ok! nao é possivel converter pointers para unsigned
}

If you remove the #include are already all bad although the compiler does not complain

Browser other questions tagged

You are not signed in. Login or sign up in order to post.