C keyword int

From DocDroppers

(Redirected from C keyword short)
Jump to: navigation, search

Contents

The C language 'int' keyword

Used to define a variable of the same type, or in casting to cast the given variable to the 'int' data type. It is also a C and C++ keyword.

Declaration Example

The following is an example of a 'int' being declared named 'foo'.

int foo;

It is important to not that since the above declaration is not explicitly signed or unsigned, it could be either one depending entirely on the compiler that is used to compile the code.

Explicitly use unsigned or signed in your variable declaration to be sure!

signed int foo;
unsigned int bar;

Modifiers for this Type

The short or long keywords are used to further define the type of integer value the given int may hold. Only one of them may be used at once on a given variable declaration.

Platform Specific Data

The size of an int in memory is not the same on every computing platform, so it is important to note that the values it can hold and the amount of memory one of them will use could be different from one hardware architecture to the other. The following table gives a good summery of the specific data bounds and sizes for a given platform.

Type Declaration CPU Platform # of bytes # of bits Min Value Max Value
inti386432*CD*CD
short inti386216*CD*CD
long inti386432*CD*CD
unsigned inti3864320+4294967295
unsigned short inti3862160+65535
unsigned long inti3864320+4294967295
signed inti386432-2147483648+2147483647
signed short inti386216-32768+32767
signed long inti386432-2147483648+2147483647



*CD: Compiler Dependent. Check your compilers documentation to assure unsigned or signed by default.

Common Uses

  • The 'int' data type is most commonly used in integer buffers, because it can not hold floating point values.
  • A unsigned int is often used to track size, since it allows a higher max value and the number of something is usually never negative.
  • A signed int is often used as a return value in functions to help track failure inside the function. 0 Means OK, -1 means error.

Detection code

The following is a small C code application that when compiled and run will tell you the systems limits for the different int types. Check your compilers documentation for details on how to compile it. If you have hardware that is not listed above, please add the details to the table above.


#include <stdio.h>
#include <limits.h>

/*
 * File: detect_int.c
 * What it does: Detects the limits on the systems 'int' type.
 * Who wrote this: Feverdream of the BinRev.com Forums
 * License: This code snippet is GPL v3 or higher.
 * To compile in gcc: gcc -o test detect_int.c
 * To run after compilation in gcc: ./test
 */
int main(int argc, char*argv[]){
        printf("unsigned long int sizeof(): %u\n", sizeof(unsigned long int));
        printf("  signed long int sizeof(): %u\n", sizeof(signed long int));

        printf("unsigned short int sizeof(): %u\n", sizeof(unsigned short int));
        printf("  signed short int sizeof(): %u\n", sizeof(signed short int));

        printf("unsigned int sizeof(): %u\n", sizeof(unsigned int));
        printf("  signed int sizeof(): %u\n", sizeof(signed int));


        printf("  signed short int min: %d\n", SHRT_MIN);
        printf("  signed short int max: %d\n", SHRT_MAX);

        printf("unsigned short int min: 0\n");
        printf("unsigned short int max: %d\n", USHRT_MAX);

        printf("  signed int min: %d\n", INT_MIN);
        printf("  signed int max: %d\n", INT_MAX);

        printf("unsigned int min: 0\n");
        printf("unsigned int max: %u\n", UINT_MAX);

        printf("  signed long int min: %ld\n", LONG_MIN);
        printf("  signed long int max: %ld\n", LONG_MAX);

        printf("unsigned long int min: 0\n");
        printf("unsigned long int max: %lu\n", ULONG_MAX);

#if 0
#define COMMENT "Not all platforms support these, so it is ifdef'ed out."
printf("signed long long int min: %ld\n", LLONG_MIN);
printf("signed long long int max: %ld\n", LLONG_MAX);
printf("unsigned long long int min: %ld\n", ULLONG_MAX);
#endif

return 0;
}
Personal tools