fuddlepixelFuddlepixel coz tech is fun();

Auto and Register Variables

Let us discuss the concept behind the auto and register types of variables. How they are important in the C programming Language !

By Shubham Ramdeo
16 November 2015 · 4 mins read
# c  newbie 

Auto and Register Variables

These are the Auto and Register types of Variables !

Welcome to the Newbie Programmer Series. In the last post, we have discussed a lot about Static Variables (click here). These are the variables which do not die. In this post , we will discuss the last thing about the “scope” topic. We will discuss two almost not so useful but important to know, auto and register variables. Its short and simple, just to know. So let’s get into it.

And obviously if you are new to this series, please go to the index (click here) and read out all the previous parts so that you can easily understand what is going down below.

Register Variables

As I told you before (here), whenever we make a variable or store any data, it eats some space in the computer’s memory. When we use that data, its accessed and edited from the memory. And this thing is very fast because all these live data is stored inside the computers RAM (Random Access Memory) . You know when we compare the speed of two smarty phones, we always have to compare which phone gets more RAM, because everything in use is stored somehow in RAM.

But as you know, we also check CPU. We compare if we are getting 2 GHz processor or 4 GHz ? We also compare things like Dual core and Octa core Processors. Why ? Because the thing is that, processors or CPUs play more important role in speed than RAM. It’s due to CPUs that RAM can live life.

But suppose we are doing some hardcore deep computer programming thing, like a Chess Engine, in that case, a user will make a move in seconds. And so computer has to evaluate which move is good and which is bad in less than that time, so to think so fast, do you think computer will first get data from RAM and then give it to CPU, then will be processed and then will be sent back to RAM, then to the chess game. In all this computer will waste a lot of time, because in such chess like computer artificial Intelligence, even a fraction of second is too useful.

But computer have a faster solution. Computer can save the temporarily useful data for calculation in CPU itself. Things stored in CPU can be used faster than those in RAMs.

By the way, it’s not the exact CPU, its called register memory. Which is the CPU’s memory.

So in C programming Language, if your system allows, we can store a variable inside CPU’s memory or register as :

register data-type variable name;

So this program:

#include <stdio.h>
main()
{
register int h;
h = 10;
printf("%d", h);
}

OUTPUT :

10

But there is a downfall to register variables.

Register variables cannot have address !

You have learned before (here) that when a variable is stored in computer’s memory, we have its address. Computer use the address to go to that space where the data is saved.

But when we use register variables, we cannot have any address. Computer can use that variable but cannot know where it is.

So this piece of code below :

#include <stdio.h>
main()
{
register int h;
printf("Enter a number \n");
scanf("%d", &h);
printf("%d", h);
}

Will not work as with the scanf, we are asking a number and saving it to memory but as the variable h is in register, computer do not know where is that space to save that entered number.

You see while using scanf, we use “&” operator to get the address. Learn more here

But in the first example, we are have already stored h = 10 directly to register before the computer think to save it so computer just print the value.

And that’s the register variables.

Auto variables

We have now learned a lot of different types of variable scopes.

We can use extern, static, register etc.

But what is when the variable is just a simple variable as we use normally ? A simple normal variable is used as an auto variable.

and it is used as :

auto data-type variable name;

But as I said above, this is that simple normal variable which we use all the time, so using “auto” or just simple writing the variable makes no difference.

So any piece of code :

#include <stdio.h>
main()
{
int h;
printf("Enter a number \n");
scanf("%d", &h);
printf("%d", h);
}

and using auto

#include <stdio.h>
main()
{
auto int h;
printf("Enter a number \n");
scanf("%d", &h);
printf("%d", h);
}

have no difference !

So we have finally completed all the different types of scopes of variables. We will discuss a lot of interesting things soon. Starting with pre-processors, I think.


Share this :



Update me weekly ✉

Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.

I'm in ♥


Or follow us
   


About author

Shubham Ramdeo

Shubham Ramdeo

Shubham Ramdeo is the founder of Fuddlepixel. He is a geek, a writer, involved with many technical and humanity projects.

Meet me!



Recent





Related

c 

newbie 

Top