Best Answer

In The Defintion of a variable space is reserved for the and some initial value is given to it, whereas a declaration only identifies the type of the variable for the function . thus definition is the place where the variable is created or assigned storage whereas declaration refers to places where the nature of the variable is stated but no storage is allocated.

User Avatar

Wiki User

โˆ™ 2010-10-04 21:46:21
This answer is:
User Avatar
Study guides

Data Science

See all cards
6 Reviews
More answers
User Avatar

Wiki User

โˆ™ 2014-11-13 23:17:42

Although the terms are often used interchangeably, they have distinct meanings in both C and C++. To be more precise, they have distinct meaning to the compiler and linker.

A declaration is simply a statement to the compiler that a type exists while a definition fully describes a type to both the compiler and the linker. It is important to note that every definition is itself a declaration, but a declaration need not be a definition. In other words, it is impossible to define something that hasn't been declared, but you can declare something without defining it. A declaration without a definition is known as a "forward declaration"; a declaration of something that will (eventually) be defined somewhere.

It is also important to note that a type can only be defined once but can be declared more than once, provided those declarations are in separate translation units and are consistent with each other. Since there can only be one definition, that definition must reside in just one translation unit. To ensure all declarations of a type remain consistent, simply place the declaration in a header file and include that header file wherever it is required. It doesn't matter where the definition resides so long as there is exactly one definition in your program. Since headers may also contain definitions (and may include other headers with definitions), it is vital that you use "header guard" macros to ensure those definitions are only included once in every compilation.

The "one definition rule" (ODR) can be broken in one of two ways: either by not providing the definition of a type at all, or by providing two or more definitions for the same type. Both will be picked up by the linker.

Unlike C++, C generally doesn't care if a type is forward declared or not, just so long as it is declared somewhere in the translation unit(s) that use them.

The built-in types do not require either a declaration or a definition; they are defined by the language itself (hence they are built-in). This includes all the fundamental types: primitive data types (int, char, float and double); all modified primitive types (types declared with the signed, unsigned, long and short modifiers); all primitive array types and; all primitive pointer types.

The only type that need not be defined before being used is a function. The declaration of a function is enough for the compiler to determine the function's prototype: the type of the function (the return type) and the type of its argument (if any). So long as the declaration is visible to the translation unit(s) that use the function, the compiler can determine if calls to that function are well-formed. The definition of the function provides the implementation of that function and so long as a definition exists somewhere, the compiler will (eventually) find it and the linker will be able to link it.

Union, struct and enum types have no implementation details in C so a declaration suffices. (In C++, we must define struct methods, if any are declared, just as we must with all class types. A struct with no declared methods is treated the same as a C-style struct).

Typedefs are, by their very nature, always defined. They are type definitions after all. This includes type definitions provided by the C standard library (such as wchar_t) as well as all user-defined aliases.

Although we quite often use the term "declare a variable", variables are not declared they are simply instantiated. To be precise, a variable is an instance of an already declared type and memory is physically allocated to the variable according to that type. When a variable is instantiated, it is said to be in an uninitialised state. In reality, the value will be whatever value happens to reside at the memory address allocated to the variable, however most compilers will warn you if you attempt to use a variable that has not been explicitly initialised.

We also quite often use the term "define a variable" when we assign a value to the variable. However we are not defining the variable, we are either initialising the (uninitialised) variable or we are assigning a new value to the (initialised) variable. The type of the value must be the same as the variable type, or of a type that can be implicitly converted to that type by the compiler.

The same can be said of constants, the only difference being that we initialise constants at the point of instantiation since there is no such thing as an uninitialised constant.

Note that although we often use the terms "declare" and "define" interchangeably and there's nothing actually wrong with using terms such as "declare a variable", they have specific meanings with respect to the compiler.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between declaration and a definition in C?
Write your answer...
Still have questions?
magnify glass
Related questions

What is the difference between declaration and a definition in c plus plus?

A declaration is an incomplete type whereas a definition is a complete type.

What is the difference between declaration and a definition in C with small example?

declaration examples:int main (void);extern int somevariable;definition examples:int main (void) { puts ("Hello world"); return 0; }int somevariable;

What is difference between declaration of variable in c and declaration of variable in c plus plus?

There is no difference, other than that declarations in C++ can also initialise the variable in a single instruction. C example: int x; // declaration x=5; // initialisation C++ example: int y=5; // declaration and initialisation combined.

What is difference between declaration an definition in c language?

The term declaration is used when referring to variables. It means that the compiler is told about the type, name, as well as allocated memory cell for variable of a variable. The later action of the compiler, allocation of storage is called the definition of the variable. Example: Declaration of variable: extern int a; Definition of a variable: int a=5;

What is the difference between class and object in c plus plus?

There is no difference; to define an object in C++ you use the 'class' definition

Difference between traditional c and ansi c?

C is C and ANSI C is ANSI C the best definition for all time.

What is definition and declaration in C?

declaration is like a promise that somewhere else the thing in question will be actually defined.

C programming language bsc it notes?

different between defining value definition section and defining value declaration section

When variable in c gets memory After declaration or initialization?

Definition. Example: extern int x1; /* declaration */ int x2; /* definition */ int x3= 2; /* definition with initialization */

What is difference between declaration and initialization in C programming?

Variable-declaration is: extern int x; extern double y; extern char a; Variable-definition is: int x; static double y; auto char a; Variable-definition with initialization is: int x = 1; static double y= 2.3; auto char a = 'w';

Where do arguments appear within a C program?

within function call, declaration and definition

Difference between class and object in c plus plus?

A class is the definition of a type, while an object is an instance of a class.

People also asked