Thread: advantage of using size qualifier before data type in C language.

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    89

    advantage of using size qualifier before data type in C language.

    Both the following statements are valid statements in C language.

    int count;
    short count;

    Compiler allocates memory for both int count and short count

    short int count is also valid statement in c language.

    short is size qualifier while int is data type, what is the use of putting short in front of int?

    I don't understand what is the advantage of using size qualifier before data type in C language.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    In most implementations, a short int takes up less memory than a regular int.

    That might not seem like that big a deal today, with your 64-bit OS and many GB of memory to play with.

    But C runs on a wide variety of machines.

    At the low end, there is for example Introduction to the Microchip PIC C Programming
    Memory space is in the low KB.

    Also, C is an old language (created in the early 1970's), when memory literally cost $1 per byte.
    It was natural and desirable that the language could support every width of data the underlying machine could support.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Kittu20 View Post
    Both the following statements are valid statements in C language.

    int count;
    short count;

    Compiler allocates memory for both int count and short count
    Ok... but not one followed by another with the same identifier... Declare two or more objects this way and you'll get a compile error.

    short isn't a "size qualifier". It is a type. There are 4 (type) qualifiers in C: const, restrict, volatile and _Atomic. And there are 10 integral different types: char, short int, int, long int and long long int (and their 'unsigned' cosins), accordingly to ISO 9899 (from 1999 until now). These types can have different sizes (Yep, char is an integer type, but it's kinda of special).

    I said 'can have' because it depends on the implementation. The only garantee is that sizeof(char)<=sizeof(short int)<=sizeof(int)<=sizeof(long int)<=sizeof(long long int). Take int and long int as example... For Intel x86 (or ARM), as examples, depending on the Application Binary Interface, they have the same size: 32 bits on MS-ABI. Or for, x86-64 mode, in SysV ABI, long int have the same size as long long int. There are (strange and rare) architectures which have short int, int, long int and long long int having the same size!
    Last edited by flp1969; 10-15-2022 at 08:22 AM.

  4. #4
    Banned
    Join Date
    Jul 2022
    Posts
    112
    Technically int should be 32-bit, otherwise this will cause many problems.
    It is easy to write broken programs.

  5. #5
    Banned
    Join Date
    Jul 2022
    Posts
    112
    OP can refer to standard types (int8_t, int32_t),
    but OP should not worry about those situations.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by kodax View Post
    OP can refer to standard types (int8_t, int32_t),
    but OP should not worry about those situations.
    char, int, long, float, double, etc... are all "Standard Types" and were part of the C Programming Language before the addition of "Fixed width integer types", (int8_t, int32_t, etc...) in C99.

    Short of taking a course in C Programming from a qualified instructor, you need to study a good book on the C Programming Language, cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach, 2nd Edition
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel

    Studying one of these books, and writing code, you will have a much better understanding of the C Programming language.

  7. #7
    Banned
    Join Date
    Jul 2022
    Posts
    112
    char, int, long, float, double, etc... are all "Standard Types"

    I was referring to Standard Integer Types (stdint.h)

    Studying one of these books, and writing code, you will have a much better understanding of the C Programming language.
    What an absurd claim !!
    Also why are you recommending these books in every thread, and in that case only these books ?

    "For years, countless educators have asserted that books give readers a chance to journey to exotic, far-off lands and meet strange, exciting new people," Education Secretary Richard Riley told reporters. "We have found this is simply not the case."
    Books Don't Take You Anywhere


    Books without the knowledge of life are useless.
    Samuel Johnson
    Last edited by kodax; 10-15-2022 at 10:30 AM.

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by kodax View Post
    Technically int should be 32-bit, otherwise this will cause many problems.
    It is easy to write broken programs.
    Nope. Technically the mininum size for `int` is suggested to be 16 bits, as described in ISO 9899 5.2.4.2 (See INT_MAX, as an example).
    And, ISO 9899 6.2.5 § 5 is clear:

    "...A "plain" int object has the natural size suggested by the architecture of the execution environment (large enough to contain any value in the range INT_MIN to INT_MAX as defined in the header <limits.h>)" (the boldface is mine).

    On 32 bit systems it has the natural size of 32 bits (Intel x86 processors are 32 bits processors, even on x86-64 mode -- which requires a REX prefix on instructions dealing with R?? registers -- except on effective addresses where the default is 64 bits cannonical flat addressing). Remember the old MS-DOS, where int has the length of 16 bits. And, as Salem remembered well, there are also microcontrollers to deal with.

  9. #9
    Registered User
    Join Date
    Oct 2022
    Posts
    2
    kodax, your quote is from The Onion. Surely you must be joking? It's widely known that The Onion is satire.

  10. #10
    Banned
    Join Date
    Jul 2022
    Posts
    112
    kodax, your quote is from The Onion. Surely you must be joking? It's widely known that The Onion is satire.

    A study released Monday by the U.S. Department of Education revealed that, contrary to the longtime claims of librarians and teachers, books do not take you anywhere.

    Not just some department, but the U.S. Department of Education, the irony..


    kodax, your quote is from The Onion. Surely you must be joking? It's widely known that The Onion is satire.

    Then you should know that books do not guarantee knowledge, but wasting your time to read one, is guaranteed.


    Just buy the book and the best thing of life could be yours, for 9.95 off your credit card,
    but don't think ahead 6 months,
    because the author said that he will change your life..


    don't worry about the fact that your are broke,
    don't worry about the fact that you won't have the cash to retire.
    don't worry about the fact that your life is a meaningless existence of consumerism,

  11. #11
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by kodax View Post
    Then you should know that books do not guarantee knowledge, but wasting your time to read one, is guaranteed.
    Ahhhh... this explains a lot!

  12. #12
    Banned
    Join Date
    Jul 2022
    Posts
    112
    Ahhhh... this explains a lot!
    What is the point of judging people,
    when you realize that you are wrong ?

    Books do not guarantee knowledge,
    in fact, books cannot guarantee knowledge.

    The right way is to find a tutor,
    even then knowledge is not guaranteed.


    Publishers are making fun of their customers,
    Beginning Programming with Java "For Dummies"

    But if you persist in your belief that books can teach you how to live,
    then sit down and do nothing but read books until you die.


    In fact, the only lives books can guarantee to change are those of the authors.


    Why books won't change your life | Fiction | The Guardian
    Last edited by kodax; 10-15-2022 at 12:45 PM.

  13. #13
    Registered User
    Join Date
    Oct 2022
    Posts
    2
    No one here claimed that books would change your life.
    Or that they'd "guarantee knowledge."
    In fact, the person who brought up books in the first place said "Studying one of these books, and writing code, you will have a much better understanding of the C Programming language," so they were already aware that books alone will not help you.

    In any case, this thread is getting off-topic.

  14. #14
    Banned
    Join Date
    Jul 2022
    Posts
    112
    you will have a much better understanding of the C Programming language
    This is an irrelevant claim.
    Here's a very good advice, you will not gain any understanding reading a book, find a local tutor..



    Critical review from amazon.

    Just for business !
    I used this book for self-learning and as support material. This is the worst book i could have bought as it drained my enthusiasm for C++, despite the hours of work i put into trying to decipher how the explanations (unending paragraphs that didn't converge to any satisfactory conclusion) were related to the complicated examples.

    At first sight, Deitels' books might appear like an impressive "catalog" of text with colors, highlights, logos, etc, (which is what initially caught my attention and made me buy it) but once you dive into the contents, it's a different story. The Deitels are making their books increasingly difficult for new students to C++, and the learning process is getting harder with each new edition, sacrificing clarity and instead adding even more reading material, which makes the entire learning experience very strenuous and confusing. If done right, the new edition should have been at least 500 pages less. But instead, you are thrown into a mix of complicated examples as from the early chapters, stretching out to several pages, and the most discouraging aspect is that the later chapters keep referring to those earlier confusing examples to build upon. There is no learning curve with this book. You just tag along, and absorb what you can. I am so disappointed with this book's structure. I really think the authors should think more about the new and intermediate programmer, rather than compiling as much information as they can and stacking it with every new edition, which just makes it into a C++ miscellaneous reference for advanced users, rather than a progressive textbook for new programmers.

    The Deitels release a new edition as soon as they can, every 18-24 months, which is faster than most other programming books. In this case, the release of new editions is not justified at all, as it just doesn't make any sense to keep pushing in that direction, since it's getting more confusing with every new edition. My friend had the 7th edition and i bought the latest 8th edition, naively believing that the book had largely improved, as it had advertised. Let's just say that i'm not trusting another Deitel book ever again!
    Amazon.com
    Last edited by kodax; 10-15-2022 at 09:22 PM.

  15. #15
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by kodax View Post
    This is an irrelevant claim.
    Here's a very good advice, you will not gain any understanding reading a book, find a local tutor..
    You have a far better chance of finding a good book than you will a tutor, especially if you are not prepared to pay a sensible hourly rate (i.e. approaching that of an experienced programmer).

    If do you find a tutor, they will usually recommend you follow a textbook, and they help you along the way.

    Sponging glimmers of information of others on a Discord, Reddit or a web forum is pretty much the least efficient way to learn any technical topic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is qualifier in c
    By skyr6546 in forum C Programming
    Replies: 4
    Last Post: 12-21-2019, 03:09 PM
  2. About volatile type qualifier
    By Mr.Lnx in forum C Programming
    Replies: 4
    Last Post: 06-27-2014, 03:39 AM
  3. ROM Qualifier vs Const?
    By Syndacate in forum C Programming
    Replies: 20
    Last Post: 01-20-2011, 01:42 PM
  4. Discards qualifier from pointer target type?
    By pdstatha in forum C Programming
    Replies: 1
    Last Post: 04-16-2002, 02:12 PM
  5. Type Qualifier const
    By PutoAmo in forum C Programming
    Replies: 3
    Last Post: 03-30-2002, 08:55 PM

Tags for this Thread