[Courses] [C] Current topic: variables in C.

Val Henson val at nmt.edu
Mon Jun 10 23:52:44 EST 2002


Another fun thing: Copy the program at the end of this email into a
file named type_size.c and compile and run it like so:

gcc -Wall -o type_size type_size.c
./type_size

(Note: The "-Wall" switch is very very useful - it complains about
things which are legal in C but almost certainly a bug.)

On my Sun Ultra10, it tells me:

Size of some C types:

char: 1
short int: 2
int: 4
long int: 4
long long int: 8
float: 4
double: 8

If you get different output, post it!

-VAL

/*
 * Print out the sizes of various types.
 * Written from scratch due to lack of web search ability just now.
 */

#include <stdio.h>

int
main (void)
{
	printf ("Size of some C types:\n\n");
	printf ("char: %d\n", sizeof(char));
	printf ("short int: %d\n", sizeof(short int));
	printf ("int: %d\n", sizeof(int));
	printf ("long int: %d\n", sizeof(long int));
	printf ("long long int: %d\n", sizeof(long long int));
	printf ("float: %d\n", sizeof(float));
	printf ("double: %d\n", sizeof(double));

	return 0;
}



More information about the Courses mailing list