3

When i run this code i m getting output as 4. But my string length is 3 why it is giving 4??

#include <stdio.h>

 int main(void)
 {

    printf("%d",sizeof("abc"));
    return 0;
 }
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
sam1006
  • 103
  • 1
  • 8
  • 1
    `sizeof` tells you how much space your data item (a string in this case) takes, which includes the null terminator for the string. If you want the length of the string, use `strlen("abc")`. – lurker Mar 13 '17 at 17:28
  • Ohkay got it... – sam1006 Mar 13 '17 at 17:29

4 Answers4

5

Character strings in C include a null terminator, so the string "abc" actually occupies 4 bytes.

Jeff Loughlin
  • 4,134
  • 2
  • 30
  • 47
  • It means it is no use of sizeof operator is used in that way – sam1006 Mar 13 '17 at 17:28
  • @Shubham correct, it's of no use to use `sizeof` in a way that isn't according to its defined meaning. – lurker Mar 13 '17 at 17:31
  • 2
    @lurker - The sizeof operator on a character array returns the size of the array - which might be different from the length of the string - but this is a perfectly legal and valid use of sizeof. – Jeff Loughlin Mar 13 '17 at 17:31
  • @jeff ya it will be legal, but the way i am using or i am trying to get the length using this is wrong that's the mistake what i am doing... Actually this doubt clears my small but effective concept. – sam1006 Mar 13 '17 at 17:34
  • why is it 'no use', you can simply subtract one. However that only works in some cases of c strings. The better solution is strlen – pm100 Mar 13 '17 at 17:35
  • @pm100 hehehe..sure but see na i am not actually doing what that function was meant for – sam1006 Mar 13 '17 at 17:38
  • A program may wish to clear the string using memset(s, 0, sizeof(s)); This is a valid use of sizeof. – Robert Jacobs Mar 13 '17 at 17:52
  • 1
    @JeffLoughlin I was just indicating that `sizeof` isn't a string length function, but a memory usage function. Different meanings. Sure, it can be used to obtain the length of the string if one subtracts 1. – lurker Mar 13 '17 at 18:04
4

The type of the string literal "abc" is char [4]. It contains the characters a, b, and c, as well as a terminating null byte. The value you're getting back reflects this.

If you want the length of the string, use strlen.

printf("%zu\n", strlen("abc"));

Note the use of the %zu format specifier, which expects a parameter of type size_t, as returned by strlen (and by sizeof). Using the wrong format specifier invokes undefined behavior.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • 3
    String literals are not `const` qualified (though they are read only, i.e. changing their contents is undefined behaviour). Their type is `char[n]` with `n` being large enough for the string and a terminating zero byte. The type of e.g. `"abcd"` is `char[5]`. *Things may be different in that other language: C++.* – pmg Mar 13 '17 at 18:53
  • @pmg Thanks. Removed the `const` reference. – dbush Mar 13 '17 at 19:17
2

Actually you might get undefined behavior because the sizeof' operator yields a result of type size_t, and %d isn't necessarily the right format specifier.

Why 4? Because the initialiser is a string literal, it is a string, and does have a null terminator, if it ever exists as an object in memory.

msc
  • 33,420
  • 29
  • 119
  • 214
  • No i am not getting any undefined behaviour..it is working good..why it will give undefined behaviour? Sizeof operator returns integer value na?? – sam1006 Mar 13 '17 at 17:53
  • No, sizeof return size_t and correct format specifier is %zu. – msc Mar 13 '17 at 17:55
  • Refer http://stackoverflow.com/questions/2524611/how-can-one-print-a-size-t-variable-portably-using-the-printf-family – msc Mar 13 '17 at 17:56
  • 2
    @Shubham What makes you say you are "not getting any undefined behaviour". Undefined behaviour is undefined; in the worst cases, it can result in you seeing what you expected (until you take your code somewhere else). – Toby Speight Mar 13 '17 at 18:05
  • @toby Sorry its my mistake..yaa sure it will effect my code in long run... – sam1006 Mar 13 '17 at 18:09
0

For strings in C I would recommend using the strlen(str1). It returns the length of the string WITHOUT the null termination character.

size_t strlen(const char *str)
Jonaswg
  • 394
  • 2
  • 12