What is the difference between defining and declaring a variable in c




















Overall, it is possible to declare and define a variable. What is a Declaration in C -Definition, Functionality 2. What is a Definition in C -Definition, Functionality 3. The declaration of a variable provides information to the compiler about the variable type and the name. In other words, this information indicates the compiler of the existing variable. Therefore, the compiler can proceed further compilation without all the details about the variable.

Variable declaration is useful when the programmer uses multiple files and when he defines the variables in one of the files. They are available when linking the program.

Even though it is possible to declare a variable multiple times in a C program, it is only possible to define it only once such as in a file or a function. A Function definition defines the function itself. It also acts as a declaration, and if the declaration includes the types of its parameters, a prototype as well. Naveen 01 Jul There is a lot of difference in both of these.

Declaration of any variable or a function means that your are specifying that the following variable or funtion will be used in your program. Where as in definition you exactly specify the value or some properties of the variable or the function. For Example: For declaration of any user defined function 'say, addition the syntax is: int addition ; Here the above line specifies that the function is an integer function, addition is the name of this function and semicolon after the specifies that this is a declaration.

In C language the function definition and declaration is generally done on the top of the main program. Other Related Discussions. Please Help. Need help in doing a simple "crossword" between two words.

Which is the best website to teach kids coding? Error in my C- code. Hello Friends,. Implementation of forward and bidimensional iterators in C.

Ask a Question. Formatting: Spelling, grammar or punctuation errors. Personal Attack: Disrespectful content about a person.

Insincere Question: Question not seeking real answers. When you declare a variable, a function, or even a class all you are doing is saying: there is something with this name, and it has this type. The compiler can then handle most but not all uses of that name without needing the full definition of that name.

Declaring a value--without defining it--allows you to write code that the compiler can understand without having to put all of the details. This is particularly useful if you are working with multiple source files, and you need to use a function in multiple files. You don't want to put the body of the function in multiple files, but you do need to provide a declaration for it. This is a function declaration; it does not provide the body of the function, but it does tell the compiler that it can use this function and expect that it will be defined somewhere.

Defining something means providing all of the necessary information to create that thing in its entirety. Defining a function means providing a function body; defining a class means giving all of the methods of the class and the fields. Once something is defined, that also counts as declaring it; so you can often both declare and define a function, class or variable at the same time.

But you don't have to. For example, having a declaration is often good enough for the compiler. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What is the difference between a definition and a declaration? Ask Question. Asked 12 years, 2 months ago.

Active 8 months ago. Viewed k times. The meaning of both eludes me. Improve this question. MD XF 7, 7 7 gold badges 36 36 silver badges 64 64 bronze badges.

Maciek Maciek Frankly, I had a lot of trouble learning which was which, so I didn't find the names obvious. I had no problem with the meanings, just which names to associate with the meanings. We've been over this in exhaustive detail: stackoverflow. DavidThornley I use this trick: a definition gives a finer description of a given variable or function. To remember this, I recall that the middle of the word "definition" has a resemblance to the word "finer".

MarcoLeogrande: Or "definite"! Show 5 more comments. Active Oldest Votes. Amongst them: [Example: [ Improve this answer. Community Bot 1 1 1 silver badge. For example, 6. Brian: "extern int i;" says that i is an int somewhere, don't worry about it. Brian: You're wrong. You can have as many extern int i in each compilation unit as you want. It denotes the space for the integer to be in this translation unit and advices the linker to link all references to i against this entity.

If you have more or less than exactly one of these definitions, the linker will complain. These amount to the same thing, which is what sbi says: in both cases this declaration specifies the object to which all references to "i" in that scope must be linked. It's allowed elsewhere though. What is a definition and what a declaration has subtle rules when it comes to templates - beware!

Show 25 more comments. Felipe Augusto 6, 7 7 gold badges 29 29 silver badges 64 64 bronze badges. Michael Kristofik Michael Kristofik RJFalconer You're correct; initialization does not necessarily turn a declaration into a definition contrary to what one might expect; certainly I found this surprising.

Your modification to the example is actually illegal unless b is also declared const. See stackoverflow. This is interesting to me. Different ways to look at it, I suppose.

Declaration is for the compiler to accept a name to tell the compiler that the name is legal, the name is introduced with intention not a typo. Definition is where a name and its content is associated. The definition is used by the linker to link a name reference to the content of the name. Add a comment. Declaration: "Somewhere, there exists a foo. Johannes Schaub - litb Johannes Schaub - litb k gold badges silver badges bronze badges. Declaration Declarations tell the compiler that a program element or name exists.

Definition Definitions specify what code or data the name describes. Um, isn't it that you can even define classes and enums in each compilation unit? At least I put class definitions into my headers and include them all over. However, "class foo;" is a declaration. It tells the compiler that foo is a class.

It tells the compiler exactly what sort of class foo is. The exception are class member names which may be used before they're declared. Yeah, that's what i meant. It's visible before its declaration in "all function bodies, default arguments, constructor ctor-initializers".

Not in the return type : — Johannes Schaub - litb. Yeah, I know, the effect is the same for many cases. But not for all cases, which is why I think we should use the precise explanation.

It is visible in default arguments?



0コメント

  • 1000 / 1000