Lars Nyström
Lars Nyström

Reputation: 6373

How do I initialize literal to size_t in C?

I have the same question as this one, except I'm writing C.

How can I initialize a literal number to have the type size_t?

Basically, I have a macro that amounts to this:

#define myprint(S) { printf("hello %zu", S); }

and I would like to use it like this:

myprint(0);

But I'm getting the message:

format '%zu' expects argument of type 'size_t', but argument has type 'int'

I've tried writing 0lu, but it does not work for all architectures.

Upvotes: 3

Views: 1359

Answers (1)

dbush
dbush

Reputation: 223872

There's no suffix specific to size_t. You'll need to use a cast instead.

#define myprint(S) { printf("hello %zu", (size_t)S); }

Given how this is used, it's the best way to do it anyway.

Upvotes: 5

Related Questions