7

I'm having trouble getting the atoll function to properly set a long long value in c. Here is my example:

#include <stdio.h>

int main(void) {
    char s[30] = { "115" };
    long long t = atoll(s);

    printf("Value is: %lld\n", t);

    return 0;
}

This prints: Value is: 0

This works though:

printf("Value is: %lld\n", atoll(s));

What is going on here?

Justin Brown
  • 101
  • 1
  • 1
  • 7
  • http://ideone.com/WntSUK Tried on both IDEOne and my computer, no problems? Is this all the code? – Jesus Ramos Mar 14 '13 at 19:18
  • same for me. worked well. – varnie Mar 14 '13 at 19:18
  • 3
    What compiler, which version, are there any warnings (`-Wall` in GCC). – Zeta Mar 14 '13 at 19:19
  • 5
    You're missing `#include ` so the result of `atoll` may be truncated. – Paul R Mar 14 '13 at 19:26
  • @PaulR can you explain please, this is rather interesting? – piokuc Mar 14 '13 at 19:42
  • @piokuc without a prototype, C defaults the return type to `int` (and issues a compiler warning you really should be looking for if you properly configured your compile-source statement in your Makefile), which on this system is likely smaller than `long long` (in fact, I all but guarantee it). – WhozCraig Mar 14 '13 at 19:48
  • @piokuc See meyumar's answer below for the warning generated if warnings are properly configured for your build process. – WhozCraig Mar 14 '13 at 19:51

1 Answers1

12

First, let's answer your question:

#include <stdio.h>
#include <stdlib.h>  // THIS IS WHAT YOU ARE MISSING


int main(void) {
    char s[30] = { "115" };
    long long t = atoll(s);

    printf("Value is: %lld\n", t);

    return 0;
}

Then, let's discuss and answer 'why?':

For compatibility with very old C programs (pre-C89), using a function without having declared it first only generates a warning from GCC, not an error (As pointed out by the first comment here, also implicit function declarations are allowed in C89, therefore generating an error would not be appropriate, that is another reason to why only a warning is generated). But the return type of such a function is assumed to be int (not the type specified in stdlib.h for atoll for instance), which is why the program executes unexpectedly but does not generate an error. If you compile with -Wall you will see that:

Warning: Implicit declaration of function atoll

This fact mostly shocks people when they use atof without including stdlib.h, in which case the expected double value is not returned.

NOTE: (As an answer to one of the comments of the question) This is the reason why the results of atoll might be truncated if the correct header is not included.

meyumer
  • 5,063
  • 1
  • 17
  • 21
  • Minor point - implicit declarations are fine in C89, and GCC defaults to that, so an error wouldn't be appropriate. – teppic Mar 14 '13 at 20:02
  • 1
    Thank you for the great response! I compiled with -Wall as suggested and got the warning. Including the correct library fixed the problem. – Justin Brown Mar 14 '13 at 20:23