Thread: how to check if a argument passed is integer

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    12

    how to check if a argument passed is integer

    Hi,
    how will you sheck if an argument passed through the argv[] is of type integer is their a builtin function in C

    Thanks

  2. #2
    Deleted Account
    Join Date
    Jan 2004
    Posts
    40
    All arguments passed will be character strings. So even if you called the function with:
    Code:
    %foo 42
    argv[0] will point to "42", not 42.
    Code:
    #include <stdlib.h>
    
    ...
    
    int foo = atoi(argv[0]);
    should give you an integer, or return 0 if no numbers are found.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > argv[0] will point to "42", not 42.
    That's argv[1]
    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.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    if the argument passed is "45d" atoi("45d") will change it 45. I might be able to use sscanf() to filter that out but was just wondering if their is a neater way of doing it

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use strtol() if you want maximum information back from the conversion as to whether it was successful or not
    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.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    well here is a crude way of doing it...
    I used to do it before i doscovered atoi..

    I take character by cahracter and subtract the ofset ASCII value (Difference between char "1" and number 1 in the ascii chart) and check if it was inside the number range...


    but this method is not recommended..

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but this method is not recommended..
    Nor is atoi. Use strtol instead if you want to bulletproof your validation code.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-27-2009, 02:33 PM
  2. Not working in linux...
    By mramazing in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2009, 02:18 AM
  3. allegro issues
    By mramazing in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2009, 11:56 PM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  5. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM