Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Hello, and welcome back.
In this lecture, we're going to talk about the register storage class specifier.
It has to do specifically with speed and efficiency.
So we all know what a register is in terms of the CPU.
A process register is a small set of data holding
places that are part of the computer processor.
So register can hold an instruction, a storage address
or any kind of data.
The register storage class is used to find local variables that should be stored
in a register instead of ram.
So normally, when you create variables right they're in memory,
random access memory,
when you specify the keyword register to a variable,
it's going to store the variable in a register.
So this will make the use of the register variables
much quicker and much faster more efficient than normal
variables that are stored in memory.
So during the run time the program is going to be more efficient.
The registered storage class should only be used for variables
that require this quick access,
the variables which are most frequently used in a c program.
So if a function uses a particular variable a lot,
then you may want to consider adding a register keyword to it.
It's basically a hint to the compiler
that a given variable can be put in a register.
It is the compiler's choice
to put in a register or not and you're going to see this as a common theme
when it comes to compiler and efficiencies.
You can suggest things to the compiler.
The compiler doesn't necessarily have to follow that, sometimes it can't.
So this is the same thing when you say register variable. It may or may not be stored in a register.
It might be stored depending on the hardware
and implementation restrictions in a register or not.
Generally, compilers themselves do optimizations
and put the variables in registers sometimes when you don't even know it.
The keyword register is used to define the register storage class,
both local variables and formal parameters can be declared as
register variables.
This storage class declares registered variables which have the same functionality
as that as automatic local variables.
So the lifetime of the register only remains
when control is within the block.
The actual variable that is stored in register
has a maximum size equal to the register size.
You cannot obtain the address of a registered variable using pointers.
So you cannot have the unary address of operator applied to it.
It doesn't have an address because it's not a memory location it's in a register itself.
So let's go ahead and look at an example.
So it's very basic to use a register it's pretty straightforward.
If we have a main function here and we want to clear our variable
with the storage class specify register, we just say register int x.
And this will be a register variable.
It's essentially a counter in this program.
And so now if you have a for loop, you can say x equals one.
X is less than equal to 15,
and x plus plus.
It doesn't really seem like a good example to use it because it's not necessarily being accessed to ton.
So I know how much efficiency you can improve, but it really is just showing you the syntax.
You apply that register keyword before the data type.
So maybe inside this function, we would print out the variable.
And it's as basic as that now.
As I previously mentioned,
if you try to use the ampersand operator with this variable,
the compiler is going to complain and give me an error,
sometimes it'll be a warning. It depends on the compiler you're using.
So when you say the variable's a register,
it may be stored in a register instead of memory
and axis in the address of the register is invalid. We can't do this. We can't
say register int x
and then try to create a pointer like int pointer a
equals ampersand x.
That's not going to work. And if you try to compile that,
again, it depends on the compiler.
In this case, it just gives me a warning, saying
unused variable a, but this is actually not okay to do.
So let's say we try to initialize that to 15
and then we just try to print out the variable,
the value of the pointer.
So we say de-reference it,
say something like this. If we build this,
we get an actual address of variable register is required.
So error address of register variable x requested, you can't do that
because x is registered.
Now you can still actually use the register key or with a pointer variable
or you just can't access the address.
So the register can store any data and that includes a memory location.
And so here, you could say, let's say, we
change this int x and we say it's equal to 15
and then we declare the pointer as a register.
So we say register int star a equals
the address of x. Now this is perfectly fine to do
because all we're doing here is we're saying
the pointer is actually going to store in a register and it' going to point to
store an actual address So if we tried to build this,
we're good to go. Because again, see the difference here.
The pointer is going to be stored in a register.
It can store an actual address.
You just can't get the address of a register variable itself.
So understand that different.
Now remember register is a storage class
and c does not allow multiple storage class specifiers for a variable.
So register cannot be used with static. We can't say register static
int a.
That's going to give you a compiler error. So again,
you can test this yourself.
But when you try to build this, it says multiple storage classes and declaration specifiers
You cannot do that now.
What if we try to use register with global scope.
So let's try to move this out, put it outside in here.
And we're going to remove the static because we can't do that.
We're not going to store -- we're not going to have it be a pointer, we're just going to say
register int x equals 10.
And then we're going to try to access this,
something like that.
If we try to build this, you're going to get an error.
It says register name not specified for x.
So register can only be used within a local block,
it can't be used in global scope outside of main.
It's perfectly fine like we did before to move this now
down inside the main function and then print it out.
This is fine. You just can't actually use it inside global scope.
So keep that in mind.
So now that we're done with storage class specifiers, let me just give you a little bit of review here.
So here's a nice summary of all our different storage class specifiers.
And you'll see also with static because you can
use it in many different ways I also summarize the static I found this online
this isn't my entire image.
But you'll notice, if you have storage class auto
and you declare that inside of a function or a block, the scope, the visibility
is within the function or the block. You can't access it in other files.
You can't access it outside of that function.
The lifetime
until the function block completes. So it gets created in the function destroyed in the function or the block.
When you use storage class register,
you can only use that inside of a function or a block, so inside the main, for example.
The scope is in within that function or block.
And again, it gets created and destroyed when the function creates
and -- when the function executes and returns.
For extern, extern is available outside all the functions.
It has basically global scope between files.
So the entire file plus other files
where the variable is disclaimed as external is the scope and the visibility.
The lifetime is the entire program until it terminates.
If you static locally inside a function or block,
it's only accessible within that function or block
and but it's it's actually allocated in memory,
all the way until the program exits.
Now if you static on a global
and it's declared outside of all functions which is global variable,
the entire file in which it is declared you can access,
you can't access it in other files. And again, it's alive in memory
until the program exits.
So understand these storage class specifiers,
understand how to use them with variables functions and so forth
and let me know if you have any questions. Thank you.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.