Thread: DataType in C

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    39

    DataType in C

    Hi guys, there's something confusing me about size of data type and not making sense for me which I need more elaboration about this point.
    lets assume I define in my compiler
    short x = 10000000000000000000;

    then if I do printf for printing number x then the number that's shown isn't the same value that I assigned to the variable x and that's because short has limited size, here all fine for me.
    what's confusing me is that how pc prints another value although I assigned deterministic value to x which is
    short x = 10000000000000000000 but it prints something else .. I know size of short is limited but what does that mean and why will affect the value that I'm printing .. I know the value that I assigned is out of the limit, but what does that mean, why PC will not print x as it's although its value out of the size limitation of the dataType.

    thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    what's confusing me is that how pc prints another value although I assigned deterministic value to x
    No, to be deterministic the value must be within the range of the type. If you try to stuff something that is too large to fit into a type then it will "probably" be mangled beyond recognition.

    I assigned is out of the limit, but what does that mean,
    It means that you entered a value too large for the type, so the compiler is free to do anything with the parts that don't fit.

  3. #3
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    49
    Hallo Bryan_Teir°

    I think you should read something about the header limits.h and float.h:
    C Library - <limits.h> - Tutorialspoint

  4. #4
    Registered User
    Join Date
    Nov 2019
    Posts
    39
    Quote Originally Posted by jimblumberg View Post
    No, to be deterministic the value must be within the range of the type. If you try to stuff something that is too large to fit into a type then it will "probably" be mangled beyond recognition.


    It means that you entered a value too large for the type, so the compiler is free to do anything with the parts that don't fit.
    So the compiler is doing that? and not the PC.

    if so, then who's made/programmed the compiler sounds that he done something stupid because if it's out of the limit why should the compiler be free to do anything with the parts that don't fit ..
    but whatever as a programmer must follow the compiler instructions.

    so the PC is reading what the compiler tells him, so if the value was out of the range, so the compiler tells the PC other value -not the assigned value- and then the PC will print the given value that COMPILER has sent/provide , so lets assume the COMPILER gives him the same assigned value although it's out of the range, then the PC was giving me the same assigned value...


    Could you provide me a link that explains the interface between my code to compiler to PC'S HARDWARE -PC- ? thanks alot

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    677
    Did you paid attention to the compiler WARNINGS?

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,876
    Could you provide me a link that explains the interface between my code to compiler to PC'S HARDWARE -PC- ? thanks alot
    Instead of studying the exact implications of doing something incorrectly, my advice would be to focus your energy into ways to avoid doing it in the first place.

    Assigning a literal too large to a variable should be an easy one to avoid: Either your variable size is too small, or what you are trying to put into it is too large.

    A good suggestion came from flp1969 - Make sure your warnings are turned on and you take time to read and understand them
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    945
    Although this is so-called "undefined behaviour" and therefore cannot be relied on to have the same behaviour between compilers or even between different versions of the same compiler, in the code below what seems to be happening is the following.

    Consider an odometer with 3 wheels. If you start it at all zeroes and move it forward 1050 times, the value will be 50. The highest representable number is 999 at which point it "wraps around" to 000 again. We can calculate the final value by taking the input mod the max value + 1, i.e., 1050 % 1000 = 50.

    That's part of what's going on, except the wheels are binary and not decimal. The other part is that the resulting bit pattern is interpretted as a signed short integer, which may cause it to be negative (see Two's complement - Wikipedia ).

    BTW, you should get a warning about the too-large value when you compile the program. Don't ignore warnings.
    Code:
    #include <stdio.h>
    #include <limits.h>
     
    int main()
    {
        short s1 = 100000000;
        printf("%d\n", s1);  // -7936
     
        // set unsigned short to value mod ushort max + 1
        unsigned short u = 100000000 % (USHRT_MAX + 1);
     
        // reinterpret bit pattern as signed short
        short s2 = *(short*)&u;
     
        printf("%d\n", s2);  // -7936
     
        return 0;
    }
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,063
    > short x = 10000000000000000000
    It be like Brian_Teir goes to a bar with a thimble, asks the barkeep for a pint of his finest, and be like "where's all my beer!?" when only a thimble full comes back.

    That's overflow for you.
    It might have existed at some point, but it's now on the floor.

    Or in the case of numeric overflow on a computer, the electrons are doomed to wander the ground plane for the rest of eternity. May they rest in peace, we will remember their valiant effort to record such a large number, however futile the attempt might have been.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,876
    Quote Originally Posted by salem View Post
    > short x = 10000000000000000000
    it be like brian_teir goes to a bar with a thimble, asks the barkeep for a pint of his finest, and be like "where's all my beer!?" when only a thimble full comes back.

    That's overflow for you.
    It might have existed at some point, but it's now on the floor.

    Or in the case of numeric overflow on a computer, the electrons are doomed to wander the ground plane for the rest of eternity. May they rest in peace, we will remember their valiant effort to record such a large number, however futile the attempt might have been.
    lmao!
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 09-23-2012, 03:06 PM
  2. Test datatype
    By tsmith94 in forum C Programming
    Replies: 2
    Last Post: 11-27-2011, 08:56 PM
  3. datatype bit?
    By MK27 in forum C Programming
    Replies: 3
    Last Post: 02-05-2009, 01:56 PM
  4. Datatype
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 08-04-2008, 12:53 PM
  5. Datatype Conversion
    By carrotcake1029 in forum C Programming
    Replies: 12
    Last Post: 04-15-2008, 11:58 PM

Tags for this Thread