Thread: How do we cast an argv[]

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    28

    How do we cast an argv[]

    Hi everyone,

    I would like to know how do we cast an argv[] if I want to check
    whether it is equal to some integer number or decimal in C ?

    another question what is the difference between unsigned char and char ?

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    To turn a string like argv[0] or argv[x] into a integer you can use atoi(String). Use atof if you want to convert a string to a float.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    A variable declared as char is capable of storing any integer between the values: -128 to +127
    While, a variable declared as unsigned char is capable of storing any integer between the values: 0 to 255
    This kind of working is due to the fact that a 'char' consists of 8 BITS (1 BYTE), the Most Significant Bit (MSB) of which is used to represent it's sign (+ or -).
    In the case of a 'char', when this bit is set to '1', the number is assumed to be a negative number, and when '0', is assumed to be a positive number.
    But in the case of 'unsigned char', the MSB is not used for terming the number a -ve or a +ve, instead, the number is presumed always to be a +ve integer.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by sean345
    To turn a string like argv[0] or argv[x] into a integer you can use atoi(String). Use atof if you want to convert a string to a float.
    Just to be pedantic , it's pretty unlikely that you'll want to convert argv[0] to an int, as it's the program name, not an argument.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >I would like to know how do we cast an argv[] if I want to check
    >whether it is equal to some integer number or decimal in C ?

    Note that argv is the array containing commands on the argument line and the program name. The elements of argv are strings, so I assume you want to convert the strings to int. For that you use atoi().

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    A char variable can be either unsigned or signed depending on the compiler. So if it's signed the range is
    -128 to 127 but if unsigned the range is 0-255.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by shaik786
    A variable declared as char is capable of storing any integer between the values: -128 to +127
    A char is usually defined as unsigned by default, so it's range will be 0-255.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    No, a char can be either signed or unsigned. With both compilers I use, gcc and borland, and the char is signed. You can test by running
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            char c = 128;
            unsigned char uc = 128;
            signed char sc = 128;
    
            printf("c = %d\n", c);
            printf("uc = %d\n", uc);
            printf("sc = %d\n", sc);
    
            return 0;
    }
    Since just about all machines are 2 complement, if the char is signed then the 128 overflows and you get an output of c = -128.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    K&R says a char being signed or unsigned is machine-dependant. I tend to believe it
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Nick
    No, a char can be either signed or unsigned. With both compilers I use, gcc and borland, and the char is signed. You can test by running
    I know that. I said that a char (notice no prefix) is usually defined as unsigned, if no predefiner is specified. I didn't say that you cannot define signed char's.
    Might be me being too unclear in my post though .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    char without any prefix being signed or unsigned will be defined by the compiler. Here, in my compiler, by default it is signed!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 03:32 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 02:46 PM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM