Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why applying sizeof operator to an extern variable does not output 0

Tags:

c

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;
}
like image 658
Vivek Avatar asked Jul 27 '18 14:07

Vivek


People also ask

What is the purpose of the operator sizeof?

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.

What is the scope of extern variable?

“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.

Can extern variables be initialized?

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.

How do I use extern to share variables between source files?

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.

Why doesn't sizeof (A)/sizeof (A[0]) work when applied for an array passed as parameters?

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.

How to find the size of an array Using sizeof operator?

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.

What is the sizeof operator in C?

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 ...

What is the use of extern variable in C?

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.


1 Answers

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.

like image 103
Eric Postpischil Avatar answered Sep 22 '22 11:09

Eric Postpischil