Hi friends,
I am sita I want to know more about storage class in C language.I think you can help me for finding out the answers.
First of all Can you please tell about Local and Global variables? Or What are Local and Global variables?
Can you please show some examples also?

Hello Sita,
I am just trying to give the answer of your question.

Local variables::
1.Local variables only exist inside the specific function that creates them.
2.Local variables are allocated memory on the stack.
3.Local variables are not initialized automatically.


Global variables are::
1.Global variables can be accessed by any funnction contained in the program.
2.Global variables are allocated memory on the Data segment.
3.Global variables are normally initialized to 0 by default.

Thank you. Thank you very much Hug.
You mean, the local variables are unknown to other function and to the main program?
And the global variables are visible throughout the program from the point of their declaration?
Then, please help me out for finding the answer for this question also,
What is the scope of static variables?

Yea you are right my dear friend.

the storage class, static , has a lifetime lasting the entire program. static storage class can be specified for automatic or local as well as global variables.Static automatic variables continue to exist even after the block in which they are defined terminates.static variables are allocated on the heap.

Ok Hug. Thank you very much once again.
Ok then, u mean , the scope of a static global variable is only within the file in which it is declared?
And a user cannot use extern in a different file and access the static global variable?

Then, can you please explain, What is the difference between static and global variables?

It takes a twisted personality to create multiple accounts so that you can answer your own questions. :icon_rolleyes:

Member Avatar for iamthwee

Of course the OP wouldn't have realised you are privy to information such as their IP addresses.
:)

Congrats on the Supermod badge! I guess you have more excuses to surf the web now. :D Have fun.

Sir, this is a discussion community. We are all the members of same house. So we have the same IP address. But it is usful to to others, those who r interested in this topic. They can understand more simple and basic things from this discussion.Most complex features of any language r starting from its basic. So our purpose is simple . Just discuss from basics. Anyway Sorry sir...
Thank you.....

Can we proceed.....

What is the difference between static and global variables.?
Please tell more about volatile variables?
What is the main use of 'auto' keywords?

show some example also plsss..

Hi friends,
I am sita I want to know more about storage class in C language.I think you can help me for finding out the answers.
First of all Can you please tell about Local and Global variables? Or What are Local and Global variables?
Can you please show some examples also?

example of local variable____


#include<stdio.h>

void main()
{
int a,s; /*** local variable****/
printf("\n enter the value of a---");
scanf("%d",&a);
s=a+2;
printf("s=%d",a);
getch();
}

output--

enter the value of a--- 5
s=7


example of global variable----

#include<stdio.h>

int a; /***global variable*********/

void main()
{
int s;
printf("\n enter the value of a--");
scanf("%d",&a);
s=a+3;
printf("\n s=%d",a);

mul(); /****another function********/

getch();


void mul()
{
int m;

m=a*2;

printf("\n m=%d",a);
}


output---
enter the value of a----5

s=8

m=10


///*********************************////

so global variables can be used by any function,,by just declaring it above the main();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.