Thread: C integer type Size

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    3

    Question C integer type Size

    ok, im working on a program for class that takes int types in c and tells you the range and size

    here is my code, but i get like 40 errors, and the compiler is pointing to the "{" at the begining of the RANGE function. can someone help me out here, im not see the error!

    #include <stdio.h>

    void RANGE(type, name)
    {
    type minval, maxval, bit;
    unsigned bits;
    char *format;

    bits=1; bit=1;
    while (bit<<=1)
    {
    bits++;
    }

    if ((type)(1L<<(bits-1))>0)
    {
    format="%12lu";
    minval=(type)0;
    }
    else
    {
    format="%12ld";
    minval=(type)(1L<<(bits-1));
    }

    maxval=minval-1;

    printf("%20s (%2u bits): ", name, bits);
    printf(format, (long) minval);
    printf(" to ");
    printf(format, (long) maxval);
    printf("\n");
    }

    void main()
    {
    printf("\nTesting Range of Unsigned Ints... \n\n");
    RANGE(unsigned char, "unsigned char");
    RANGE(unsigned short int, "unsigned short int");
    RANGE(unsigned int, "unsigned int");
    RANGE(unsigned long int, "unsigned long int");

    printf("\nTesting Range of Signed Ints... \n\n");
    RANGE(signed char, "signed char");
    RANGE(signed short int, "signed short int");
    RANGE(signed int, "signed int");
    RANGE(signed long int, "signed long int");
    }

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Wow, this code is filled so many errors I dunno if I'd wanna touch this with my 10 foot pole.

    Here's a few comments:
    - use code tags
    - use int main, not void main
    - void RANGE(type, name), you didn't specify a type for type or name
    - type minval, maxval, bit; what's type?
    - unsigned bits; again, what type is this?
    .
    .
    .
    My initial reaction is I wouldn't want to do this program this way. Oh man, anyone wanna tackle this?
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Not quite sure what you're doing. But you perhaps can use this:

    sizeof (type) - gives the size of type in bytes

    Fill the variable of type with ones and you have the range of that type.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your whole problem stems from this:

    void main()


    Ok, maybe not, but that's something you should never do. Anyone who tells you to do that should have their kneecaps broken.

    Your whole problem stems from this:

    void RANGE(type, name)


    Didn't I just say that? Anyway, this time it's true. 'type' is not a valid type. 'name' isn't either. You have to actually specify types for your variables. You can't just declare generic non-types there. That's wrong. You'll have to actually specify the types. In other words, you'll have to redo your thought process here.

    [edit]Damn. Multiple people beat me to it.[/edit]
    [edit]
    Personally, I'd do this:

    Use a switch, have them enter a single character which represents the type you want to test, then hard code the test.
    [/edit]

    Quzah.
    Last edited by quzah; 09-26-2002 at 01:46 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    3

    more

    see, the problem is, this is the code my prof gave me

    and i dont know what its doing either
    i THINK that in class he had it as a large define statement instead of a function, but then he changed it to this


    im so lost!

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    >> see, the problem is, this is the code my prof gave me
    I am quite surprised. How did these guys become professors anyways?

    Just code it the way quzah suggested and you'll be fine.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    3

    GOT IT

    ok, i got the code to work, so if any of you would tryed to suggest help wants to see what its actually supposed to do, here it is


    #include <stdio.h>

    #define RANGE(type, name) \
    { \
    type minval, maxval, bit ; \
    unsigned bits ; \
    char *format ; \
    \
    /* Measure size of data type */ \
    bits = 1 ; bit = (type) 1 ; \
    while (bit <<= 1) bits++ ; \
    \
    /* Signed or unsigned? (2's complement!) */ \
    if ((type) (1L << (bits - 1)) > 0) \
    { \
    format = "%12lu" ; /* unsigned */ \
    minval = (type) 0 ; \
    } \
    else \
    { \
    format = "%12ld" ; /* signed */ \
    minval = (type) 1 << (bits - 1) ; \
    } \
    \
    /* Assume two's complement */ \
    maxval = minval - 1 ; \
    \
    printf("%20s (%2u bits): ", name, bits) ; \
    printf(format, (long) minval) ; \
    printf(" to ") ; \
    printf(format, (long) maxval) ; \
    printf("\n") ; \
    }

    int main()
    {
    printf("\nTesting range of UNSIGNED ints ...\n\n") ;
    RANGE(unsigned char, "unsigned char" ) ;
    RANGE(unsigned short int, "unsigned short int" ) ;
    RANGE(unsigned int, "unsigned int" ) ;
    RANGE(unsigned long int, "unsigned long int" ) ;

    printf("\nTesting range of SIGNED ints ...\n\n") ;
    RANGE(signed char, "signed char" ) ;
    RANGE(signed short int, "signed short int" ) ;
    RANGE(signed int, "signed int" ) ;
    RANGE(signed long int, "signed long int" ) ;

    return 0 ;
    }


    now, i have to change the #DEFINE to a function... shoot me!

    -[guard]
    ps: thanks for the help all

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: more

    Originally posted by [guard]
    see, the problem is, this is the code my prof gave me

    and i dont know what its doing either
    i THINK that in class he had it as a large define statement instead of a function, but then he changed it to this


    im so lost!
    Maybe he's tired, or drunk. A #define statement could possibly work. I just don't see the point.

    Code:
    int type;
    
    printf("Pick a type by choosing the correct number:\n"
    	"1 - signed char\n"
    	"2 - unsigned char\n"
    	"3 - signed short int\n"
    	"4 - unsigned short int\n"
    	"5 - signed long int\n"
    	"6 - unsigned long int\n"
    	"Your choice: " );
    
    if( scanf("%d", &type ) == 1 )
    {
    	switch( type )
    	{
    		case 1: ... do calculation; break;
    	}
    }
    else
    	printf("Input failure. Terminating.\n");
    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's a few comments:
    - use code tags
    ... and if you don't know how, see the link in my signature.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM