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 ?
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 ?
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
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.
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.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.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
>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().
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.
A char is usually defined as unsigned by default, so it's range will be 0-255.Originally posted by shaik786
A variable declared as char is capable of storing any integer between the values: -128 to +127
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.
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
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.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; }
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]
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.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
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.
char without any prefix being signed or unsigned will be defined by the compiler. Here, in my compiler, by default it is signed!