Category:C: Difference between revisions

no edit summary
No edit summary
Line 82:
//Define it to equal the output of the function sum using the previously defined variables "a" and "b" as arguments.
//This is only valid if the return type of the function "sum" matches the type of the variable "c."</lang>
 
===Declaring vs. Defining===
This is a very unintuitive aspect of C that often confuses new users. Declaring a variable or function tells the compiler that a function may exist. Defining a variable or function assigns it a value or procedure, respectively. Compare the two examples below:
<lang C>int a; // The variable "a" has been declared, but not defined.
a = 2; // Now the variable has been defined.</lang>
 
<lang C>int a = 2; //The variable "a" has been both declared and defined.</lang>
 
* You cannot define a variable without declaring it first.
* Before a variable can be used, it must be defined.
 
Functions work the same way. You can declare a function without defining what it does.
<lang C> int foo(int bar);
// The function foo was declared. It takes an integer as an argument and returns an integer.
// What it actually does is currently unknown but can be defined later.</lang>
 
 
==Citation==
*[[wp:C_%28programming_language%29|Wikipedia:C (programming language)]]
1,489

edits