Thread: a question about signed and unsigned variables

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    18

    a question about signed and unsigned variables

    for example both short and unsigend short take 2 bytes of memory and from 0 to 65535 or from -32768 to 32767. so if both types take up 2 bytes of memory, how does the computer realize one is signed and the other is unsigend?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    how does the computer realize one is signed and the other is unsigend?
    You, the programmer, told the compiler to use the proper type for the variables in question.

    Jim

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    To illustrate @jimblumberg's response please see this demo program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       short svar = -1234;         // short signed integer
       unsigned short uvar = 10; // unsigned short integer
    
       printf("The sizeof a signed short is %zu, and the value is %d\n", sizeof(svar), svar);
       printf("The sizeof an unsigned short is %zu, and the value is %d\n", sizeof(uvar), uvar);
    
       return 0;
    }
    I don't know your knowledge or experience with the C Programming Language, but perhaps you should read my response to a previous posting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about signed and unsigned ints in c++
    By asilvester635 in forum C++ Programming
    Replies: 7
    Last Post: 01-31-2017, 01:47 PM
  2. Question on unsigned vs signed comparisons
    By CodeSlapper in forum C Programming
    Replies: 6
    Last Post: 03-08-2016, 06:24 PM
  3. Replies: 3
    Last Post: 05-24-2015, 12:24 PM
  4. Why can't 'float' variables be assigned to signed or unsigned?
    By cplusplusnoob in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2012, 12:02 PM
  5. Signed and unsigned
    By ncode in forum C Programming
    Replies: 3
    Last Post: 09-02-2011, 08:59 PM

Tags for this Thread