Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Display the minimum and maximum values for each of C's data types

C Basic Declarations and Expressions: Exercise-87 with Solution

Write a C program to display the minimum and maximum values for each of C's data types.

Sample Solution:

C Code:

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <float.h>
int main( void )
{
  printf( "Ranges for integer data types in C\n\n" );
  printf( "------------------------------------------------------------\n");
  printf( "int8_t    %20d  %20d\n"     , SCHAR_MIN , SCHAR_MAX  );
  printf( "int16_t   %20d  %20d\n"     , SHRT_MIN  , SHRT_MAX   );
  printf( "int32_t   %20d  %20d\n"     , INT_MIN   , INT_MAX    );
  printf( "int64_t   %20lld  %20lld\n" , LLONG_MIN , LLONG_MAX  );
  printf( "uint8_t   %20d  %20d\n"     , 0         , UCHAR_MAX  );
  printf( "uint16_t  %20d  %20d\n"     , 0         , USHRT_MAX  );
  printf( "uint32_t  %20d  %20u\n"     , 0         , UINT_MAX   );
  printf( "uint64_t  %20d  %20llu\n"   , 0         , ULLONG_MAX );
  printf( "\n" );
  printf( "============================================================\n\n");
  printf( "Ranges for real number data types in C\n\n" );
  printf( "------------------------------------------------------------\n");
  printf( "float        %14.7g  %14.7g\n"   , FLT_MIN  , FLT_MAX  );
  printf( "double       %14.7g  %14.7g\n"   , DBL_MIN  , DBL_MAX  );
  printf( "long double  %14.7Lg  %14.7Lg\n" , LDBL_MIN , LDBL_MAX );
  printf( "\n" );
return 0;
}

Sample Output:

Ranges for integer data types in C

------------------------------------------------------------
int8_t                    -128                   127
int16_t                 -32768                 32767
int32_t            -2147483648            2147483647
int64_t   -9223372036854775808   9223372036854775807
uint8_t                      0                   255
uint16_t                     0                 65535
uint32_t                     0            4294967295
uint64_t                     0  18446744073709551615

============================================================

Ranges for real number data types in C

------------------------------------------------------------
float          1.175494e-38    3.402823e+38
double        2.225074e-308   1.797693e+308
long double  3.362103e-4932  1.189731e+4932

Flowchart:

C Programming Flowchart: Display the minimum and maximum values for each of C's data types.

C programming Code Editor:

Contribute your code and comments through Disqus.

Previous:Write a C program to display the sizes and ranges for each of C's data types.

Next:Write a C program to create an extended ASCII table. Print the ASCII values 32 through 255.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP