[TynesideLUG] Getting type sizes

Ian Bruntlett ian.bruntlett at gmail.com
Wed Jan 27 20:16:19 UTC 2021


// list-sizeofs.c Ian Bruntlett 22nd January 2021
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h> // bool
#include <limits.h> // CHAR_BIT
void print_size(char *pTypeName, size_t TypeSize);

void print_size(char *pTypeName, size_t TypeSize)
{
  printf ("Size of %s is %zu\n", pTypeName, TypeSize);
}

typedef struct {
  char c[10]; // 10 bytes
  int i;      //  4 bytes
  double d;   //  8 bytes
} widget;

typedef struct {
  size_t num;
  int data[];
} widget1;


int main(void)
{
  printf("CHAR_BIT = %d\n", CHAR_BIT );
  print_size("bool", sizeof(bool));
  print_size("char", sizeof(char));
  print_size("int", sizeof(int));
  print_size("long", sizeof(long));
  print_size("size_t", sizeof(size_t));
  print_size("float", sizeof(float));
  print_size("double", sizeof(double));
  print_size("widget", sizeof(widget));
  print_size("widget1", sizeof(widget1));
  return EXIT_SUCCESS;
}


-- 
-- ACCU - Professionalism in programming - http://www.accu.org
-- My writing - https://sites.google.com/site/ianbruntlett/
-- Free Software page -
https://sites.google.com/site/ianbruntlett/home/free-software


More information about the Tyneside mailing list