Thread: typecase strlen set to variable--bad

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

    typecase strlen set to variable--bad

    In this simple code fragment, the first printf displays 32767, but the second displays the correct value for the length of theName.

    Why?

    char theName[50];
    int theLength;

    printf("enter your name: ");
    scanf("%s", theName);
    theLength==(int)strlen(theName);

    printf("your name is %s , and it is %d characters long.\n",theName,theLength);
    printf("your name is %s , and it is %d characters long.\n",theName,(int)strlen(theName));

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by bcole View Post
    In this simple code fragment, the first printf displays 32767, but the second displays the correct value for the length of theName.

    Why?

    char theName[50];
    int theLength;

    printf("enter your name: ");
    scanf("%s", theName);
    theLength==(int)strlen(theName);

    printf("your name is %s , and it is %d characters long.\n",theName,theLength);
    printf("your name is %s , and it is %d characters long.\n",theName,(int)strlen(theName));
    shouldn't this be "=" ?

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    2
    Duh. Of course! Thank you. I'm more competent in some other languages. It's humbling to be a beginner again...

  4. #4
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    Quote Originally Posted by bcole View Post
    Duh. Of course! Thank you. I'm more competent in some other languages. It's humbling to be a beginner again...
    You're new here & welcome! In the future, you'll get more replies if you
    use code tags. :-)

    Be sure to read the FAQs here and there is another link that is
    on how to ask questions.

    EDIT:
    http://www.catb.org/~esr/faqs/smart-questions.html
    Last edited by Char*Pntr; 09-11-2010 at 12:22 PM. Reason: include question link

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that strlen returns size_t which is not the same as int.
    Also note that you should not read strings with scanf: SourceForge.net: Scanf woes - cpwiki
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment problem
    By SterlingM in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2009, 09:43 AM
  2. *** how to set up a variable in Linux? ***
    By meili100 in forum Linux Programming
    Replies: 5
    Last Post: 10-05-2007, 04:34 PM
  3. Bad Habbit? (variable management)
    By Taikon in forum C Programming
    Replies: 3
    Last Post: 06-30-2005, 04:30 PM
  4. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM

Tags for this Thread