There is a code:

//buf.st_size типа off_t и равен 2147583648 //countProcess типа int и равен 4 //len типа unsigned long long len = buf.st_size / countProcess; printf("len: %llu\n", len); 

As a result of the division should be 536895912, but instead it turns out some sort of garbage. If instead of buf.st_size substitute 2147583648 then everything is divided normally. Please tell me what is wrong?

    1 answer 1

    Elementary Watson.

    Your value does not fit into a 32-bit signed number , that's all ... And when you specify it explicitly, it is taken as a perfectly fitting unsigned value, and it is normally divided.

    If you write the division as

     unsigned long long len = (unsigned long)st_size / countProcess; 

    you will succeed...

    Your number turns into a negative value of the long type, is divided, and the negative value is obtained, which then, when converted to an unsigned long long expands all 64 bits, and then this number is considered as unsigned. Here is what it looks like in hexadecimal:

    800186A0 after the sign division by 4 turns into E00061A8 , and after the sign extension up to 64 bits - into FFFFFFFFE00061A8 .

    • And so I knew that the mistake was stupid ... I understood everything, everything works. Thank you very much) - WenSiL