-4

I am learning C programming language and I stumbled on something while playing with some code. I was led to believe that an int has 4 bytes, and therefore has a maximum value of +2147483647(The problem was the long int). But when I tested that in my computer with GCC compiler, the result is different. I tried the same with long int, It gave the same maximum size. Can you please expain this behavior to me ? Other questions didn't help me understand the underlying reason. Let me add some examples I tried to find out what is happening.

enter image description here

Code sample:

#include <stdio.h>
#include <limits.h>
int main(void){

    int a = 1, i;

    printf("int limit: %d\n", INT_MAX);
    printf("long int limit: %li\n", LONG_MAX);
    for (i = 0; i < 10; i++){
        a *= 10;
        printf("A: %d\n", a); // To see when a is corrupted.
    }




    return 0;
}
Rockybilly
  • 2,938
  • 1
  • 13
  • 38
  • 1
    First of all - how 4 bytes turn to be 32767? – Eugene Sh. Mar 29 '16 at 21:47
  • The first bit representing the sign ? – Rockybilly Mar 29 '16 at 21:47
  • And how many remaining? – Eugene Sh. Mar 29 '16 at 21:48
  • 2
    @Rockybilly No images for code please! – πάντα ῥεῖ Mar 29 '16 at 21:49
  • @Eugene The C language does not specify how many bits are in a byte, so perhaps Rockybilly is running on an odd architecture where each byte has only 4 bits? – AJNeufeld Mar 29 '16 at 21:51
  • 1
    Because you did not even show a little effort to solve the problem on your own. And you shall not post images of text. What's the problem with copy/paste? – too honest for this site Mar 29 '16 at 21:51
  • @AJNeufeld I am not talking of C here, but math. – Eugene Sh. Mar 29 '16 at 21:51
  • @AJNeufeld: 4bits/byte is not possible. A byte has at least 8 bits. – too honest for this site Mar 29 '16 at 21:53
  • 1
    I did try to solve this on my own, then wanted to seek help here. Or perhaps you are used to being harsh on people, or you just enjoy it ? This was a simple question maybe caused by a simple misunderstanding or anything I may have overlooked, just needed some guidance about something I could not find, and I got -4 votes. – Rockybilly Mar 29 '16 at 21:53
  • C has five signed integer types, plus unsigned variants: char, short, int, long, and long long. Each type is at least as long as the previous one, but not necessarily longer. An int must have at least 16 bits, a long at least 32, and a long long at least 64. – rici Mar 29 '16 at 21:54
  • @EugeneSh. Yes. And if Rocky's version of C uses 4-bit bytes, 4 bytes would give you 16 bits, which would give you the +32767 max ;-) – AJNeufeld Mar 29 '16 at 21:54
  • If you need an answer - don't mind the votes. – Eugene Sh. Mar 29 '16 at 21:54
  • Indeed @Eugene, forgive my overreacting. – Rockybilly Mar 29 '16 at 21:54
  • @rici: No. It's six types. You forgot `_Bool`! – too honest for this site Mar 29 '16 at 21:55
  • @AJNeufeld Oh, that... I doubt he has such a weird arch :) – Eugene Sh. Mar 29 '16 at 21:55
  • @AJNeufeld: If Rocky's version of C uses 4-bit bytes, **it's not C**. C requires `CHAR_BIT >= 8`. – Keith Thompson Mar 29 '16 at 22:00
  • @rici: `_Bool` is very well an integer type. See 6.2.5p6 ("The type _Bool and the unsigned integer types that correspond to the standard signed integer types are the standard **unsigned** integer types"). You did not state it is about signed integer types. p4 uses the phrase "There are five standard **signed** integer types. None is just about "integer types". – too honest for this site Mar 29 '16 at 22:00
  • 1
    @Olaf I `char` is defined as at least 8 bits ( CHAR_BIT is required to be at least 8). From Wikipedia: a byte is "a group of binary digits or bits (usually eight) operated on as a unit." But where does it say that the size of a byte **must** be 8 bits? A C-compiler for the TMS320 I worked on used 32-bit chars ... all memory accesses were 32-bits wide; `sizeof(int) == sizeof(char)`. One could argue that its bytes were 32-bits. And yes, I am being facetious. – AJNeufeld Mar 29 '16 at 22:00
  • @AJNeufeld: That is fine, but 4-bit bytes are still not possible. 8 bits is the minimum – rici Mar 29 '16 at 22:01
  • @AJNeufeld: How is that facetious? It's irrelevant to the OP's question, but in a C implementation with 32-bit `char`, a byte is 32 bits by definition. – Keith Thompson Mar 29 '16 at 22:01
  • @AJNeufeld: This is about C, so the definition of "byte" from the **authoritative** C standard applies, not the one of the **non-authoritative** Wikipedia. I wrote "A byte has **at least** 8 bits." which includes it can have more than 8 bits! But you stated "... an odd architecture where each byte has only 4 bits? – AJNeufeld 12 mins ago" Which you now conformed as wrong. – too honest for this site Mar 29 '16 at 22:01
  • @olaf: Yes, I fixed my comment. (Mostly; the time limit ran out.) – rici Mar 29 '16 at 22:02

