C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Integers (short,int,long) in C Programming Language

In this tutorial, we will discuss the different integer types in C programming language: short, int, and long. These integer types are used to store integer values of various sizes and can be signed (can store negative values) or unsigned (can only store non-negative values).

1. short:

short is used to store short integer values. The size of short can vary between different systems, but it is guaranteed to be at least 16 bits in size. The range of signed short is typically from -32,768 to 32,767, while the range of unsigned short is from 0 to 65,535.

short signedShort = -32768;
unsigned short unsignedShort = 65535;

2. int:

int is used to store regular integer values. The size of int is guaranteed to be at least 16 bits, but it is commonly 32 bits on modern systems. The range of signed int on a 32-bit system is typically from -2,147,483,648 to 2,147,483,647, while the range of unsigned int is from 0 to 4,294,967,295.

int signedInt = -2147483648;
unsigned int unsignedInt = 4294967295;

3. long:

long is used to store long integer values. The size of long is guaranteed to be at least 32 bits, but it can be larger on modern systems, such as 64 bits. The range of signed long on a 64-bit system is typically from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, while the range of unsigned long is from 0 to 18,446,744,073,709,551,615.

long signedLong = -9223372036854775808L;
unsigned long unsignedLong = 18446744073709551615UL;

Note: In C99 and later standards, you can also use the long long type, which is guaranteed to be at least 64 bits in size.

long long signedLongLong = -9223372036854775807LL;
unsigned long long unsignedLongLong = 18446744073709551615ULL;

Printing integer types:

When printing integer types with printf(), you should use the appropriate format specifiers for each type:

  • short: %hd for signed and %hu for unsigned
  • int: %d or %i for signed and %u for unsigned
  • long: %ld for signed and %lu for unsigned
  • long long: %lld for signed and %llu for unsigned

Here's an example demonstrating the usage of various integer types:

#include <stdio.h>

int main() {
  short signedShort = -32768;
  unsigned short unsignedShort = 65535;
  printf("signedShort: %hd, unsignedShort: %hu\n", signedShort, unsignedShort);

  int signedInt = -2147483648;
  unsigned int unsignedInt = 4294967295;
  printf("signedInt: %d, unsignedInt: %u\n", signedInt, unsignedInt);

  long signedLong = -9223372036854775807L;
  unsigned long unsignedLong = 18446744073709551615UL;
  printf("signedLong: %ld, unsignedLong: %u\n", signedLong, unsignedLong);

  return 0;
}
  1. Size and Range of Integer Types in C Language:

    #include <stdio.h>
    #include <limits.h>
    
    int main() {
        printf("Size of short: %lu bytes\n", sizeof(short));
        printf("Range of short: %d to %d\n", SHRT_MIN, SHRT_MAX);
    
        printf("Size of int: %lu bytes\n", sizeof(int));
        printf("Range of int: %d to %d\n", INT_MIN, INT_MAX);
    
        printf("Size of long: %lu bytes\n", sizeof(long));
        printf("Range of long: %ld to %ld\n", LONG_MIN, LONG_MAX);
    
        return 0;
    }
    

    This code shows the size and range of short, int, and long using sizeof and constants from the <limits.h> header.

  2. C Code Examples with short, int, and long Variables:

    #include <stdio.h>
    
    int main() {
        short shortVar = 32767;
        int intVar = 2147483647;
        long longVar = 9223372036854775807L;
    
        printf("Short: %hd\n", shortVar);
        printf("Int: %d\n", intVar);
        printf("Long: %ld\n", longVar);
    
        return 0;
    }
    

    Variables of type short, int, and long can hold integer values within their respective ranges.

  3. Choosing the Right Integer Type for Variables in C:

    Choose the appropriate integer type based on the required range and memory usage. Use int for general-purpose integers, short for small values, and long for larger values.

  4. Type Casting and Conversions with Integers in C:

    #include <stdio.h>
    
    int main() {
        int intValue = 42;
        float floatValue = 3.14;
    
        // Type casting from int to float
        float result = (float)intValue + floatValue;
    
        printf("Result: %f\n", result);
    
        return 0;
    }
    

    Type casting is used to convert between different data types, such as casting an int to a float.

  5. Signed vs. Unsigned Integers in C Programming:

    #include <stdio.h>
    
    int main() {
        int signedVar = -42;
        unsigned int unsignedVar = 42;
    
        printf("Signed: %d\n", signedVar);
        printf("Unsigned: %u\n", unsignedVar);
    
        return 0;
    }
    

    signed integers can represent both positive and negative values, while unsigned integers represent only non-negative values.

  6. Efficient Use of Integer Types in C Code:

    Use the smallest integer type that accommodates the required range to optimize memory usage.

  7. Integer Overflow and Handling in C:

    #include <stdio.h>
    #include <limits.h>
    
    int main() {
        int maxValue = INT_MAX;
    
        // Overflow example
        int overflowedValue = maxValue + 1;
    
        printf("Overflowed Value: %d\n", overflowedValue);
    
        return 0;
    }
    

    Integer overflow occurs when a value exceeds the maximum representable value for its type. Handling overflow requires careful validation and error checking.