Thread: size and length of the string doubt

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    size and length of the string doubt

    Hi all,

    i have the following program
    Code:
    #include <stdio.h>  
    main()
      { 
     char str[]="S\065AB";
      printf("\n%d", sizeof(str));  
     }
    the actual answer is 5. but from my calculation it should be 7 if i consider \0 as null as a single character and one for the last null character. Where i am going wrong somebody please show the concept to me, also if i apply a strlen(str) that also giving me not the expected result. how to decode this?

    thanks in advance,
    regards,
    satya

  2. #2
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    "sizeof" is used to get the size of a data type, not a string. You should indeed use strlen(), which gives you the number of character in the string, excluding the null termination char '\0'.

    Also, I'm not sure what you mean by "the last null character". The last character of a well formatted C string is '\0'.

    So if you want the size of the string, including the last '\0', you should use "strlen() + 1".

    Hope that helps.

    I did not notice that you wanted to put \0 in your string directly. You cannot do that. In C string, the '\' character is an escape character. When writing "\065", it means "character with octal value 065" (see : C syntax - Wikipedia, the free encyclopedia).
    Last edited by Tibo-88; 02-23-2012 at 07:20 AM.
    HomePort : A C Web Service API for heterogeneous home automation systems

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    great many thanks i understood the concept. actually the question was one of the online problems i was trying to solve.

    thanks and regards,
    satya

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yes you can use sizeof. It gives the hard-coded size of the object.
    str[0] is 'S'
    str[1] is '5' (digit '5' from octal 065)
    str[2] is 'A'
    str[3] is 'B'
    str[4] is '\0' (null character)

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Tibo-88 View Post
    I did not notice that you wanted to put \0 in your string directly. You cannot do that. In C string, the '\' character is an escape character. When writing "\065", it means "character with octal value 065" (see : C syntax - Wikipedia, the free encyclopedia).
    Even if he did want to embed \0 in the middle of a series of characters, he would still have a string that ended on the first \0 encountered (ignoring the octal thing for a moment). So strlen would have given him 1.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    @nonoob : thanks for clarifying. But I might add that I would not advice taking the habit of using sizeof for computing string length, because when you switch to dynamically allocated strings, then it is not true anymore.

    @quzah : Yeah I didn't express myself right (once again), thanks also. It will actually only be taken as octal number if three decimals are following the "\".
    Last edited by Tibo-88; 02-23-2012 at 01:02 PM.
    HomePort : A C Web Service API for heterogeneous home automation systems

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 20
    Last Post: 03-08-2011, 05:16 PM
  2. doubt in C--(reducing the length of code)
    By nardneran in forum C Programming
    Replies: 44
    Last Post: 10-15-2006, 02:38 PM
  3. std::string length
    By FoodDude in forum C++ Programming
    Replies: 4
    Last Post: 08-26-2005, 07:57 AM
  4. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 05:50 PM
  5. Length of a string
    By Munkey01 in forum C++ Programming
    Replies: 6
    Last Post: 07-06-2003, 03:26 PM