Category:C: Difference between revisions

m
Fixed syntax highlighting and tidied the examples a bit.
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.
 
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.
<lang C>int main()
{
 
<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!
 
// yourYour main program goes here.
}</lang>
// ifIf 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.
<langsyntaxhighlight Clang="c">{
//this This wouldn't actually compile as none of these variables were declared in this scope. More on that later.
 
if (K == 3) {
X = Y; X = Y; //this This line will be skipped if K doesn't equal 3.
{
}
X = Y; //this line will be skipped if K doesn't equal 3.
Y = Z; //this This is not part of the if statement. It will execute even 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.
 
}</langsyntaxhighlight>
 
===Semicolons===
Line 60:
===Functions===
A function is made up of three parts: its return type, its name, and its arguments.
<syntaxhighlight lang C="c">int main(void) // This is the function "main," which takes no arguments and returns a 32-bit signed integer value.
 
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.</langsyntaxhighlight>
 
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.
<langsyntaxhighlight Clang="c">int foo(int x) {
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); //note Note that x was never involved. That's because the "x" earlier was the formal parameter.
 
}</langsyntaxhighlight>
 
===Assignment===
C allows you to define a variable as equal to a value, in more ways than just simple numerals.
<langsyntaxhighlight Clang="c">int a = 3; //declare Declare the variable a of type int, define it equal to decimal 3.
 
int b = -1; //declare Declare the variable b of type int, define itand equal to -1 (0xFFFFFFFF in hex).
 
char letter = "A";
//declare Declare the variable "letter" of type char, itand equal equalsto capital 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"; //define Define the array "myString" containing the letters "Hello" followed by a null terminator.
 
int myArray[5] = {10, 20, 30, 40, 50};
//declare Declare the array variable "myArray" containing integer values, with a maximum size of 5 elements.
// Then assign 10 to the beginning, 20 after it, 30 after that, and so on.
 
int c = sum(a, b);
//declare Declare the integer variable "c".
// 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."</langsyntaxhighlight>
 
===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:
<langsyntaxhighlight Clang="c">int a; // The variable "a" has been declared, but not defined.
a = 2; // Now the variable has been defined.</langsyntaxhighlight>
 
<langsyntaxhighlight Clang="c">int a = 2; // The variable "a" has been both declared and defined.</langsyntaxhighlight>
 
* You cannot define a variable without declaring it first.
Line 130 ⟶ 129:
 
Examples:
<langsyntaxhighlight Clang="c">unsigned int x;
volatile int HorizontalScroll;</langsyntaxhighlight>
 
Functions are declared in a similar fashion to variables, except a function's "type" is the type of the value it returns.
<langsyntaxhighlight Clang="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.</langsyntaxhighlight>
 
==Citation==
*[[wp:C_%28programming_language%29|Wikipedia:C (programming language)]]
 
==Todo==
* [[Tasks not implemented in C]]
 
 
9,476

edits