The first cout
statement cout<<sizeof(A)<<"\n";
in your code is giving 0
as output. Irrespective of what I take n
as input, the next cout
statement is also giving a 0
. There are two declarations here, int n
and int A[n]
. As a beginner, it is fair to assume that n
remains the same in both cases or has the same value, therefore the size shouldn't change. However, one is an integer(n)
, the other is an array of integer(A[n])
. That makes all the difference!
The first time you print the size of A[n]
, you are getting a 0
because the array is only declared and not initialized so we know it's empty. The next time, you are taking n
as an input, so its size should be 4 bytes
(try it yourself) because it's an integer.
Having said that, it really depends on the type of compiler or operating system you are using. I got 4
as an output in one of the online compilers and when I tried implementing it on codeblocks and vscode, I got 32
and 80
respectively. Essentially, this is an undefined behaviour even if n
had a garbage value!