• C Programming
  • C++ Programming
  • The Difference Between

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

Top Answer
User Avatar
Wiki User
Answered 2010-10-04 21:46:21

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

User Avatar
Wiki User
Answered 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.

User Avatar

Your Answer

Still have questions?

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.

Is the declaration of independence and the US Constitution the same thing?

NOPE for more info c my answer to what is the difference between the declaration of independance and the us constitution? -Superknowitall12345

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 */

C programming language bsc it notes?

different between defining value definition section and defining value declaration section

What is definition and declaration in C?

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

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.

What is the difference between a class and an object of a class in C plus plus?

A class is a type definition. An object is an instance of a class.

What is the difference between initialisation and definition in C programming?

A discussion of initialization and definition should also include a discussion of declaration. Declaration is the point in compilation where an object is described to the compiler. At that point, the compiler knows the type and size of the object. Definition is the point in compilation where an object is allocated by the compiler. At that point, the compiler knows the address of the object. Initialization is the point in compilation where an object is assigned an initial value. At that point, the compiler knows the value of the object.

Can you explain the structure of a C program?

Something like this: program -> <empty> program -> type-definition program program -> type-declaration program program -> variable-definition program program -> variable-declaration program program -> function-definition program program -> function-declaration program

What is the difference between C and advanced C?

The difference between C and the advanced C is that C is basic. On the other hand, the advanced C is thorough and to the detail.

Difference between C and C programming language?

The C and C programming languages are one and the same. There is no difference between those languages.

What is difference between c and c?


What is the difference between c c?


What is the use of extern in c language?

In C and C++, extern is a declaration of a variable or function, said declaration including the fact that the definition of that variable or function will occur in a different compilation unit, linking provided by the link editor or binder.

What is the difference between solipsism and solipcism?

What is the difference between a C; and an S?

What is the difference between a function definition and function prototype in visual c plus plus?

//function prototype int myFunction(char a); //function definition int myFunction(char a) { ...//your code }

What is the difference between the c and C programming?

The question is similar to : What is the Difference between "English" and "Talking in English" "C" is a Language. "C Programming" is a verbal usage

Difference between c structure and c plus plus structure?

The definition of the structure in C is limited to within the module and cannot be initialized outside its scope. Where as in C++ you can initialize the objects anywhere within the boundaries of the project.