473,387 Members | 1,903 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,387 software developers and data experts.

Defination of a varibale/ function in C

While reading on difference between declaration and definition of variable ; I encountered following statement-
"In Definition the compiler actually reserves memory from the program's image."
I didn't get what exactly does it mean.
Which memory it is referring to ? Is it primary memory ?
But primary memory (RAM) will come into picture when the program is running and compiler is far before that.

I would really appreciate if somebody could please elaborate on this.

Thanks.
May 12 '10 #1
4 1886
Dheeraj Joshi
1,123 Expert 1GB
During declaration the memory is allocated according to the type.

If char 1 byte, if int 4 byte etc.
While definition the value of that variable is stored in that address.

Regards
Dheeraj Joshi
May 12 '10 #2
donbock
2,426 Expert 2GB
A declaration informs the compiler that a variable exists. This only has an effect on the internal symbol table used by the compiler while it translates your source code. Declarations do not appear in the compiled object code that is the primary output of the compiler.

A definition is a directive to the compiler that a variable has to be created here. This affects the internal symbol table, but the compiled object code will also contain the necessary instructions to create the variable. The precise sequence of instructions varies depending on what kind of variable was defined.
May 12 '10 #3
Hi Dheeraj and donbock,
thanks for your reply...!

So, when the declaration statement is getting compiled , no compile object code is generated, rather, declaration statement is used to get the compiler know that this particular identifier exists, and it has its its effect on symbol table.
However, while compiling 'definition' statement, compiler generates some compiled object code statement that will eventually lead to memory allocation for variable during execution of program.

donbock, could you please verify my understanding.

Thanks,
-Sushant.
May 12 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
Here's a declaration:

Expand|Select|Wrap|Line Numbers
  1. extern int val;
This declares that an integer named val exists but it's not in this file. This tell the compuiler to not worry about not hav in actual variable that the linker will sort it out.

Here's another declaration:

Expand|Select|Wrap|Line Numbers
  1. struct Data
  2. {
  3.      int val;
  4.      int anotherVal;
  5. };
This says that should I define a variable of type data that this is how it is to be constructed. The actual definition is:

Expand|Select|Wrap|Line Numbers
  1. Data myVariable;     //memory for a struct Data actually allocated.
It is is declaration that goes in the header file and the definition that goes in the implementation file.
May 12 '10 #5

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

Similar topics

8
by: scl | last post by:
two class with same name exist in different dynamic linked library: a.so class REGION() { public: .... ~REGION() {} } b.so
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
10
by: Nitin | last post by:
Ppl , Want to have ur opinions on having function calls like the one stated below: function funcA ( struct A *st_A , struct B *st_B ) { st_A->a = st_B->a
1
by: Guha | last post by:
I have a problem with returning a 2D array using a function which is called in main(). The piece of the code is given below. This is a test code only. #include"stdio.h" #include"alloc.h" ...
2
by: sukrit.mehra | last post by:
Hi People, Few of my colleuages were flexing their C muscles and they asked me this question. void Function(void); int main(void) { int x=5;
0
by: Pravin | last post by:
I am trying to access C function from C#. I have made DLL of C Code. I have exported function to be caled using DEF file. defination of the function I want to call is have one of the parameter is...
3
by: sunnylele | last post by:
Hi, all, a question on the following defination: an external function is defined as: nmriopen(char *const filename, const int np1, const float step1, const int np2, const float step2) what's...
5
by: Chris H | last post by:
Okay, I am trying to us a varialble with a list of number values as my data for an array, yet when I do this it doesnt return any values from the array, i was able to temporarily fix the proble by...
25
by: venky | last post by:
Hi main() { int x; /* it declaration or defination??*/ }
1
by: Johnny E. Jensen | last post by:
Hello I have a class that inherence from the ToolStrip control called (AWToolbar), and i added my own methods to that class. Then i have a usercontroll (QuickNavigator), and I add the AWToolbar,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.