1
#include <stdio.h>

int main(void) 
{
    long long x = test();
    printf("%lld\n", x);

    return 1;
}

long long test()
{   
    return 1111111111111111111;
} 

The output is 734294471 . If I replace the call to test() by a the number, the output is as I expect. I checked the value of x using a debugger and it wasn't set the to value returned by the function. What is going wrong?

I am using Visual Studio 2010 with the Visual C++ compiler.

Pulkit Sinha
  • 2,654
  • 4
  • 19
  • 20
  • 1
    Your IDE is not relevant for this sort of question, and `` is not a C++ header - if for some reason you needed to use the C-style IO library in C++ code, `#include ` instead. – Karl Knechtel Jan 24 '11 at 23:02
  • @Karl I mention the IDE as it might affect the compiler settings. – Pulkit Sinha Jan 24 '11 at 23:12
  • Excellent question, which pinpoints briefly and clearly exactly the problem I had. I'm surprised it didn't get more upvotes. – kotchwane Mar 24 '21 at 08:58

4 Answers4

4

You need to declare test before you call it, otherwise C assumes it returns int.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • 1
    Isn't this behaviour deprecated even in C nowadays? I would have expected a compilation error, actually. At least in C++ this -- using a function without having seen a declaration before -- is illegal. – sellibitze Jan 24 '11 at 22:30
  • 1
    this solved it! Enabling all compiler warnings showed the warning. – Pulkit Sinha Jan 24 '11 at 23:11
  • 1
    @tobyodavies: It is allowed in C89, which is the latest C standard that the Microsoft compiler supports. Presumably the OP is compiling this code as C, rather than C++. – caf Jan 25 '11 at 00:01
  • @Pulkit, you posted on SO _before_ reading the warnings from your compiler... seriously? – tobyodavies Jan 25 '11 at 00:21
  • @tobydavies I haven't worked with VC++ compiler a lot and didn't know that some warnings are disabled by default. The original compilation didn't show any warnings. – Pulkit Sinha Jan 25 '11 at 07:10
4

IIRC, a long long constant in C/C++ is suffixed by 'LL'.

long long test() {
    return 1111111111111111111LL;
}

Your compiler is treating your constant as a 32-bit long (if you take your constant modulo 2^32, you get 734294471.)

BobG
  • 2,113
  • 17
  • 15
  • I thought one L made it a `long` and a second made it a `long long` – tobyodavies Jan 24 '11 at 22:30
  • 1
    The type of an unsuffixed decimal constant is the first out of `int`, `long int` and `long long int` that can represent the number. The `LL` suffix is only required to force a *smaller* number that would otherwise be of type `int` or `long` to be of type `long long` instead. – caf Jan 25 '11 at 00:04
  • the suffix is not necessary because if a constant doesn't fit in `int` it'll have type `long` or `long long` depending on its value http://stackoverflow.com/q/8108642/995714 – phuclv Feb 21 '16 at 10:02
1

Try adding LL to your return value:

long long test()
{   
    return 1111111111111111111LL;
} 
mikhon
  • 21
  • 1
0

Add the suffix LL to your literal and see what happens. Presumably the compiler conberts the literal to an int. Are you getting any warnings from the compiler?

Jörgen Sigvardsson
  • 4,839
  • 3
  • 28
  • 51
  • Don't presume, *know*. For a decimal integer constant without a suffix, the compiler picks the first out of `int`, `long` and `long long` that is long enough to represent the number. – caf Jan 25 '11 at 00:07