Pages

Saturday, 27 March 2021

How to find the ranges of Basic C Data types




Signed : (2n-1) - 1
Min : - (2n-1
Max: + (2n-1) - 1

Unsigned:(2n) - 1
Min :0
Max: (2n) - 1

1.Example: 
Char=1Byte =8bits 
Signed Char: (2n-1) - 1
Min : - (28-1) = -128 
Max: + (28-1) - 1= +127

Unsigned char:(2n) - 1
Min :0
Max: (28) - 1=255

2.Example: 
Int=2Bytes=16bits
Signed int: (2n-1) - 1
Min : - (216-1) = -32768 
Max: + (216-1) - 1= +32767

Unsigned int:(2n) - 1
Min :0
Max: (216) - 1=65535

3.Example:
Float=4Bytes=32bits
Signed int: (2n-1) - 1
Min : - (232-1) = -2.14748365E9 
Max: + (232-1) - 1= +32767

Unsigned int:(2n) - 1
Min :0
Max: (216) - 1=65535

Float=4
Double=8

DATA TYPES RANGES :

Data Type

Memory (In Bytes)

Format Specifiers

Range

int

4

%d

-2,147,483,648 to 2,147,483,647

short int

2

%hd

-32,768 to 32,767

unsigned int

4

%u

0 to 4,294,967,295

unsigned short int

2

%hu

0 to 65,535

long int

4

%ld

-2,147,483,648 to 2,147,483,647

unsigned long int

4

%lu

0 to 4,294,967,295

long long int

8

%lld

-(263) to (263)-1

unsigned long long int

8

%llu

0 to 18,446,744,073,709,551,615

char

1

%c

-128 to 127

Signed char

1

%c

-128 to 127

Unsigned char

1

%c

0 to 255

float

4

%f

double

8

%lf

long double

12

%Lf

FORMAT SPECIFIERS 

Format Specifiers

Data Type

Elucidation

%d, %i

int
short
long
unsigned short

Used for integers

%c

char
signed char
unsigned char

Used for characters with any type modifier

%f

float

Used for decimal values

%e, %E, %g, %G

float
double

Used for scientific notation of decimal values

%hi

short

Used for signed short integers

%hu

unsigned short

Used for unsigned short integers

%l, %ld, %li

long

Used for signed integers

%lf

double

Used for floating-point

%Lf

long double

Used for floating-point

%lu

unsigned int
unsigned long

Used for unsigned integers

%lli, %lld

long long

Used for signed long integers

%llu

unsigned long long

Used for unsigned long long integers

%s

char *

Used for a string

%p

void *

Used when finding the address of the pointer to void *

%o

int
short
long
unsigned short
unsigned int

Used for the octal representation of Integer.

%u

unsigned int
unsigned long

Used for unsigned integers

%x, %X

int
short
long
unsigned short
unsigned int

Used for the hexadecimal representation of Unsigned Integer

%%

Used to print % character

%n

Used to print nothing


METHOD 1:

#include<stdio.h>

#include<limits.h>

int main()

 {

   printf("The number of bits in a byte = %d\n", CHAR_BIT);

   printf("\nThe minimum value of Signed CHAR is = %d\n", SCHAR_MIN);

   printf("The maximum value of Signed CHAR is = %d\n", SCHAR_MAX);

   printf("The minimum value of CHAR is = %d\n", CHAR_MIN);

   printf("The maximum value of CHAR is = %d\n", CHAR_MAX);

   printf("The maximum value of Unsigned CHAR is = %u\n", UCHAR_MAX);


   printf("\nThe minimum value of Signed Short is = %d\n", SHRT_MIN);

   printf("The maximum value of Signed Short is = %d\n", SHRT_MAX);

   printf("The maximum value of Unsigned Short is = %u\n", USHRT_MAX);


   printf("\nThe minimum value of Signed INT is = %d\n", INT_MIN);

   printf("The maximum value of Signed INT is = %d\n", INT_MAX);

   printf("The maximum value of Unsigned INT is = %u\n", UINT_MAX);


   printf("\nThe minimum value of Signed LONG is = %ld\n", LONG_MIN);

   printf("The maximum value of Signed LONG is = %ld\n", LONG_MAX);

   printf("The maximum value of Unsigned LONG is = %lu\n", ULONG_MAX);

   return 0;

 }

Output:

The number of bits in a byte = 8


The minimum value of Signed CHAR is = -128

The maximum value of Signed CHAR is = 127

The minimum value of CHAR is = -128

The maximum value of CHAR is = 127

The maximum value of Unsigned CHAR is = 255


The minimum value of Signed Short is = -32768

The maximum value of Signed Short is = 32767

The maximum value of Unsigned Short is = 65535


The minimum value of Signed INT is = -2147483648

The maximum value of Signed INT is = 2147483647

The maximum value of Unsigned INT is = 4294967295


The minimum value of Signed LONG is = -9223372036854775808

The maximum value of Signed LONG is = 9223372036854775807

The maximum value of Unsigned LONG is = 18446744073709551615


METHOD 2:

The following are the constants defined in the header file <float.h>

#include<stdio.h>

#include<float.h>

int main()

 {

   printf("The minimum value of float is = %.10e\n", FLT_MIN);

   printf("The maximum value of float is = %.10e\n", FLT_MAX);


   printf("\nThe minimum value of Double is = %.10e\n", DBL_MIN);

   printf("The maximum value of Double is = %.10e\n", DBL_MAX);


   printf("\nThe minimum value of LONG Double is = %.10Le\n",LDBL_MIN);

   printf("The maximum value of LONG Double is = %.10Le\n",LDBL_MAX);


   return 0;

 }

Output:-

The minimum value of float is = 1.1754943508e-38

The maximum value of float is = 3.4028234664e+38


The minimum value of Double is = 2.2250738585e-308

The maximum value of Double is = 1.7976931349e+308


The minimum value of LONG Double is = 3.3621031431e-4932

The maximum value of LONG Double is = 1.1897314954e+4932


METHOD 3:
PROGRAM TO FIND THE MIN AND MAX VALUES OFTHE GIVEN DATA TYPES:

# include <stdio.h>
# include <float.h>
# include <conio.h>
# include <limits.h>
void main()
{
  clrscr();
   printf(" The smallest value of signed char is %d\n", CHAR_MIN);
   printf(" The largest  value of signed char is %d\n", CHAR_MAX);
   printf(" The largest  value of unsigned char is %u\n", UCHAR_MAX);
   printf("\n");
   printf(" The smallest value of int is %d\n", INT_MIN);
   printf(" The largest  value of int is %d\n", INT_MAX);
   printf(" The smallest value of short int is %hd\n", SHRT_MIN);
   printf(" The largest  value of short int is %hd\n", SHRT_MAX);
   printf(" The smallest value of long int is %ld\n", LONG_MIN);
   printf(" The largest  value of long int is %ld\n", LONG_MAX);
   printf(" The largest  value of unsigned short int  is %u\n", USHRT_MAX);
   printf(" The largest  value of unsigned int  is %u\n", UINT_MAX);
   printf(" The largest  value of unsigned long int is %lu\n", ULONG_MAX);
   printf("\n");
   printf(" The smallest value of float is %e\n", FLT_MIN);
   printf(" The largest  value of float is %e\n", FLT_MAX);
   printf("\n");
   printf(" The smallest value of double is %le\n", DBL_MIN);
   printf(" Tthe largest  value of double is %le\n", DBL_MAX);
   printf(" The smallest value of long double is %Le\n", LDBL_MIN);
   printf(" Tthe largest  value of long double is %Le\n", LDBL_MAX);
   printf("\n");

  getch();
}

Output:


No comments:

Post a Comment

Programs in turboc3 : Files

File Handling in C File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our...