Why can I use size_t without defining it?

504 Views Asked by At
int main(void)
{
    size_t a = 20;
    unsigned int b = 0;

    b = a;

    return 0;
}

no header files are included.

i wrote this in cmd

clang -std=c89 -W -Wall -pedantic-errors *.c

why does it compile and run without problems?

I am using clang 13.0.1. (LLVM-13.0.1-win32.exe)

enter image description here

linker is using VS enter image description here

i don't know where to put this "#error Compiled indeed" enter image description here

enter image description here

1

There are 1 best solutions below

2
chqrlie On BEST ANSWER

The behavior you describe does not happen with clang on linux systems, but it does with the x64 version of MSVC as can be verified on the Godbolt compiler explorer.

It looks like size_t is a built-in type for this compiler.

The compiler accepts this built-in to be redefined as long as the definition is

typedef unsigned long long size_t;

Anything else causes an error. As commented by Jonathan Leffler, C11 and later allows you to define a typedef more than once as long as the definitions are the same (and not for a variably-modified type).