Thread: Clear a doubt, Its a task given by trainer.

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    5

    Clear a doubt, Its a task given by trainer.

    Hi
    I have a question, please clear my doubt as soon as possible.

    In this code, i declared a string constant and trying to print the length of string. I know that if i write char a1[7] or char a1[] than it runs and give aggregate output, but in this case it is giving double length of string.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    void main()
    {
    char a1[6]={"Bhopal"};
    clrscr();
    printf("%d\n",strlen(a1));
    getch();
    }
    here generated output is 12

    So please tell me, what is the reason behind it.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're not providing enough space in your array for the null character ('\0'), so that char array is not even a string. "strlen()" looks for the null character to determine the end of the string. So it will continue checking characters beyond the bounds of your array until it happens to come across a byte of value '\0'. So the value of 12 you're getting is arbitrary.

    Also, the return type of "main()" should be int, not void.

    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    5
    hi Matticus,
    can u tell me, why is this program giving same result. suppose i initialize a string like

    Code:
    char a1[6]={"Bhopal"};   // here output is 12
    
    char a1[5]={"Bhopa"};   // here output is 10
    
    char a1[4]={"Bhop"};   // here output is 8
    these 12, 10, 8 is not a garbage value.
    how is compiler working for this code.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    The problem is that you are telling the compiler that you are reserving space for 6 char elements, but the string itself requres 7. You also don't need the curly brackets around the string. You should remove them.

    Code:
    //Wrong:
    char a1[6]="Bhopal";
    
    //Right:
    char a1[7]="Bhopal";
    //or
    char a1[]="Bhopal";
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Running past the bounds of your array is undefined behavior. There is no value in trying to reason about undefined behavior.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Well whenever you start doing undefined things, there's no telling what 'answers' you're going to get.

    Code:
    #include <stdio.h>
    
    void memdump ( const char *p, size_t len ) {
      char ascii[17] = { 0 };
      for ( size_t i = 0, c = 0 ; i < len ; i++ ) {
        char b = p[i];
        if ( b >= 32 && b <= 126 ) ascii[c++] = b;
        else ascii[c++] = '.';
        printf("%02x ", (unsigned char)b);
        if ( c == 16 ) {
          printf("%s\n", ascii );
          c = 0;
        }
      }
    }
    
    int main(){
      char a1[6]={"Bhopal"};   // here output is 12
      memdump(a1,16);
      char a2[5]={"Bhopa"};   // here output is 10
      memdump(a2,16);
      char a3[4]={"Bhop"};   // here output is 8
      memdump(a3,16);
      return 0;
    }
    
    $ gcc -std=c99 foo.c
    $ ./a.out 
    42 68 6f 70 61 6c 00 00 20 07 40 00 00 00 00 00 Bhopal.. .@.....
    42 68 6f 70 61 00 00 00 d0 04 40 00 00 00 00 00 Bhopa.....@.....
    42 68 6f 70 ff 7f 00 00 00 b0 7d 1e 63 89 c6 ba Bhop......}.c...
    Bhopal and Bhopa would apparently work for me, but I wouldn't call that 'success' because I know the code is wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector::clear() won't clear
    By Ducky in forum C++ Programming
    Replies: 3
    Last Post: 06-16-2013, 04:23 AM
  2. Help from the task
    By MAJA83 in forum C Programming
    Replies: 1
    Last Post: 06-06-2012, 11:35 AM
  3. Replies: 1
    Last Post: 11-02-2009, 06:45 AM
  4. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  5. Replies: 2
    Last Post: 12-31-2007, 11:40 AM

Tags for this Thread