answersLogoWhite
C Programming
C++ Programming
The Difference Between

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

505152

Top Answer
User Avatar
Wiki User
Answered
2010-10-04 21:46:21
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.

001
๐Ÿฆƒ
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0

User Avatar
Wiki User
Answered
2014-11-13 23:17:42
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.

001
๐Ÿฆƒ
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0

Related Questions


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


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


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.


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;


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


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


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


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


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


different between defining value definition section and defining value declaration section


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';


within function call, declaration and definition


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


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


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.


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.


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


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


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


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.


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


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


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.


During function call and function definition and Declaration. These are the three places where arguments appear in C Program.


Forget about major. There is not even minor difference between C and C



Copyright ยฉ 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.