×
 Introduction
Introduction to Newbie Programmer Series Introduction to Computer The Computer Programming Language Setting up the Computer for Programming
 Lets Get Started !
Beginning C Programming Variables a Handbag ! Basic Variable types in C Basic Formatted Output Basic Formatted Input
 Loops
Looping Repeating Concept Different Kinds of Loops Advanced concepts and trick with Loops
 Operators
Operators, An Introduction Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Miscellaneous Operators and Operators Precedence
 Controlling the Flow
Flow Charts Conditional Statements Using Goto and Label Break and Continue the loops Switch the Cases Some Practical Applications of Control Flow
 The program structure
The program Structure Functions More Concepts of Functions Scope of the Variables across functions How the C language itself Works ? Scope of the Variables across files Static Variables Auto and Register Variables #define PreProcessor Some more PreProcessors
 Arrays and Structures
Introduction to Arrays Arrays, some more Concepts Arrays, Cool Examples Structures Introduction Structure, Some more Concepts Structure, Some Examples
 File Management
Basics of File management in C Steps Involved with File Handling in C
 Miscellaneous
Inside Logic Gates – The Electronic Logic Binary Number System Mathematical Reasoning
Newbie Programmer
full book coming soon
by Shubham Ramdeo

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.

 

Please share this as much as you can so that we can get connected with more and more people who want to learn programming.

© Shubham Ramdeo

Site Map
with
© Shubham Ramdeo