Thread: strlen

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    1

    strlen

    hi, if i want to get the length of argv1 and 2 and compare it against a value for an if statement , why does the following not work ? if (strlen(argv[1]) && strlen(argv[2]) >info, char fullname[20]; or if (strlen(argv[1]) && strlen(argv[2])

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    The result of operator && is an int with a value of 0 or 1.
    When you do

    strlen(argv[1]) && strlen(argv[2])

    you will get, almost 100% certainly, 1, so

    if (1 > info)

    will only be true if info <= 0

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, cahce!

    It's pretty useless to fight to read code after the forum software has chewed it up. Use code tags to surround all your code.

    The advanced editor has an icon - just hilite your code and click on it, or you can add your own, if in quick reply mode.

    Code:
     if (strlen(argv[1]) && strlen(argv[2]) >info, char fullname[20]; or if (strlen(argv[1]) && strlen(argv[2])
    
    
    Best to break it up, closing the comparison after "info" ) {
       state your actions if that comparison is true
    }
    else {
       make the code here for when the comparison is false.
    }
    And "or" is not a C keyword. Maybe you want || ?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Maybe you meant:
    if (strlen(argv[1]) > info && strlen(argv[2]) > info)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another strlen
    By voidX in forum C Programming
    Replies: 15
    Last Post: 01-09-2011, 11:24 PM
  2. strlen()
    By BoneXXX in forum C Programming
    Replies: 6
    Last Post: 06-17-2007, 01:32 AM
  3. 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
  4. Strlen(...)
    By Korhedron in forum C++ Programming
    Replies: 6
    Last Post: 06-10-2003, 03:02 PM
  5. Why O why, strlen?
    By Sebastiani in forum C Programming
    Replies: 11
    Last Post: 08-24-2001, 01:41 PM