473,378 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

doubt in storage class

Hai,
I am jeyshree.W ill anyone please explain me when is the memory allocated for gloabal variable in the following snippets?
#include<stdio.h>
#include<conio.h>
extern int a;
main()
{
printf("%d",a) ;
getch();
}
int a=10;
OUTPUT:
10
and another one:
#include<stdio.h>
#include<conio.h>
extern int a=10;
main()
{
printf("%d",a) ;
getch();
}
OUTPUT:
10
Please explain .Thanks in advance
Jul 6 '10 #1
3 1210
donbock
2,426 Expert 2GB
"when is memory allocated"

Are you looking for an answer that is true for typical compiler implementations or are you interested in only what is guaranteed by the C Standard?

It would help me understand what you're asking for if you listed some of the possible options you can think of for when memory might be allocated.
Jul 7 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
Memory for global variables is allocated before main() starts.

This is a global variable:

Expand|Select|Wrap|Line Numbers
  1. extern int val = 10;
This is not:

Expand|Select|Wrap|Line Numbers
  1. extern int val;
The first case creates a global variable with an intial value of 10 and the variable has external linkage.

The second case tells the compiler that there is a variable named val that has external linkage but it's not in this file.
Jul 7 '10 #3
donbock
2,426 Expert 2GB
In the following example, the extern statement serves only one purpose -- it allows a to be accessed from the main function. Without the extern statement, references to a from main would be forward references to line 5, which are not permitted in C. Line 1 could be deleted without altering the operation of the program if line 5 were moved to that position.
Expand|Select|Wrap|Line Numbers
  1. extern int a;
  2. int main() {
  3.    ...
  4.    }
  5. int a = 10;
In the following example, the extern keyword is superfluous. There would be no change to the operation of the program if it were removed.
Expand|Select|Wrap|Line Numbers
  1. extern int a = 10;
Jul 7 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Tobias | last post by:
Hallo, I have a strange problem here: I a VC++6 project that compiles without any errors. If i now include a certain header file from my project into a header file of a certain dialog class i get...
1
by: ravi_shankar | last post by:
I know java,but I am just beginner in C.I have some confusions regarding extern storage specifier and default storage class specifier for a variable when it has file scope that is ,when it is not...
14
by: aruna | last post by:
What is the disadvantage of using register storage class specifier?
4
by: Ced | last post by:
Hi, i'm not an expert in C but i try to compile BTNG software under linux kernel 2.4.2-2. I get these errors at the very first stage. Does someone could have a rapid look on this and tell me...
2
by: sarathy | last post by:
Hi, What is the difference b/w a "storage class" and "storage class specifier"? It is said that there, Storage Class --automatic static ( K&R ) Storage Class Specifier --auto extern register...
1
by: sunny | last post by:
Hi C Ref Man says " The typedef specifier does not reserve storage and is called a storage class specifier only for syntactic convenience " does this mean storage specifier for "user...
5
by: shan_rish | last post by:
Hello Group, Please go through the program below: RatingComponent.h ================= class RatingComponent { public:
1
by: BobPaul | last post by:
I'm following code out of a howto book and this is really bugging me. This header file was created by VStudio 6.0 when I did a "Right Click: Add Member Function" CLine is a class I wrote (per the...
0
by: terminator(jam) | last post by:
Since delaration of C++ the r/l-valueness of objects has been source of confusion there are some actions that can perform on lvalues but not on rvalues , especifically on initrinsic types .BTW the...
2
by: asit | last post by:
Can functions have static storage clas ?? If yes, what is it's advantage ??
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.