Typesize: View the Size of each C-Data-type

This topic was published by and viewed 1661 times since "". The last page revision was "".

Viewing 1 post (of 1 total)
  • Author
    Posts

  • DevynCJohnson
    Keymaster
    • Topics - 437
    • @devyncjohnson

    Typesize is a small command-line program that displays the size (in bytes) of several data-types used in the C programming language. To use the program, in a terminal, type "/PATH/TO/typesize". Typesize is licensed under LGPLv3 and is made by Devyn Collier Johnson <[email protected]>.

    NOTE: Typesize has been merged with "Linux-Utils" (https://dcjtech.info/topic/linux-utils-software-collection-for-linux/). Updates will be released with Linux-Utils rather than this page.

    To compile the C-source-code, execute one of the below listed commands. Suggestions for other compiler commands are welcome.

    • General (GNU GCC) - gcc -O3 -gtoggle -g0 -s -pipe -pedantic -std=c11 ./typesize.c -o ./typesize
    • Optimized (GNU GCC) - gcc -O3 -Wl,-O3 -gtoggle -g0 -s -pipe -funroll-loops -ffunction-sections -fdata-sections -pedantic -Wall -std=c11 ./typesize.c -o ./typesize && strip --remove-section=.note --remove-section=.comment --strip-debug --strip-unneeded ./typesize

    Please feel free to email me suggestions, bug reports, concerns, etc. Also, please consider donating to DCJTech for further development.

    [paypal-donation]

    Download Version 2015.11.15

    https://dcjtech.info/wp-content/uploads/2015/11/typesize.c.tar.gz

    Source Code

    /*
    Created by Devyn Collier Johnson
    <[email protected]>
    LGPLv3
    
    GNU Lesser General Public License v3
    Copyright (c) Devyn Collier Johnson, All rights reserved.
    
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3.0 of the License, or (at your option) any later version.
    
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library.
    */
    
    // Version 2015.11.15
    
    // gcc -O3 -gtoggle -g0 -s -pipe -pedantic -std=c11 ./typesize.c -o ./typesize
    // gcc -march=haswell -O3 -Wl,-O3 -gtoggle -g0 -s -pipe -funroll-loops -ffunction-sections -fdata-sections -pedantic -Wall -Wextra -std=c11 ./typesize.c -o ./typesize && strip --remove-section=.note --remove-section=.comment --strip-debug --strip-unneeded ./typesize
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    
    #if defined(__linux__)  // Linux
    #   include <unistd.h>
    #   if !defined(_GNU_SOURCE)
    #      define _GNU_SOURCE
    #   endif
    #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))  // Unix-systems
    #   define _POSIX_C_SOURCE = 199309L
    #   include <unistd.h>
    #   if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))  // Unix-systems
    #       include <sys/param.h>
    #           if defined(BSD)  // BSD
    #               define _XOPEN_SOURCE_EXTENDED
    #           endif
    #   endif
    #endif
    
    
    // Print the size of each datatype
    int main(void) {
        // Get Datatype Size
        const size_t charsize = sizeof(char);
        const size_t signedcharsize = sizeof(signed char);
        const size_t unsignedcharsize = sizeof(unsigned char);
        const size_t intsize = sizeof(int);
        const size_t signedsize = sizeof(signed);
        const size_t signedintsize = sizeof(signed int);
        const size_t unsignedsize = sizeof(unsigned);
        const size_t unsignedintsize = sizeof(unsigned int);
        const size_t shortsize = sizeof(short);
        const size_t shortintsize = sizeof(short int);
        const size_t signedshortsize = sizeof(signed short);
        const size_t signedshortintsize = sizeof(signed short int);
        const size_t unsignedshortsize = sizeof(unsigned short);
        const size_t unsignedshortintsize = sizeof(unsigned short int);
        const size_t longsize = sizeof(long);
        const size_t longintsize = sizeof(long int);
        const size_t signedlongsize = sizeof(signed long);
        const size_t signedlongintsize = sizeof(signed long int);
        const size_t unsignedlongsize = sizeof(unsigned long);
        const size_t unsignedlongintsize = sizeof(unsigned long int);
        const size_t longlongsize = sizeof(long long);
        const size_t longlongintsize = sizeof(long long int);
        const size_t signedlonglongsize = sizeof(signed long long);
        const size_t signedlonglongintsize = sizeof(signed long long int);
        const size_t unsignedlonglongsize = sizeof(unsigned long long);
        const size_t unsignedlonglongintsize = sizeof(unsigned long long int);
        const size_t floatsize = sizeof(float);
        const size_t doublesize = sizeof(double);
        const size_t longdoublesize = sizeof(long double);
        const size_t floatcomplexsize = sizeof(float _Complex);
        const size_t doublecomplexsize = sizeof(double _Complex);
        const size_t longdoublecomplexsize = sizeof(long double _Complex);
        const size_t boolsize = sizeof(_Bool);
        const size_t sizetsize = sizeof(size_t);
        #if defined(_POSIX_VERSION)
        const size_t ssizetsize = sizeof(ssize_t);
        #endif
        const size_t ptrsize = sizeof(char*);
        const size_t adrsize = sizeof(&ptrsize);
        // Display Sizes
        printf("Char: %lu\n", charsize);
        printf("Signed Char: %lu\n", signedcharsize);
        printf("Unsigned Char: %lu\n", unsignedcharsize);
        printf("Int: %lu\n", intsize);
        printf("Signed: %lu\n", signedsize);
        printf("Signed Int: %lu\n", signedintsize);
        printf("Unsigned: %lu\n", unsignedsize);
        printf("Unsigned Int: %lu\n", unsignedintsize);
        printf("Short: %lu\n", shortsize);
        printf("Short Int: %lu\n", shortintsize);
        printf("Signed Short: %lu\n", signedshortsize);
        printf("Signed Short Int: %lu\n", signedshortintsize);
        printf("Unsigned Short: %lu\n", unsignedshortsize);
        printf("Unsigned Short Int: %lu\n", unsignedshortintsize);
        printf("Long: %lu\n", longsize);
        printf("Long Int: %lu\n", longintsize);
        printf("Signed Long: %lu\n", signedlongsize);
        printf("Signed Long Int: %lu\n", signedlongintsize);
        printf("Unsigned Long: %lu\n", unsignedlongsize);
        printf("Unsigned Long Int: %lu\n", unsignedlongintsize);
        printf("Long Long: %lu\n", longlongsize);
        printf("Long Long Int: %lu\n", longlongintsize);
        printf("Signed Long Long: %lu\n", signedlonglongsize);
        printf("Signed Long Long Int: %lu\n", signedlonglongintsize);
        printf("Unsigned Long Long: %lu\n", unsignedlonglongsize);
        printf("Unsigned Long Long Int: %lu\n", unsignedlonglongintsize);
        printf("Float: %lu\n", floatsize);
        printf("Double: %lu\n", doublesize);
        printf("Long Double: %lu\n", longdoublesize);
        printf("Float Complex: %lu\n", floatcomplexsize);
        printf("Double Complex: %lu\n", doublecomplexsize);
        printf("Long Double Complex: %lu\n", longdoublecomplexsize);
        printf("_Bool: %lu\n", boolsize);
        printf("size_t: %lu\n", sizetsize);
        #if defined(_POSIX_VERSION)
        printf("ssize_t: %lu\n", ssizetsize);
        #endif
        printf("Char*: %lu\n", ptrsize);
        printf("&adrsize: %lu\n", adrsize);
        return 0;
    }
Viewing 1 post (of 1 total)