Search code examples
clinuxcommandsizeofnano

Errors compiling C code with sizeof from command line


I wrote the following code in Nano from the Linux command line to get errors when compiling:

Warnings when compiling

I'd like to know what I need to change in my code to make it compile properly. I am trying to get the number of bits in each data type listed to print on a single line.

#include<stdio.h>

int main(void){
    char A;
    unsigned char B;
    int a;
    unsigned int b;
    long c;
    unsigned long d;
    float e;
    double f;
    long double g;

    printf(
        "%c %c %i %u %li %lu %f %lf %Lf\n",
         sizeof(char), sizeof(unsigned char),
         sizeof(int), sizeof(unsigned int),
         sizeof(long), sizeof(unsigned long),
         sizeof(float), sizeof(double), sizeof(long double)
    );

    return 0;
}

Solution

  • The sizeof operator returns an integer of type size_t, so you should use the appropriate printf format specifier ("%zu") (assuming C99):

    printf(
        "%zu %zu %zu %zu %zu %zu %zu %zu %zu\n",
         sizeof(char), sizeof(unsigned char),
         sizeof(int), sizeof(unsigned int),
         sizeof(long), sizeof(unsigned long),
         sizeof(float), sizeof(double), sizeof(long double)
    );
    

    However, this prints the number of bytes in each type. If you want the number of bits in each type, include <limits.h> and multiply each result by CHAR_BIT to get that:

    #include <limits.h>
    
    /* ... */
    
    printf(
        "%zu %zu %zu %zu %zu %zu %zu %zu %zu\n",
         sizeof(char) * CHAR_BIT, sizeof(unsigned char) * CHAR_BIT,
         sizeof(int) * CHAR_BIT, sizeof(unsigned int) * CHAR_BIT,
         sizeof(long) * CHAR_BIT, sizeof(unsigned long) * CHAR_BIT,
         sizeof(float) * CHAR_BIT, sizeof(double) * CHAR_BIT, sizeof(long double) * CHAR_BIT
    );
    

    IMO, it would look much clearer if you label what you're printing and print each value on its own line, like this:

    printf("Number of bits in char = %zu\n", sizeof(char) * CHAR_BIT);
    printf("Number of bits in unsigned char = %zu\n", sizeof(unsigned char) * CHAR_BIT);
    printf("Number of bits in int = %zu\n", sizeof(int) * CHAR_BIT);
    printf("Number of bits in unsigned int = %zu\n", sizeof(unsigned int) * CHAR_BIT);
    printf("Number of bits in long = %zu\n", sizeof(long) * CHAR_BIT);
    printf("Number of bits in unsigned long = %zu\n", sizeof(unsigned long) * CHAR_BIT);
    printf("Number of bits in float = %zu\n", sizeof(float) * CHAR_BIT);
    printf("Number of bits in double = %zu\n", sizeof(double) * CHAR_BIT);
    printf("Number of bits in long double = %zu\n", sizeof(long double) * CHAR_BIT);
    

    And that can be reduced with a macro (though macros aren't the best, they are useful for repetitive code):

    #define PRINT_BITS_IN_TYPE(type) \
        printf("Number of bits in " #type " = %zu\n", sizeof(type) * CHAR_BIT)
    
    PRINT_BITS_IN_TYPE(char);
    PRINT_BITS_IN_TYPE(unsigned char);
    PRINT_BITS_IN_TYPE(int);
    PRINT_BITS_IN_TYPE(unsigned int);
    PRINT_BITS_IN_TYPE(long);
    PRINT_BITS_IN_TYPE(unsigned long);
    PRINT_BITS_IN_TYPE(float);
    PRINT_BITS_IN_TYPE(double);
    PRINT_BITS_IN_TYPE(long double);