Size of type int with short and long prefixes in C

Asked

Viewed 439 times

4

My architecture is Unix, so by default the size of type int is 4 bytes, so far so good. In Luis Dama’s book he states that short and long prefixes solve the problem for portability of programs between different architectures, because short prefixes establish a minimum value of 2 bytes and long prefixes a maximum value of 4 bytes. According to him a statement, on a Unix architecture, of the type:

long int num;

printf("%d", sizeof(num));

Would have as exit:

4

But in practice the way out is:

8

I wonder if there is an error in the information given in the book (pag 31 of the 10th edition), or if there has been any change in the pattern of C that does not make it more possible.

  • 1

    Some useful links: http://linguagemc.com.br/tipos-de-dados-em-c/ and https://stackoverflow.com/questions/35844586/can-i-assume-the-size-of-long-int-always-4-bytes

  • 1

    Here is an example of the real world. For the following code: #include <stdio.h>&#xA;&#xA;int main ()&#xA;{&#xA; printf("sizeof(long) = %zu\n", sizeof(long));&#xA; return 0;&#xA;} Output on Debian 7 i686: sizeof(long) = 4 Output on Centos 7 x64: sizeof(long) = 8

  • It seems the author commented an error in stating that the prefixes define a single size for any architecture.

  • Do you have link book? I want to read this book,to see me the best excerpt.

  • 1

    http://www.ams.eti.br/booklets/Luis_Damas.pdf page 42 of the pdf

  • 1

    He missed badly.... I don’t know anything C, but I knew it. The values in memory are 8(long) bytes and 4(short) bytes.

Show 1 more comment

2 answers

3


For portability that question already talks a little about, where @Maniero responds:

Defined behaviour
In fact most behaviors are defined by the specification and for an implementation conform to the specification needs to do exactly what she says. Good. By be normal and there’s no definition of it.

If your code needs to be portable, only defined behaviors should be used, or the other three forms of behavior need be properly addressed in each compilation environment in a individual.

According to the answers that question in Soen, no specification guarantees, nor POSIX, of size occupied in memory that a type variable short, int and long shall have. In return for draft of the specification you have the guarantees of "minimum values":

Their implementation-defined values Shall be Equal or Greater in magnitude (Absolute value) to those Shown, with the same Sign.

For example, in my system:

#include<stdio.h>
#include<limits.h>
int main(void){
    printf("The value of INT_MAX is: %d.\n", INT_MAX);
    printf("The value of INT_MIN is: %d.\n", INT_MIN);
}

Whose result is:

mateus@debian:~/desk$ ./a.out 
The value of INT_MAX is: 2147483647.
The value of INT_MIN is: -2147483648.

Note that the value of INT_MIN in my system is minor than the specification, but in absolute values my machine is larger, and therefore in this case meets the specification.

Still in draft §5.2.4.2.1:

minimum value for an Object of type short int
SHRT_MIN -32767 // (2 15 1)

Maximum value for an Object of type short int
SHRT_MAX +32767 // 2 15 1

minimum value for an Object of type int
INT_MIN -32767 // (2 15 1)

Maximum value for an Object of type int
INT_MAX +32767 // 2 15 1

minimum value for an Object of type long int
LONG_MIN -2147483647 // (2 31 1)

Maximum value for an Object of type long int
LONG_MAX +2147483647 // 2 31 1

What I wrote is about C11, the book goes back, although I can’t guarantee that it met the older standards of the language, because I don’t know it myself.

1

Hello. Here’s an example of the real world. For the following code:

#include <stdio.h>
int main () {
  printf("sizeof(long) = %zu\n", sizeof(long)); 
  return 0;
}

Output on Debian 7 i686: sizeof(long) = 4

Output on Centos 7 x64: sizeof(long) = 8

Luis Dama stated in his book:

short = 2 bytes long = 4 bytes

Your book: http://www.ams.eti.br/livros/Luis_Damas.pdf

But in outputs the value is different.

The value returns in this different:

sizeof(short): %d\n", 4
sizeof(int): %d\n", 4
sizeof(long): %d\n", 4
sizeof(long long): 8
sizeof(size_t): 4
sizeof(void *): 4
Hit enter to exit.

Take an example: https://stackoverflow.com/questions/20109984/c-c-sizeofshort-sizeofint-sizeoflong-sizeoflong-long-etc-on-a

Did he miss or missed attention?

Source: https://stackoverflow.com/questions/35844586/can-i-assume-the-size-of-long-int-is-always-4-bytes

Browser other questions tagged

You are not signed in. Login or sign up in order to post.