Storage Classes in C - Types And Classification Languages Programming Tutorials

Storage Classes in C-A storage class defines the scope (visibility) and life time of variables and/or
functions within a C Program..Two types of storage classes are there in c as explained below
1. Auto
2. Static

Storage class specifiers are
1. Auto
2. Static
3. Extern
4. Register
Auto - Storage Class-Auto is automatic class. resides in stack.
auto is the default storage class for all local variables.
{
int Count;
auto int Month;
}

The example above defines two variables with the same storage class. auto can
only be used within functions, i.e. local variables.
Variable Function Structure Pointer in C
Programming - C Viva Questions
Function  in Embedded C
C Programming Basic Questions
Array in Embedded C
Arrays And Pointer in C

Register - Storage Class-Register is used to define local variables that should be stored in a register instead
of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location).Fast access but limited.Reside in CPU registers details.


{
register int Miles;
}
Register should only be used for variables that require quick access - such as counters. It should also be noted that defining 'register' goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register -depending on hardware and implementation restrictions.

Static - Storage Class-static is the default storage class for global variables. The two variables below (count and road) both have a static storage class.

Static has three means
static variable
static global
static function
resides in data memory and BSS

static int Count;
int Road;
{
printf("%d ", Road);
}

Static variables can be 'seen' within all functions in this source file. At link time, the static variables defined here will not be seen by the object modules that are brought in.Static can also be defined within a function. If this is done the variable is initialized at run time but is not reinitialized when the function is called. This inside a function static variable retains its value during various calls.

void func(void);
static count=10; /* Global variable - static is the default */
main()
{
while (count--)
{
func();
}
}
void func( void )
{
static i = 5;
i++;
printf("i is %d and count is %d ", i, count);
}

This will produce following result
i is 6 and count is 9
i is 7 and count is 8
i is 8 and count is 7
i is 9 and count is 6
i is 10 and count is 5
i is 11 and count is 4
i is 12 and count is 3
i is 13 and count is 2
i is 14 and count is 1
i is 15 and count is 0


NOTE : Here keyword void means function does not return anything and it does not take any parameter.You can memories void as nothing. static variables are initialized to 0 automatically.Definition vs Declaration : Before proceeding, let us understand the difference between definition and declaration of a variable or function. Definition means where a variable or function is defined in reality and actual memory is allocated for variable or function. Declaration means just giving a reference of a variable and function.Through declaration we assure to the compiler that this variable or function has been defined somewhere else in the program and will be provided at the time of linking. In the above examples 
char *func(void) has been put at thetop which is a declaration of this function where as this function has been defined below to main() function.

There is one more very important use for 'static'. Consider this bit of code.
char *func(void);
main()
{
char *Text1;
Text1 = func();
}
char *func(void)
{
char Text2[10]="martin";
return(Text2);
}

Now, 'func' returns a pointer to the memory location where 'text2' starts BUT text2 has a storage class of 'auto' and will disappear when we exit the function and could be overwritten but something else. The answer is to specify

static char Text[10]="martin";

The storage assigned to 'text2' will remain reserved for the duration if the program.

Extern - Storage Class - Extern is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern' the variable cannot be initalized as all it does is point the variable name at a storage location that has been previously defined.When you have multiple files and you define a global variable or function which will be used in other files also, then extern will be used in another file to give reference of defined variable or function. Just for understanding extern is used to decalre a global variable or function in another files.

File 1: main.c
int count=5;
main()
{
write_extern();
}
File 2: write.c
void write_extern(void);
extern int count;
void write_extern(void)
{
printf("count is %i ", count);
}

Here extern keyword is being used to declare count in another file.Now compile these two files as follows
gcc main.c write.c -o write. This fill produce write program which can be executed to produce result.Count in 'main.c' will have a value of 5. If main.c changes the value of count - write.c will see the new value.Global resides in data memory.

Related

Embedded C Programming Twenty Interview Questions Basics Viva Tutorials

1.) Trace the output of the code #include  void foo(int*); int main() { int i = 10; foo((&i)++); } void foo(int *p) { printf(""%d\n"", *p); }  a.) 10 b.) Some garbage val...

What is Storage class - Embedded C Programming Frequently Asked Interview Questions

What is Storage class? Explain with example The storage class determines the part of memory where storage is allocated for an object (particularly variables and functions) and how long the storage ...

Post a Comment

  1. Replies
    1. Thank you for your encouraging words.Keep visiting our blog for more updated contents.

      Delete
  2. Again thumbs up from me.Please post tutorials for Array giving details about programming and use.If possible please provide video tutorials.Thank you in advance.

    ReplyDelete
    Replies
    1. Thanks +Echelon Tech.We will do our best to provide good contents to our readers.

      Delete

SPAMMING will not be Appreciated.

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

Hot in weekRecentComments

Hot in week

Recent

C Programming - Data Structure Interview Questions Answer

We have started series of C programming Q&A for job interview for freshers.Computer / IT Engineering Professionals and Students alike will be benefited.We recommend our user to go through pre...

C Programming Question Answer

We have started series of C programming Q&A for job interview candidates.Engineering Professionals and students alike will be benefited.We recommend our user to go through previous post to enha...

Java Objective Multiple Answer Questions Bank

We have chosen Java Oracle J2EE, J2SE, Net-beans, JVM ( Java Virtual Machine ) as our readers choice.We have created Objective Type Multiple Answer Question Collection that are frequent in job inte...

Java Question Bank Objective Q&A

Here we have collected frequently asked question in job interviews.We have chosen Java as our readers choice.You will find these helpful.We encourage our Readers to send in their suggestion. If re...

C Question Bank

We have started series of C programming Question bank for job interview candidates.Engineering Professionals and students alike will be benefited.We encourage our readers to provide feedback and as...

Comments

Anonymous:

Technology is always being the vital part of evolution either mobile phones or computer all are the part of it. Electronics have made things so easy and reliable for human being s. very few schools in...

Anonymous:

A detailed and complete knowledge guide for fresher's to crack their interviews in Embedded Programming. Looking for a job contact <a href="http:/celebratejobs.com/>celebratejobs</a&...

YouLoseBellyFat:

visual basic example codes

App Development Mumbai:

It was very useful for me. Keep sharing such ideas in the future as well. This was actually what I was looking for, and I am glad to came here! Thanks for sharing the such information with us.

Anonymous:

Thanks for Appreciations.We love to hear again from you.

Our Channel

Contact Us

Name

Email *

Message *

item