Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

long long int initialization warnings

Tags:

c

2 Questions

First, while

long long int num = 1000000000000;

works fine

long long int num = 4014109449;

gives

warning: this decimal constant is unsigned only in ISO C90 [enabled by default]

What does it mean ?

Secondly

long long int num = 1000000*1000000;

gives an overflow warning while

long long int num = 1000000000000;

is ok,even though they are same.How do i get rid of it? Multiplication gives a garbage value

like image 825
rohan013 Avatar asked Nov 06 '13 18:11

rohan013


People also ask

What is the maximum size of an unsigned long int?

An unsigned data type stores only positive values. It takes a size of 64 bits. A maximum integer value that can be stored in an unsigned long long int data type is 18, 446, 744, 073, 709, 551, 615, around 264 – 1 (but is compiler dependent ).

How to initialize an unsigned long long type?

First, be sure your compiler supports the long long type. Second, add a "ULL" suffix to the number. Show activity on this post. Q: How to initialize an unsigned long long type?

What is the maximum value a long int can be stored?

A maximum integer value that can be stored in a long long int data type is typically 9, 223, 372, 036, 854, 775, 807 around 263 – 1 (but is compiler dependent). The maximum value that can be stored in long long int is stored as a constant in <climits> header file.

What is long long int in C++?

Maximum value of long long int in C++ Last Updated : 18 Dec, 2020 In this article, we will discuss the long long int data type in C++. long long int data type in C++ is used to store 64-bit integers. It is one of the largest data types to store integer values, unlike unsigned long long int both positive as well as negative.


1 Answers

The problem is that the value 4014109449 is an unsigned long int in C90 but a long long int in C99 because it is too large for a 32-bit long int. While 1000000000000 is too large for any 32-bit type, so is automatically a long long int. The warning relates to the fact that the behaviour differs between C90 and C99.

The solution is to force type agreement between the literal and the variable type by using an appropriate type suffix. In this case:

long long num = 4014109449LL ;

or use a type cast:

long long num = (long long)4014109449 ;

Similarly the expression 1000000 * 1000000 is a multiply of two int types and has an int result, but causes an overflow - there is no automatic promotion to a larger type for int expressions. The solution is again to be explicit about the type of the literal:

long long num = 1000000LL * 1000000LL;

or you can also use a type cast on one or both operands.

long long num = (long long)1000000 * 1000000;
like image 192
Clifford Avatar answered Sep 20 '22 21:09

Clifford