C – Register Char Different Size on Windows and Mac


I have a C program I need to run on both Windows and Mac. Since the code is already working on Windows I prefer to change the Mac "version" to function the same as Windows and leave the code on Windows as is. I have been reading into this and I have already discovered that yes data types have different sizes on different machines/compilers and this is nothing new. Additionally I have seen plenty of questions on purpose the register keyword and I am not specifically asking about that but it may be relevant to my issue.

My issue is that I have a few variables defined as register char that are strings read from a file and the issue becomes that sizeof() on the register char variables is 4 on Windows and 8 on Mac and other parts of the program do not function correctly because of this. Unfortunately the program is rather complex so I think my best option is to fix this issue rather than rewrite other parts of the program to accommodate the difference in size for the register char variables.

So what I am wondering is can I switch the data type from register char to something else if I can find something "equivalent" that uses 4 bytes instead of 8, is there some way I can keep it as is and convert from 8 bytes to 4, or is the best option to rework the rest of the program to accept a value that is either 4 or 8 bytes?

It may also be important to mention this code is in s_copy.c from libf2c which is part of f2c:

void s_copy(register char *a, register char *b, ftnlen la, ftnlen lb)
    register char *aend, *bend;

    aend = a + la;
    printf("a: -%lu- %d\n",sizeof(a),__LINE__);
    printf("b: -%lu- %d\n",sizeof(b),__LINE__);


When those print statements are executed on Windows the result printed is 4, on Mac it is 8.

>Solution :

a and b are pointers. You are getting the size of pointers. (Well, different type of pointers can have different sizes, but not on systems running Windows and MacOS.)

Your Mac environment is using 64-bit pointers.

Your Windows environment is using 32-bit pointers. This means you are compiling for the x86. If you were to compile the program for the x86-64, you would be using 64-bit pointers there too.

But is the difference in the size of the pointers really the issue? I think the problem is simply that you are using sizeof(a) when you should be using sizeof(*a). But since *a is a char, that’s guaranteed to be 1.

Leave a ReplyCancel reply