in dev c++ my library is missing how to install it

102 Views Asked by At

//my code is

#include <stdio.h>
int main() {
char txt[] = "xyz";
printf("%d", strlen(txt));
return 0;
}

//error is strlen is not declared in this scope

//it should work my code is correct

2

There are 2 best solutions below

0
Solomon Ucko On

https://en.cppreference.com/w/c/string/byte/strlen says:

Defined in header <string.h>

P.S. It also says that the return type is size_t, which is unsigned, and https://en.cppreference.com/w/c/io/fprintf says that the printf specifier for size_t is z, so the format string should be "%zu".

1
FredFrugal On

issues

  • You're missing the <string.h> header for strlen(). More info here.
  • The return type of strlen is that of size_t not int so use %zu for the format

fix:

#include <stdio.h>
#include <string.h>  // The header you were missing
int main(void) {
    char txt[] = "xyz";
    printf("%zu", strlen(txt));
    return 0;
}