0

I am a bit confused about the difference between size_t and unsigned long long. Is there any difference between them at all?

Also when printing or scanning a size_t object for input/output, will it be appropriate to use %llu (unsigned long long's format specifier)?

  • 2
    `size_t` tends to change based on the architecture you're compiling for. – ChrisMM Jun 18 '20 at 10:00
  • 5
    Yes, there is a difference. `unsigned long long` is a primitive type and `size_t` is a typedef to a type, usually to `unsigned long long`. Other systems can typedef it differently. – Thomas Sablik Jun 18 '20 at 10:00
  • 3
    dupe of [Where do I find the definition of size\_t?](https://stackoverflow.com/questions/1119370/where-do-i-find-the-definition-of-size-t) or [Is size_t guaranteed to be an alias type to one of integer types?](https://stackoverflow.com/questions/23749822/is-size-t-guaranteed-to-be-an-alias-type-to-one-of-integer-types) or etc. – underscore_d Jun 18 '20 at 10:01
  • 4
    Both the [`scanf`](https://en.cppreference.com/w/c/io/fscanf) and [`printf`](https://en.cppreference.com/w/c/io/fprintf) have the `z` prefix for `size_t`. So to properly scan for or print a `size_t` value use e.g. `%zu`. – Some programmer dude Jun 18 '20 at 10:02
  • 3
    `size_t` is an implementation-defined type that's large enough to hold the size of any object. End of story. It's **NOT** the same thing as `unsigned long long`. `size_t` is `size_t` and the correct format specifier is `%zu`. – Andrew Henle Jun 18 '20 at 10:02
  • The definition of `size_t` is platform dependent. On a 64 bit platform it's usually `unsigned long long`, on a 32 bit platform it's usually `unsigned long`. – Jabberwocky Jun 18 '20 at 10:02
  • 2
    Or in the case of C++ don't use either `scanf` nor `printf`. Instead use `std::cin` and `std::cout` with the standard input and output operator `>>` and `<<`, and the right thing will be done without having to worry about format specifiers. – Some programmer dude Jun 18 '20 at 10:03
  • Please one question per question – 463035818_is_not_an_ai Jun 18 '20 at 10:06
  • On my machine, `std::size_t` is smaller than `unsigned long long`. `%llu` is inappropriate for `std::size_t`, rather use `%zu`. – Eljay Jun 18 '20 at 12:59

0 Answers0