• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

'long' with more than 10 digits won't compile

 
Nick Stevens
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on an anagram solver and I've found that

int 1234567890;

compiles okay (as does long 1234567890);

long 12345678901;

generates the error ': integer number too large 12345678901'. I thought 'long' variables could consist of more digits than 'int' variables? Any suggestions would be much appreciated!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put an 'L' on the end to indicate it is a long

long l = 12345678901L;
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write

The compiler handles the number as an int and therefore this gives a compiler error. (All literals like that are considered int's by default)

To make the compiler handle this as a long, you have to append a 'l' or a 'L' to the number.

Now the compiler will know you really meant a long and not complain.
 
Nick Stevens
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks both of you for your help, I've added an l and it compiles okay - hurray! I wonder why the compiler assumes a long 'literal' is an int? I'll be unscrambling anagrams again now :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic