-4
int main()
{
    int LengthofWord = strlen('word');
    printf("%s", LengthofWord);
}

For some reason this code is crashing. I want to retrieve the length of a string ( in this case string) to use later on. Why is this code crashing?

Thanks

alk
  • 69,737
  • 10
  • 105
  • 255
user2955782
  • 49
  • 1
  • 6
  • 2
    @DavidHeffernan: I suppose that's interpreted by some compilers as a multibyte literal, it should be of type `int` or something like that, and gets implicitly casted to `char *`, which tragic results. – Matteo Italia Dec 26 '13 at 18:07
  • this does not compile for a start – Ed Heal Dec 26 '13 at 18:48

3 Answers3

4

You have these problems with your code:

  • This is not valid: 'word'. The ' symbol is used for single characters, for instance 'a'. You meant to write "word".
  • The type of LengthofWord should be size_t rather than int since that is what strlen returns.
  • You used the wrong format string for printf. You used %s which is appropriate for a string. For an integral value, you should use %zu. Although, do beware that some runtimes will not understand %zu.
  • Your main signature is incorrect – it should be int main(void)

So your complete program should be:

#include <stdio.h>
#include <string.h>

int main(void)
{
    size_t LengthofWord = strlen("word");
    printf("%zu", LengthofWord);
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Why should it be size_t? What is the diffrence between size_t and int? – user2955782 Dec 26 '13 at 18:12
  • 2
    It should be `size_t` because that's what `strlen` returns. What's the difference between `size_t` and `int`. Well, they are just different things. At the very least, `size_t` is unsigned, and `int` is signed. And they may be different sizes. – David Heffernan Dec 26 '13 at 18:13
  • You can't say or rely on that `int` is `sighned` or `unsigned`. – haccks Dec 26 '13 at 18:19
  • 1
    @haccks Yes you can. int is always signed. char can be signed or unsigned, but the other basic integer types (short, int, long, long long) are always signed. – Nigel Harper Dec 26 '13 at 18:42
3

It should be

... = strlen("word");

In C string literals are put into double quotation marks.


Also strlen() returns size_t not int. So the full line shall look like this:

size_t LengthofWord = strlen("word");

Finally to print out a size_t use the conversion specifier "zu":

printf("%zu", LengthofWord);

A "%s" expects a 0-terminated array of char, a "string".


Last not least, start the program using

int main(void)
{
  ...

and finish it by returing an int

  ...
  return 0;
}
alk
  • 69,737
  • 10
  • 105
  • 255
2

There is difference between ' quote and " quote in C. For strings " is used.
Change

int LengthofWord = strlen('word');  
                          ^    ^
                          |    |
                          -----------Replace these single quotes with double quotes("")       

to

int LengthofWord = strlen("word");     

strlen returns size_t type. Declare it as size_t type instead of int. Also change the format specifier in the printf to %zu

  printf("%d", LengthofWord);  

Your final code should look like

int main(void)
{
    size_t LengthofWord = strlen('word');
    printf("%zu", LengthofWord);

    return 0;
}
Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264