plz tell me that in c..
whether
int a;
is declaration or definition??
will memory be allocated at the time it is written...
13 1766
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
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.
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.
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
Well, there you go, I gave a C++ answer again on a C topic.
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.
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.
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
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
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
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.
I think he was talking about using ifndef combined with define in the format of - #ifndef test_h
-
#define test_h
-
-
// Code
-
-
#endif
This would prevent it from being defined twice as it would only be compiled once.
Edward
I think he was talking about using ifndef combined with define in the format of - #ifndef test_h
-
#define test_h
-
-
// Code
-
-
#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
Sign in to post your reply or Sign up for a free account.
Similar topics
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>...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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: 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...
|
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,...
|
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...
|
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,...
|
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...
| |