Thread: Question with size_t and ssize_t

  1. #1
    Registered User
    Join Date
    Mar 2021
    Posts
    3

    Question with size_t and ssize_t

    Hello,

    I started coding recently and I don't understand very well the size_t and ssize_t data types. I know that size_t is the same as ssize_t, but the latter is signed.

    I've recently seen these lines:


    Code:
    const ssize_t a = -1;
    const size_t b = (size_t) a;


    When I use the function printf() with a and b they seem to print the same number, but I don't understand why. To me, they seem to do the same thing as the int data type, but I think I'm missing something. Moreover, how does (size_t) affect the value of a in "size_t b = (size_t) a"?

    I don't understand that part either :/



  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    Quote Originally Posted by Dante404
    When I use the function printf() with a and b they seem to print the same number, but I don't understand why. To me, they seem to do the same thing as the int data type, but I think I'm missing something.
    These kinds of claims are usually best made with some code to demonstrate what you tried and the output you received. For example, I compiled and ran:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        const ssize_t a = -1;
        const size_t b = (size_t) a;
        printf("ssize_t: %ld\nsize_t: %zu\n", a, b);
        return 0;
    }
    The output was:
    Code:
    ssize_t: -1
    size_t: 18446744073709551615
    which of course contradicts your claim that "they seem to print the same number".

    Quote Originally Posted by Dante404
    Moreover, how does (size_t) affect the value of a in "size_t b = (size_t) a"?
    It performs a cast, which is not absolutely necessary since there is an implicit conversion from signed integer types to unsigned integer types, but is still good to highlight that such a conversion is happening.
    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
    Join Date
    Dec 2017
    Posts
    1,127
    printf requires a format spec. Which did you use? The proper format for size_t is %zu. The proper one for ssize_t is %zd.
    When you assign -1 to an unsigned value it's interpreted as the maximum value (kind of like starting an odometer at all 0's and turning it back one click).
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  4. #4
    Registered User
    Join Date
    Mar 2021
    Posts
    3
    Thank you very much for the answers. As I suspected I was missing something: I wasn't using the format specifier %zu, because I didn't know it existed, so my results were different. Although I got the correct results for "a", it's good to know I should have also used %zd for ssize_t

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size_t
    By KemalT in forum C Programming
    Replies: 5
    Last Post: 02-09-2015, 04:02 PM
  2. Replies: 3
    Last Post: 02-11-2013, 11:27 AM
  3. ssize_t sendfile - error
    By ilans11il in forum C Programming
    Replies: 1
    Last Post: 02-11-2013, 07:45 AM
  4. size_t question
    By csisz3r in forum C Programming
    Replies: 14
    Last Post: 09-17-2005, 12:20 PM
  5. size_t
    By falconetti in forum C Programming
    Replies: 2
    Last Post: 02-25-2002, 08:52 AM

Tags for this Thread