Category:C: Difference between revisions
m
Fixed syntax highlighting and tidied the examples a bit.
Puppydrum64 (talk | contribs) m (→Functions) |
m (Fixed syntax highlighting and tidied the examples a bit.) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 30:
===Curly Braces===
C uses curly braces as a separator for sections of code. All curly braces must be "balanced," i.e. every left curly brace must have a right curly brace after it. Nesting curly brace pairs inside curly braces is also acceptable as long as none of them are "lonely."
Most advanced code editors will help you with curly braces by automatically typing the right brace as soon as you type the left one. It is a matter of style as to whether you prefer to place an opening curly brace on its own line or at the end of the previous line. Here we use the latter style.
<syntaxhighlight lang="c">int main() {
// your main program goes here▼
// if you forgot either of these curly braces you would get an error message when you try to compile!▼
▲ //
}</syntaxhighlight>
The contents of a function, if statement, etc. <b>must</b> be enclosed in curly braces for the code to count as part of that section.
<
//
if (K == 3) {
}
▲X = Y; //this line will be skipped if K doesn't equal 3.
▲Y = Z; //this is not part of the if statement. It will execute even if K doesn't equal 3.
}</
===Semicolons===
Line 60:
===Functions===
A function is made up of three parts: its return type, its name, and its arguments.
<syntaxhighlight lang
int sum(int a, int b) // This is the function "sum," which takes two integer arguments and returns an integer.
void PlaySound(char songName)
// This takes a character string as an argument and presumably sends a command to sound hardware.
// It returns no values. Functions that have a return value of "void" typically do some sort of
// procedure whose outcome does not need to be measured or remembered later.</
Note that the variable names listed as arguments when declaring a function are known as <i>formal parameters</i> and only are there to define the function. Variables with those names need not be declared or defined in your actual function, nor do they refer to any variables in your program that happen to have the same name. Essentially, formal parameters act as placeholders for the actual function parameters that you'll be using.
<
return x;
} // "x" doesn't need to be a variable in your real program. If it is, that's not related in any way to the "x" here.
int main() {
int y;
int z = 2;
y = foo(z);
}</
===Assignment===
C allows you to define a variable as equal to a value, in more ways than just simple numerals.
<
int b = -1; //
char letter = "A";
//
// C allows you to treat an ascii value as its numeric equivalent whenever you feel like it. Other languages do not.
char *myString = "Hello"; //
int myArray[5] = {10, 20, 30, 40, 50};
//
// Then assign 10 to the beginning, 20 after it, 30 after that, and so on.
int c = sum(a, b);
//
// Define it to equal the output of the function sum using the previously defined variables "a" and "b" as arguments.
// When this line of code is executed, the computer will perform the function "sum(a,b)" and store the result in c.
// This is only valid if the return type of the function "sum" matches the type of the variable "c."</
===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:
<
a = 2; // Now the variable has been defined.</
<
* You cannot define a variable without declaring it first.
Line 130 ⟶ 129:
Examples:
<
volatile int HorizontalScroll;</
Functions are declared in a similar fashion to variables, except a function's "type" is the type of the value it returns.
<
// 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.</
==Citation==
*[[wp:C_%28programming_language%29|Wikipedia:C (programming language)]]
==Todo==
* [[Tasks not implemented in C]]
|