Thread: wierd behaviour of strlen

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    2

    wierd behaviour of strlen

    when I try this code:
    Code:
             
    
    void main()
    {
             int l;
    
             char a[3]={'d','b','c'};
    
             l=strlen(a);
    
    printf("%d",l);
    
    }
    
    I am getting o/p as 4.
    
    
    void main()
    {
             int l;
    
             char a[4]={'d','b','c'};
    
             l=strlen(a);
    
    printf("%d",l);
    
    }
    
    o/p is 3.
    can anyone explain this odd behavior?

    I am using turboc3 in winxp

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The weird thing is that you are using strlen on an array of char that does not have a null character, and thus is not considered a string with respect to strlen.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Turbo C is very old.
    Why don't you use something like visual C++ or gcc ?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    ...And changing void main to int main? SourceForge.net: Revision history of "Void main" - cpwiki
    Or rather you should get rid of Turbo C: SourceForge.net: Turbo C - cpwiki
    And get a better IDE/compiler: SourceForge.net: Integrated Development Environment - cpwiki
    Last edited by Elysia; 07-18-2010 at 09:05 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    74
    You need to add a '\0' to be considered a string.

    An easier way to do this is by initializing like this,
    Code:
    char a[] = "abc";
    or
    char a[4] = "abc";

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you don't intend to leave space for the null terminator, then you probably intend to use sizeof instead of strlen.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    And Welcome to the forum, saurabhsnha.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    2
    Thanks a lot!!!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL Wierd **** - Order of Drawing Stuff?
    By Tonto in forum Game Programming
    Replies: 9
    Last Post: 11-09-2006, 09:56 PM
  2. strlen in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 10:28 AM
  3. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  4. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM
  5. String overflow behaviour
    By Morgan in forum C Programming
    Replies: 15
    Last Post: 10-10-2003, 02:37 AM