The output of the following code is 4
.
Shouldn't it be 0
?
Since a is declared and not been defined and hence memory is not allocated for it.
#include <stdio.h>
#include <stdlib.h>
int main()
{
extern int a;
printf("%ld",sizeof(a));
return 0;
}
The sizeof operator applied to a type name yields the amount of memory that can be used by an object of that type, including any internal or trailing padding. The result is the total number of bytes in the array. For example, in an array with 10 elements, the size is equal to 10 times the size of a single element.
“extern” keyword is used to declare and define the external variables. Scope − They are not bound by any function. They are everywhere in the program i.e. global. Default value − Default initialized value of global variables are Zero.
You can initialize any object with the extern storage class specifier at global scope in C or at namespace scope in C++. The initializer for an extern object must either: Appear as part of the definition and the initial value must be described by a constant expression; or.
Best way to declare and define global variables The clean, reliable way to declare and define global variables is to use a header file to contain an extern declaration of the variable. The header is included by the one source file that defines the variable and by all the source files that reference the variable.
Originally Answered: Why doesn't sizeof (a)/sizeof (a [0]) doesn't work when applied for an array passed as parameters? The behavior you found is actually a big wart in the C language. Whenever you declare a function that takes an array parameter, the C standard requires the compiler to ignore you and change the parameter to a pointer.
Using sizeof directly to find the size of arrays can result in an error in the code, as array parameters are treated as pointers. Consider the below program. Explanation: This code generates an error as the function fun () receives an array parameter ‘arr []’ and tries to find out the number of elements in arr [] using sizeof operator.
The sizeof operator is the most common operator in C. It is a compile-time unary operator and used to compute the size of its operand. It returns the size of a variable. It can be applied to any data type, float type, pointer type variables. When sizeof() is used with the data types, it simply returns the amount of memory allocated to that data ...
Its use is implicit. When extern is used with a variable, it’s only declared, not defined. As an exception, when an extern variable is declared with initialization, it is taken as the definition of the variable as well. Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
We know what the size of a
is even if it is not defined in this module. sizeof
does not tell you how much memory has been allocated for an object in this module. It tells you how much memory the object requires.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With