2 Answers2

1

You're using %d to print them both out, that's what is causing this misunderstanding.

Ivan Valeriani
  • 620
  • 5
  • 13
  • I tried the second printf function with %ld and %li but I am getting the same result. Could be another thing I missed ? – Rockybilly Mar 29 '16 at 21:56
  • Take a look at http://stackoverflow.com/questions/38561/what-is-the-argument-for-printf-that-formats-a-long – Ivan Valeriani Mar 29 '16 at 21:58
  • No, that's not what's causing the misunderstanding. Printing a `long` value with `%d` has undefined behavior, but on the OP's system `int` and `long` have the same representation, so it's very likely to work. – Keith Thompson Mar 29 '16 at 22:05
0

The C++ standard guarantees that int is greater than or larger than short and long is greater than or larger than int etc. See http://en.cppreference.com/w/cpp/language/types or the standard itself for all the details. On 32 bit machines 32 bits is usually the size of int. On 64 bit machines int may be 32 or 64 bits depending on operating system and other circumstances. Also, as others have pointed out, your printing of the variable may truncate it.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • @Keith Thompson I know. That's why I implied that OP should read the actual standard. – Jesper Juhl Mar 29 '16 at 22:07
  • @Keith Thompson I am well aware that it also guarantees minimum sizes, but that's not really all that relevant to OP's question. – Jesper Juhl Mar 29 '16 at 22:11
  • My problem was a simple confusion because I looked to ANSI values of max_int I guess. The main problem was my long int and int having same bytes(`sizeof()`). This distracted me from the fact that byte has eight bits. I should delete the question. – Rockybilly Mar 29 '16 at 22:13
  • @Keith Thompson fixed – Jesper Juhl Mar 29 '16 at 22:19
  • Let's clean up these comments. – Keith Thompson Mar 29 '16 at 22:20
  • @Rockybilly: The question might help some future reader who has the same confusion you did. But what do you mean by "ANSI values of max_int"? – Keith Thompson Mar 29 '16 at 22:22
  • I was looking at a page which called it ANSI and gave int_max as +32767 but I could not find it. Here is another page I looked and it says something about macros which I didnt understand http://www.tutorialspoint.com/c_standard_library/limits_h.htm – Rockybilly Mar 29 '16 at 22:25
  • @Rockybilly: [ANSI](http://ansi.org/) is the body that produced the first C standard in 1989. Since then, the 1990, 1999, and 2011 standards have been published by ISO, but the term "ANSI C" is often (not quite correctly) used to refer to the language described by the original standard. The standard requires `INT_MAX` to be *at least* 32767, but it's commonly larger (typically 2**31-1, or 2147483647). And spelling is important: it's `INT_MAX`, not "max_int". – Keith Thompson Mar 29 '16 at 23:17
  • @Rockybilly: [That web site](http://www.tutorialspoint.com/c_standard_library/limits_h.htm) is not very well organized, but it does say above the table that "these values may not be any lower than what is given here". – Keith Thompson Mar 30 '16 at 00:36