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

c definition and declaration

plz tell me that in c..
whether
int a;
is declaration or definition??
will memory be allocated at the time it is written...
Aug 19 '08 #1
13 1766
JosAH
11,448 Expert 8TB
It is a tentative definition; I normally use the declaration 'extern int a' in a header
file and use one tentative definition in exactly one .c file: 'int a'.

kind regards,

Jos
Aug 19 '08 #2
memory will be allocated. Although you have set no number, a number will be stored in a. If you

cout << a << endl;

after int a; then a number is still displayed.
Aug 19 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
Nothing tentative about it. Memory is allocated. That makes it a definition. It's not initialized to any particular value but that is irrelevant.

As a definition, it is not supposed to be in a header file.
Aug 19 '08 #4
JosAH
11,448 Expert 8TB
Nothing tentative about it.
Yes it is; see the C99 Standard for this (I can't quote it here because it is all
over the place).

kind regards,

Jos
Aug 19 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
Well, there you go, I gave a C++ answer again on a C topic.
Aug 19 '08 #6
They should only be defined in header files if they are part of a function or a class. Otherwise I dont believe they should be.
Aug 19 '08 #7
weaknessforcats
9,208 Expert Mod 8TB
I repeat, definitions do not belong in header files.

Every time you include that header you get another definition. Multiple definitions will cause problems at link time.
Aug 19 '08 #8
I repeat, definitions do not belong in header files.

Every time you include that header you get another definition. Multiple definitions will cause problems at link time.
Using #ifndef would prevent this problem
Aug 19 '08 #9
But that doesn't mean that it is good style. By including definitions in a header file you are also slowing down compile time. The entire reason that there are header files and libraries is that way it is unnecessary to compile the entire library every time it is used. This would be incredibly tome consuming and a pain. Instead a header file contains the decleration of the function (and often the documentation for the function as well) while the library contains the compiled but unlinked definitions.

If you search for header files in google you will get a lot of good examples of this.

Edward
Aug 19 '08 #10
JosAH
11,448 Expert 8TB
Using #ifndef would prevent this problem
No it wouldn't; different compilation units can include the same header file
(with a definition in it) and there is no #ifndef that can prevent another definition
in another compilation unit.

kind regards,

Jos
Aug 19 '08 #11
But that doesn't mean that it is good style. By including definitions in a header file you are also slowing down compile time. The entire reason that there are header files and libraries is that way it is unnecessary to compile the entire library every time it is used. This would be incredibly tome consuming and a pain. Instead a header file contains the decleration of the function (and often the documentation for the function as well) while the library contains the compiled but unlinked definitions.

If you search for header files in google you will get a lot of good examples of this.

Edward
EDIT: I am not saying that it is unnecessary to use ifndef but instead am saying that ifndef is only part of the solution.

Sorry: hit the wrong button and didn't notice.
Aug 19 '08 #12
I think he was talking about using ifndef combined with define in the format of

Expand|Select|Wrap|Line Numbers
  1. #ifndef test_h
  2. #define test_h
  3.  
  4. // Code
  5.  
  6. #endif
This would prevent it from being defined twice as it would only be compiled once.

Edward
Aug 19 '08 #13
JosAH
11,448 Expert 8TB
I think he was talking about using ifndef combined with define in the format of

Expand|Select|Wrap|Line Numbers
  1. #ifndef test_h
  2. #define test_h
  3.  
  4. // Code
  5.  
  6. #endif
This would prevent it from being defined twice as it would only be compiled once.

Edward
Sure but it can't prevent multiple definitions over multiple translation units.
Multiple defintions in one translation unit are signalled by the compiler.

kind regards,

Jos
Aug 19 '08 #14

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

Similar topics

8
by: newmans | last post by:
Perhaps one of the experts can straighten me out on this point... In Bjarne Stroustrup's book 'The C++ Programming Language' 3rd Edition, section 4.9, indicates that typedef complex<short>...
19
by: J. J. Farrell | last post by:
After many years of dealing with definition and linkage issues in ways that I know to be safe, I've decided it's time to try to understand this area properly. Consider a header file with the file...
10
by: Kobu | last post by:
My question is about the use and meaning of the terms "declaration" and "definition" as it pertains to the C language. I've read sources that mix the two up when talking about such things as...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
15
by: vaib | last post by:
hi to all.i'd like to know the actual difference between variable declaration and definition.it would be very helpful if anyone out there wud help me out with this thing.i'm writing here after here...
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: 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
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.