Showing posts with label compiler. Show all posts
Showing posts with label compiler. Show all posts

Tuesday, August 18, 2020

Gentle Introduction to C : extern Keyword

Originally Posted on SeedBx


Welcome to the seventh part of the Gentle Introduction to C series. As said before this series aims to provide you a brief introduction to C language.

In this part, we will be talking about the keyword extern.

However before talking about about extern keyword, let us just refresh our mind with the understanding of declaration and definition.

Declaration : Declaration of a variable or a function, as the name suggests, declares the variable or the function. However, during the declaration of a variable or function no memory is allocated for the entities. The sole purpose of declaration is to tell the compiler or the program about the type of variable or number of arguments, type of each argument,return type (in case of functions).

Definition : Definition of a variable or function, as the name suggests, defines the variable or the function. Definition of a variable or function, in addition to what declaration does, allocates memory for the entities. Hence, you can consider definition as a super set of declaration i.e. all definitions are declarations but all declarations are not definitions.

Note : Declaration of a variable or function can occur many times. However, a variable or function can only be defined once. This is due to the fact that a single variable  or function cannot be defined at multiple memory locations.

Now let's talk about extern keyword.

extern Keyword

The sole purpose of extern keyword is to extend the visibility of a variable or function. By visibility here I mean the scope of the variable or the function.

In case of variables, extern keyword can also be used to declare a variable.

Let us try to understand this using an example.

        int a;
In the line above the variable a is defined i.e. memory has been allocated to the variable a.

        extern int a;
In the line above the variable a is declared only i.e. memory has not been allocated to the variable 'a'.

So, if write a code like,
        extern int a;
        int main()
        {
            a=0;
            return 0;
        }
The code above will throw an error, as the variable a is only declared and not defined. So the code above essentially is trying to change the value of a memory location which doesn't exists.

So, we can modify the code as,
        #include "fileContainingDefinitionOfa.h"
        extern int a;
        int main()
        {
            a=0;
            return 0;
        }
Assuming that the file fileContainingDefinitionOfa.h contains the definition of variable a, the above code will compile successfully.

Note : If the variable declared using extern keyword is initialised, then the compiler allocates memory to the variable and hence implicitly defines the variable.

Example :
        extern int a=0;
        int main()
        {
            a=100;
            return 0;
        }
The above code will not throw any errors.

In case of functions, extern keyword is used to extend the visibility of the function. 
Since, the extern extends the visibility of the function to the whole program, the function can be called from any files provided that those files contains the declaration of the function.
With functions, the extern keyword is implicitly assumed whenever a function is declared or defined i.e.
        int foo (int a, int b)
is treated by the compiler as
        extern int foo (int a,int b)


At last, using extern keyword is a great way to define and declare global variables and often useful when certain functionalities and variables are to be shared among several files.


Thanks for reading.

As usual your suggestions and feedback are always welcome.


Also See : Pointers, Function, void Pointer

Friday, July 17, 2020

Gentle Introduction to C : Pointers

Originally Posted on SeedBx


Welcome to the fifth part of the Gentle Introduction to C series. As said before this series aims to provide you a brief introduction to C language.

In this part we will be talking about one of the powerful features of C, pointers.


Before starting with pointers, let’s first start by looking at how data is actually stored in the computer memory.

Representation of Variables in Memory

For every variable defined, at the run-time, the compiler assigns/allocates a particular unit of memory (depending on the type of the variable) to the variable which can be uniquely identified with the help of an address id. The address simply specifies the location where a particular variable is stored in memory. If you like, you can even think of the computer memory as a huge array in which the address is simply the index of the position at which the variable is stored.


Note : The allocation behavior is totally compiler dependent, and therefore certain irregularities might be experienced across various compilers usually due to the fact that different compilers may use different techniques to optimise working.

Pointers

The primary job of pointers is to store addresses of other variables or memory location. Each pointer has a particular data type which just specifies the type of data to which the pointer will point to. However there is some uniqueness about void data type.

For example, a typical definition of a pointer will look like,

datatype *var_name;

The asterisk (*) there specifies that the variable defined is a not a normal variable and rather it is a pointer.

The pointer is basically intended to do two operations, namely access the address of a variable and store it, which is done using the ‘address of’ unary operator & (ampersand) and to access the value stored at the location which the pointer points to which can be done using the ‘at address’ unary operator * (asterisk). The latter operation is also called Dereferencing.


Note : The dereferencing operator * and the asterisk (*) at the declaration should not be confused with each other. It is just a typical situation of overloading of an operator and the two operators have no relation among them.


& Operator

The & operator is used to access the address of a variable.

Example -

          int *ptr;

          ptr=&a;

In the given example, the address of the variable ‘a’ (assumed to be declared somewhere before) is stored in the pointer variable ‘ptr’.

* Operator

The * operator is used to access the value stored at the address which the pointer points to.

Example -

          int *ptr;

          ptr=&a;

          int b;

          b=*ptr;

In the given example, the value of the variable ‘a’ is actually assigned to the variable ‘b’ using the pointer variable ‘ptr’.

Illustration specifying the basic working of Pointers

Note : The size of a pointer variable is typically 4 bytes or 8 bytes. The size of a pointer variable is compiler dependent and it often varies from one compiler to the other.

At last, pointers are a pretty powerful tool to manipulate and play with the addresses of different variables in order to optimise various workings and often handle dynamically defined structures.

 

Thanks for reading.

As usual your suggestions and feedback are always welcome.


Also See : Header Files, Functions, Call by Value and Reference