-4

What is the difference between unsigned int a=2 and int a=2U

Also,

sizeof(a) operator gives the same value for int a=2 and int a=2L , why? Shouldn't the size be doubled.

UPDATE: Thanks all for the answers. here is the summery:

  1. long int or int are types with which variable is declared. 2 or 2L is the value with which variable is initialised.
  2. Size of the variable is declared by type instead of its initialisation, so both will have same size
bareMetal
  • 425
  • 1
  • 7
  • 20

3 Answers3

5

In C++, all variables are declared with type. C++ forces1 you to specify the type explicitly, but doesn't force you to initialize the variable at all.

long int a = 2;
long int b = 2L;
long int c;

This code makes 3 variables of the same type long int.

int a = 2;
int b = 2L;
int c;

This code makes 3 variables of the same type int.

The idea of type is roughly "the set of all values the variable can take". It doesn't (and cannot) depend on the initial value of the variable - whether it's 2 or 2L or anything else.

So, if you have two variables of different type but same value

int a = 2L;
long int b = 2;

The difference between them is what they can do further in the code. For example:

a += 2147483647; // most likely, overflow
b += 2147483647; // probably calculates correctly

The type of the variable won't change from the point it's defined onwards.

Another example:

int x = 2.5;

Here the type of x is int, and it's initialized to 2. Even though the initializer has a different type, C++ regards the declaration type of x "more important".


1 BTW C++ has support for "type inference"; you can use it if you want the type of the initializer to be important:

auto a = 2L; // "a" has type "long int"
auto b = 2; // "b" has type "int"
anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • Got it, thing is, `int a=2L` , though a is initialized with value '2' which is of long int type, a is still int type. The variable of one type can be initialized with a value of another type – bareMetal Jan 29 '17 at 21:36
  • I tried: `auto a = 2L;` It gives the error: **'a' does not name a type** why? – bareMetal Feb 20 '17 at 20:36
  • Maybe you're using an old compiler. This syntax of using `auto` is relatively new (C++11, 5 years old now). See also [here](http://stackoverflow.com/q/10363646/509868) and [here](http://stackoverflow.com/q/37582094/509868) for hints on how to fix this. – anatolyg Feb 20 '17 at 21:54
4

What is difference between "long int a=2" and "int a=2L"?

The former defines a variable a as having type long int initialised from the value 2, the latter defines it as having type int initialised from the value 2L. The initialiser is implicitly converted to the type of the variable, and does not affect the type of the variable.

Or what is the difference between long char c='a' and char c=L'a'

The former defines a variable c as having type long char initialised from the value 'a', the latter defines it as having type char initialised from the value L'a'. Since the type long char doesn't exist, the former is an error. The type of L'a' is called wchar_t, not long char, and in the latter case is again converted to the type of the variable.

or what is the difference between unsigned int a=2 and int a=2U

The former defines a variable a as having type unsigned int initialised from the value 2, the latter defines it as having type int initialised from the value 2U. Yet again, the initialiser does not affect the type of the variable.

Also,

sizeof(a) operator gives the same value for int a=2 and int a=2L , why? Shouldn't the size be doubled.

Since they both define a as type int, sizeof(a) should give sizeof(int) for both.

Community
  • 1
  • 1
  • help me, exactly that is where I am confused **The initialiser is implicitly converted to the type of the variable, and does not affect the type of the variable.** if it does not effect, then what is its use. and as memory is allocated according to the type of variable then how can 2L(int long type) accommodate inside the memory block which has size of int only, – bareMetal Jan 29 '17 at 21:23
  • @AvinashKumarShudhanshu `int a=2L;` does exactly the same thing as `int a=2;`. It's the value that is being converted, not the bytes that make up that value. Converting `2L` to type `int` simply produces `2`. –  Jan 29 '17 at 21:28
1
#include <stdio.h>

int main()
{
    int a1=2;      //a1=2
    int a2=2L;     //a2=2
    int a3=2.5673; //a3=2
    int a4='A';    //a4=65
    return 0;
}

Here, even though the value of a3 and a4 is float and char respectively, the value will be transformed to int as a3 and a4 is declared as an int. In the same way, the value of a2 will be transformed into int even though the value was set as 2L. The variable doesn't depend on the value, rather on type declaration. int a will always be an integer, no matter what's it's value is.

Miraz
  • 343
  • 3
  • 15