size_t Type | C Programming Tutorial

Поделиться
HTML-код
  • Опубликовано: 9 фев 2025
  • An explanation of the size_t type in C that is used by many standard library functions in C. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!

Комментарии • 31

  • @mohammadahmedragab837
    @mohammadahmedragab837 2 года назад +6

    you are the best really C videos tutorial 💙❤️❤️❤️. I love your channel from Egypt

  • @chrisray9653
    @chrisray9653 2 года назад +2

    Thanks for the explanation, you see this in a lot of production code but hardly ever mentioned in C books.

  • @Moon-hh1sh
    @Moon-hh1sh 5 месяцев назад +1

    thank you so much for this tutorial, you are the best

  • @D_Ladybug
    @D_Ladybug 2 года назад +2

    Weldone
    Appreciate you're efforts.
    Can you please do video on using malloc to allocate memory to a two dimensional array?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      Thanks Olamide! That's definitely a topic that's on my "todo list". 🙂

  • @Andrii87
    @Andrii87 2 месяца назад

    TLDR: t_size is just unsigned int, which is 4 bytes long.

  • @michaelbenard6758
    @michaelbenard6758 Год назад

    I have seen alot of your videos and are helpful,
    I have a little problem how would I get computer serial Number.
    system("wmic get bios serialnumber>sn.txt");
    And store it in an array of character and then print it on screen.?
    I really need help with that.

  • @MadpolygonDEV
    @MadpolygonDEV 2 года назад +1

    What stops me from using unsigned int instead of size_t?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +2

      unsigned int is not necessarily equivalent to size_t... size_t is guaranteed to be at least 16 bits wide, but in practice it is often much, much larger as it is the largest unsigned integer value that can be stored. Even unsigned long may not be equivalent to the range of values that size_t can store. :-)

    • @MadpolygonDEV
      @MadpolygonDEV 2 года назад

      @@PortfolioCourses I understood from the replies about, so basically size_t will adopt the size of the most possible value and is therefore equivalent of the INT_MAX macro, hope its correct. Thanks for replying.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +2

      You're welcome! :-) INT_MAX is the maximum value of an int. SIZE_MAX is the maximum value of size_t.

    • @MadpolygonDEV
      @MadpolygonDEV 2 года назад

      @@PortfolioCourses oops, sorry yea meant that :) Thanks for the swift responses.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      @@MadpolygonDEV No problem! 🙂

  • @davidhayward1426
    @davidhayward1426 Год назад

    You might want to do a follow up on what is the signed equivalent of size_t. (Hint: it is not off_t)

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      Is there a universal equivalent, and if so what is it? long long int? ssize_t is POSIX so it's not truly universal.

    • @davidhayward1426
      @davidhayward1426 Год назад

      @@PortfolioCourses I’d suggest prtdiff_t.

  • @awekeningbro1207
    @awekeningbro1207 11 месяцев назад

    doesnt size_t mean unsigned int? I thought in unsigned int, there are 32 bits, and the number is always greater than 0. so technically it can represent any number in 2^32 combinations of bit which is like 4 billion

  • @germankoga8640
    @germankoga8640 Год назад

    making a size_t variable does't have a lot of overhead? since I'm asking so many bytes from the computer as many as possible on one variable to likely don't use even half of them?

    • @PortfolioCourses
      @PortfolioCourses  Год назад +1

      The exact size of size_t depends on the compiler/system. It could be as small as 2 bytes. But it's true that in a lot of situations where size_t is returned, for example by strlen(), we can use an int and it will work fine because the numbers we're working with just aren't big enough for it to matter in practice. :-)

    • @germankoga8640
      @germankoga8640 Год назад

      @@PortfolioCourses with that being the case, I should only use size_t when declaring a function that works with arrays and such to make it work in more scenarios but not for regular usage, right? And thanks for the answer and the vid.

    • @PortfolioCourses
      @PortfolioCourses  Год назад +1

      Yes. Though to be honest with how much memory modern computers have, even using it "regularly" is OK. Really any scenario where we could have large enough numbers to justify size_t is where it makes sense to use size_t, I would say. :-)

  • @enterepajfeijj3
    @enterepajfeijj3 7 месяцев назад

    for an int of 2 bytes the range should be -32,768 to 32,767 right ?

    • @PortfolioCourses
      @PortfolioCourses  7 месяцев назад

      That sounds right as 2^16 = 65536 possible numbers. :-)

  • @ΠαύλοςΦουρφουριανάκης

    Bravo! Just a question. why can't we just use unsigned int data type instead of size_t?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      size_t will store the largest unsigned numbers possible, but unsigned int may not be able to store numbers that are as large.

    • @wizrom3046
      @wizrom3046 2 года назад

      @@PortfolioCourses unsigned long does.
      It is my preferred data type unless I specifically need signed math.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      On some compilers unsigned long will be able to store numbers as large as size_t, but interestingly it’s not guaranteed to be the case: stackoverflow.com/questions/15637228/what-is-the-downside-of-replacing-size-t-with-unsigned-long. Sometimes it may need to be unsigned long long for example.

    • @wizrom3046
      @wizrom3046 2 года назад

      @@PortfolioCourses interesting. Ive been using C compilers since TurboC for dos in 1987 and all the later Borland products and unsigned long has always been a 4 byte integer. And multiple compilers for PIC and embedded too. Char=byte, short=2byte, long=4byte
      Uint32 and Sint32 is probably the best way to say it which ie standard most of the newer embedded C compilers.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      @@wizrom3046 Makes sense! I've found C is filled with lots of little things like this where something is supported 95-99% of the time as a convention by compilers but is not technically part of the C standard.