1

warning: it says to change %d for %I64d

char string[DIM]="hello";

printf("%d",strlen(string));
23
  • 2
    strlen() returns size_t, use %zu as format specifier. Commented Nov 30, 2021 at 15:19
  • 1
    If the warning: too many arguments for format, then check that the code you have here is the same as the actual code you are running on your computer. Commented Nov 30, 2021 at 15:22
  • 2
    are you using an old microsoft compiler? I remember around the VS2013 days "%zu" wasn't supported, think I had to use "%Iu".. ? Commented Nov 30, 2021 at 15:25
  • 3
    there's this Commented Nov 30, 2021 at 15:29
  • 3
    @Tenko There's a general theme here, in this as well as several of your other questions: When calling printf, argument types must match their format specifiers, and doing so is your responsibility; the compiler will generally not insert any automatic conversions to match them up for you. At best the compiler will warn you about mismatches. (Modern compilers do try to warn you, and are recommended for this reason.) Commented Nov 30, 2021 at 15:46

3 Answers 3

4

As @AndreasWenzel answered "%zu" is the most most correct. But you wrote:

it outputs this warning when I put %zd warning: unknown conversion type character 'z' in format

if your implementation does not support "%z..." formats you need to convert the result of strlen to the largest possible unsigned integer.

printf( "%llu", (unsigned long long)strlen(string) );

But if your implementation does not support "%llu"

printf( "%lu", (unsigned long)strlen(string) );

And the last resort:

printf( "%u", (unsigned)strlen(string) );
Sign up to request clarification or add additional context in comments.

6 Comments

Yes printf( "%lu", (unsigned long)strlen(string) ); is the typical hack for MS (or other old) code and to maintain portability and future growth.
@chux-ReinstateMonica "%lu" is not suppered by default (you need to use special linlker options to enable it) by AVR gcc standard library. And it is why I have added it.
If using type casting in this answer, why not just use (int) to match OP question about compatibility with "%d" ?
@ryyker I assume because he is worried, quite properly, about the possibility that the OP is using strings longer than 4294967295 characters. :-)
@ryyker for type correctness.
|
2

The %d printf format specifier expects an int. However, the return value of strlen is of type size_t.

The correct printf format specifier for size_t is %zu.

This line should work:

printf( "%zu", strlen(string) );

17 Comments

it outputs this warning when I put %zd warning: unknown conversion type character 'z' in format
Re "unknown conversion type character 'z' in format", It was added to the C standard 22 years ago in C99
@Tenko: Are you using Turbo C or some other very old compiler?
@Tenko chocolate and ice cream are both sweets. But you would not be happy if you ask for the ice cream but the clerk will give you chocolate or mars bar instead. Same is with types. All are integral types, but they are diffrent
@Tenko: The reason why you don't have to distinguish between float and double when using printf (in contrast to scanf) is that the float parameter gets promoted to a double, because printf is a variadic function. However, int does not get promoted to size_t.
|
2

strlen doesn't return int. But your printf formats and their arguments must match. So just say

printf("%d", (int)strlen(string));

As long as your string is never longer than 32767 characters, you'll be fine.

The preferred solution is

printf("%zu", strlen(string));

but it sounds like your antique compiler doesn't support %zu. (You have my sympathies: I had a compiler at work for a long time that had the same problem.)

If you're worried about strings longer than 32767 characters, see the other answers, or this question: How can one print a size_t variable portably using the printf family?

12 Comments

Why to use signed format for unsigned number?
I like this method, but what is the unit for strlen? I thought it was int
@Tenko Read the documentation: strlen returns size_t.
@Tenko did you read other answers? Andreas has explained what is the return type of strlen
@0___________ Because (int) is considerably easier to type than (unsigned). (And for reasonable-length strings, it won't make a difference.)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.