-1

I'm trying to use atoll to convert a string of numbers with spaces. I want to make a loop in C that will only take positive numbers.

So far, from what I understand, atoll on a char would return 0, likewise with a combination of an int and char, such as "3a". However, is there a range of values for negative numbers for atoll on a negative number?

I found that the result of -1 is 18446744073709551615, -2 is 18446744073709551614, -3 is 18446744073709551613, etc, based on my first print statement.

for(arg = 1; arg < argc; ++arg)
{
   printf("%llu\n", atoll(argv[arg]));

   if(atoll(argv[arg]) == 0 || atoll(argv[arg]) >= xxxxx))
   {
     printf("Enter a valid number.");
   }
   else
   {
     printf("irrelevant code here");
   }
}
Jamie L.
  • 49
  • 2
  • 7
  • This shouldn't compile. `atoll(x >= y)` is not a valid call. Do you mean `atoll(x) >= y`? – tadman Feb 11 '19 at 23:40
  • Are your valid input numbers guaranteed to lie in some range? – Alex Feb 11 '19 at 23:41
  • 1
    @tadman yes, sorry. in that case, y is to be where the lower bound of negative numbers atoll would be. – Jamie L. Feb 11 '19 at 23:44
  • 1
    "I found that..." Those results are not correct, but some incorrect ways of printing or comparing numbers might make it seem like they are. Please paste a [mcve] showing why you thought that was happening. – aschepler Feb 11 '19 at 23:44
  • @alex I wanted positive numbers only for this loop. – Jamie L. Feb 11 '19 at 23:44
  • @aschepler kindly view update – Jamie L. Feb 11 '19 at 23:49
  • 1
    If you're working with *unsigned* values you can't even talk about negative numbers, they don't exist. If you're talking about signed values you can deal with negative values, but `%llu` is explicitly unsigned. Use `%lld` in that case. – tadman Feb 11 '19 at 23:51
  • @tadman Oh I see, that makes sense now. In that case, what was the print `%llu` statement representing with that output? – Jamie L. Feb 11 '19 at 23:55
  • It was converting to unsigned which for negative values exposes [Two's Complement](https://en.wikipedia.org/wiki/Two's_complement). – tadman Feb 11 '19 at 23:58
  • 18446744073709551615 is 2^64 - 1. Look up "two's complement arithmetic". – Nate Eldredge Feb 11 '19 at 23:58
  • [using the wrong format specifier invokes UB](https://stackoverflow.com/q/16864552/995714). And [don't use `atoll`](https://stackoverflow.com/q/17710018/995714) – phuclv Feb 12 '19 at 00:12
  • Jamie L., A subtle important aspect of this coding goal is "Is the value of xxxxx representable in the long long range?" If xxxxx is larger than `LONG_MAX`, than the problem becomes harder . There is more at play than just a mis-match printf specifier. What is the type of xxxxx and its potential maximum value? – chux - Reinstate Monica Feb 12 '19 at 00:36

1 Answers1

1

From your code fragment it is not possible to figure out several important things. Did you include correct headers, did you use correct format specifiers and length modifiers in printf? In example bellow you can find correct includes an correct format for long long.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("%lld\n", atoll("-1"));
    return 0;
}

/* this example prints correct output of -1 */

risbo
  • 188
  • 7
  • Actually, it was because I was using `%llu`, so it was printing output that I expected from `%lld` instead. – Jamie L. Feb 12 '19 at 00:08