Reputation: 1
#include <unistd.h>
#include <stdlib.h>
void func_putchar(char c)
{
write(1, &c, 1);
}
void func_putchar(char c);
void func_is_negative(char *n){
if (n >= 0)
{
func_putchar('P');
}
else
{
func_putchar('N');
}
}
int main ( int argc, char *argv[] ){
char n = atoi (argv[1]);
func_is_negative(&n);
}
I tried inserting a number through terminal, however each time I get a random result, it's either positive or negative. I think the code doesn't check the number submitted or translates it badly with atoi()
Upvotes: -1
Views: 188
Reputation: 36
You do stange things in my point of view,
Why char n = atoi(argv[1])
, better use int n = atoi(argv[1])
because atoi return a int or a long.
After, I don't think that you need a pointer of n , func_is_negative(n);
looks better for your case.
so replace the prototype of your functions too, void func_is_negative(int n)
looks better cause you are using integers.
If I don't say mess, when you do : if (n >= 0)
whith char *n
, you are comparing a pointer (char *) whith 0 and the value of a pointer can't be 0 or less
Upvotes: 2
Reputation: 17454
You're checking whether the pointer to the char is greater than or equal to 0
. After sign conversion, this may or may not be the case at any given time.
Check *n
, not n
. Or don't pass a pointer in in the first place (there's no need to).
You may also wish to consider using int
rather than char
, as your input is quite constrained at the moment and prone to overflows.
Upvotes: 1