how to check if a argument passed is integer

This is a discussion on how to check if a argument passed is integer within the C Programming forums, part of the General Programming Boards category; Hi, how will you sheck if an argument passed through the argv[] is of type integer is their a builtin ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,796
    >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, 01:33 PM
  2. Not working in linux...
    By mramazing in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2009, 01:18 AM
  3. allegro issues
    By mramazing in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2009, 10: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, 07:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21