Thread: What is happening here please explain

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    10

    What is happening here please explain

    signed int x,y,z;


    x = (int) (((long)y * z) /100);
    /* if i gave y = 8000 and z = 1100 iam getting x as -1 instead of 22464*/
    Last edited by jayee_spicyguy; 09-23-2008 at 01:56 AM. Reason: mis spell

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    if i gave y = 8000 and z = 1100 iam getting x as -1 instead of 22464
    How in the world can you get 22464 in the first place? Mathematically, the result should be 88000.

    Oh wait, you are expecting an overflow, and sizeof(int) is 2 on your machine?

    EDIT:
    Right, here's the relevant text from C99:
    Quote Originally Posted by C99 section 6.3.1.3
    When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

    Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

    Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.
    Now, 88000 is always in the range of a long. It may not be in the range of an int, since int is only guaranteed a maximum value of at least 32767. Therefore the third paragraph holds, the result is implementation defined, and -1 is what your implementation defined.
    Last edited by laserlight; 09-23-2008 at 02:05 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Here is what I tried in a 32bit CPU:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	signed short x,y,z;
    	y = 8000; z=1100;
    	x = (short)(((int)y * z) / 100);
    	printf("&#37;d", x);
    	return 0;
    }
    Compiled with: gcc -std=gnu99 and without -std=gnu99 and got:
    Output: 22464

    That is expected though, since I use short and what the text from C99 refers to is only for int.

    Lets see what happens when we remove the casting. If you remove the (short) you get the same result, since gcc doesn't even yield a warning. If you remove the (int) then again you get the same result. That makes me assume that the x*y calculation is saved into a 32bit register. So it holds the correct value. If it was hold in a 16bit register (or half register) then you would get a different result.
    That is why I believe when it is bigger than an int the value is defined somehow. Because the overflow would not make any sense and it could be more or less undefined. If it not an int though, as in my example, I can guess, as you did, the result. And that makes sense and might be useful in a program, since the overflow can be helpful sometimes.

    EDIT: What I forgot to highlight is that the CPU will probably use specific registers for the calculations, which might be a of a certain size no matter the variables type/size
    Last edited by C_ntua; 09-23-2008 at 02:32 AM.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    10
    Thanks alot you seems to be an encyclopedia to me. What I have to do if I want be like you my master.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    10
    Thanks alot

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    From what I understand, all integers are considered as int, possible larger data types if the number does not fit inside an int.
    However, the compiler must not choose a data type that can hold the result of the calculation if the calculation can not be done in compile time.
    Calculations are done with both sides being the same size and the result also being of the same type that the types of the calculation is done with (ie if you are using int, the result is int, and if you use long, the result is long).
    If the calculation does not fit inside the type of the result, you get overflow. To fix that, you must make sure at least one operand is of a bigger type to force the result to use a bigger type.

    Correct me if I'm wrong somewhere here. I don't know if this is the same in C++ and C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Could someone explain this program?
    By Beachblue in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:51 AM
  2. Explain me what is happening
    By capvirgo in forum C Programming
    Replies: 2
    Last Post: 02-18-2008, 08:26 AM
  3. Please explain?
    By neo_phyte in forum C Programming
    Replies: 3
    Last Post: 08-25-2006, 05:23 AM
  4. explain this loop statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-05-2002, 02:46 AM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM

Tags for this Thread