2 Replies - 8120 Views - Last Post: 31 January 2011 - 12:56 AM Rate Topic: -----

#1 kaiser0792  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 15-December 10

C Data Type Size and Ranges

Posted 30 January 2011 - 11:09 PM

I'm trying to write a program that outputs the size (in bytes and bits) and range of simple, common data types. Below is my code. I'm good until I get to long ints, floats and doubles. I'm pretty sure that the problems is that my placeholders need modification, i.e., not big enough to hold the limits.h and float.h data types. Any insight would be greatly appreciated.

#include <stdio.h>
#include <limits.h>
#include <float.h>
#define BITS_PER_BYTE 8

int
main(void)
{
    int charBytes = sizeof(char);
    int shortIntBytes = sizeof(short int);
    int shortIntBits; 
    int intBytes = sizeof(int);
    int intBits;
    int longIntBytes = sizeof(long int);
    int longIntBits;
    int floatBytes = sizeof(float);
    int floatBits;
    int doubleBytes = sizeof(double);
    int doubleBits;
        
    printf("The size of data type 'char' is %d bytes, or %d bits.\n", 
           charBytes, CHAR_BIT);
    printf("The minimum value for the unsigned 'char' data type is %d.\n", 
           CHAR_MIN);
    printf("The maximum value for the unsigned 'char' data type is %d.\n", 
           CHAR_MAX);
    printf("The minimum value for the signed 'char' data type is %d.\n",
           SCHAR_MIN);
    printf("The maximum value for the signed 'char' data type is %d.\n\n",
           SCHAR_MAX);
    
    shortIntBits = shortIntBytes * BITS_PER_BYTE;
    printf("The size of data type 'short int' is %d bytes, or %d bits.\n", 
           shortIntBytes, shortIntBits);
    printf("The minimum value for the signed 'short int' data type is %d.\n",
           SHRT_MIN);
    printf("The maximum value for the signed 'short int' data type is %d.\n\n",
           SHRT_MAX);
    
    intBits = intBytes * BITS_PER_BYTE;
    printf("The size of data type 'int' is %d bytes, or %d bits.\n",
           intBytes, intBits);
    printf("The minimum value for the signed 'int' data type is %d.\n",
           INT_MIN);
    printf("The maximum value for the signed 'int' data type is %d.\n\n",
           INT_MAX);
    
    longIntBits = longIntBytes * BITS_PER_BYTE;
    printf("The size of data type 'long int' is %d bytes, or %d bits.\n",
           longIntBytes, longIntBits);
    printf("The minimum value for the signed 'long int' data type is %d.\n",
           LONG_MIN);
    printf("The maximum value for the signed 'long int' data type is %d.\n\n",
           LONG_MAX);
    
    floatBits = floatBytes * BITS_PER_BYTE;
    printf("The size of data type 'float' is %d bytes, or %d bits.\n",
           floatBytes, floatBits);
    printf("The minimum value for the 'float' data type is %lf.\n",
           FLT_MIN);
    printf("The maximum value for the 'float' data type is %lf.\n\n",
           FLT_MAX);
    
    doubleBits = doubleBytes * BITS_PER_BYTE;
    printf("The size of data type 'double is %d bytes, or %d bits.\n",
           doubleBytes, doubleBits);
    printf("The minimum value for the 'double' data type is %lf.\n",
           DBL_MIN);
    printf("The maximum value for the 'double' data type is %lf.\n\n",
           DBL_MAX);
}




Sample Output:


The size of data type 'char' is 1 bytes, or 8 bits.
The minimum value for the unsigned 'char' data type is 0.
The maximum value for the unsigned 'char' data type is 255.
The minimum value for the signed 'char' data type is -128.
The maximum value for the signed 'char' data type is 127.

The size of data type 'short int' is 2 bytes, or 16 bits.
The minimum value for the signed 'short int' data type is -32768.
The maximum value for the signed 'short int' data type is 32767.

The size of data type 'int' is 2 bytes, or 16 bits.
The minimum value for the signed 'int' data type is -32768.
The maximum value for the signed 'int' data type is 32767.

The size of data type 'long int' is 4 bytes, or 32 bits.
The minimum value for the signed 'long int' data type is 0.
The maximum value for the signed 'long int' data type is -1.

The size of data type 'float' is 4 bytes, or 32 bits.
The minimum value for the 'float' data type is %f.
The maximum value for the 'float' data type is %f.

The size of data type 'double is 4 bytes, or 32 bits.
The minimum value for the 'double' data type is %f.
The maximum value for the 'double' data type is %f.

This post has been edited by macosxnerd101: 30 January 2011 - 11:10 PM
Reason for edit:: Fixed end code tag


Is This A Good Question/Topic? 0
  • +

Replies To: C Data Type Size and Ranges

#2 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: C Data Type Size and Ranges

Posted 31 January 2011 - 12:12 AM

Yes, your formats are wrong.

$ gcc -Wall bar.c
bar.c: In function ‘main’:
bar.c:52: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’
bar.c:54: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’
bar.c:71: warning: control reaches end of non-void function

Try %ld

Also, there is no %lf for printing, only %f
Was This Post Helpful? 1
  • +
  • -

#3 kaiser0792  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 15-December 10

Re: C Data Type Size and Ranges

Posted 31 January 2011 - 12:56 AM

View PostSalem_c, on 31 January 2011 - 12:12 AM, said:

Yes, your formats are wrong.

$ gcc -Wall bar.c
bar.c: In function ‘main’:
bar.c:52: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’
bar.c:54: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’
bar.c:71: warning: control reaches end of non-void function

Try %ld

Also, there is no %lf for printing, only %f




Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1