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.) |
||
(27 intermediate revisions by 2 users not shown) | |||
Line 25:
* '''C11''' is the previous standard, published in December 2011. It is the default for [[GCC]] as of version 5.1.
* '''C18''' is the current standard, published in June 2018. It is the default for [[GCC]] as of version 8.1.
* '''C2x''' is the upcoming standard,
==Overview==
===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.
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.
<syntaxhighlight lang="c">{
// This wouldn't actually compile as none of these variables were declared in this scope. More on that later.
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.
}</syntaxhighlight>
===Semicolons===
Any "executable" statement must end in a semicolon, such as an assignment or function call. If you get an error message from your compiler, it won't explicitly tell you "Expected semicolon at end of line X." Go to the line number it says the error is at, and look a few lines <i>above</i> that. You might have forgotten a semicolon there.
===Scope===
Line 41 ⟶ 59:
===Functions===
A function is made up of three parts: its return
<
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.</syntaxhighlight>
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.
<syntaxhighlight lang="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 that x was never involved. That's because the "x" earlier was the formal parameter.
}</syntaxhighlight>
===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
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);
// 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."</syntaxhighlight>
===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:
<syntaxhighlight lang="c">int a; // The variable "a" has been declared, but not defined.
a = 2; // Now the variable has been defined.</syntaxhighlight>
<syntaxhighlight lang="c">int a = 2; // The variable "a" has been both declared and defined.</syntaxhighlight>
* You cannot define a variable without declaring it first.
* Before a variable can be used, it must be defined.
===Types===
C has the following types built in by default, but you can create your own based on these using the <code>typedef</code> directive. This is not an exhaustive list. Some of these names will have different meanings depending on the hardware you're programming for.
* <code>char</code>: an 8 bit value, typically used to represent ASCII characters.
* <code>short</code>: a 16 bit value.
* <code>int</code>: a 32 bit value.
* <code>struct</code>: a collection of several other values, stored consecutively in memory. Each can be a different type.
* <code>union</code>: a variable that can hold several different types of data, but only one at a time.
* <code>float</code>: a single-precision (32-bit) floating-point decimal value.
You can also add a few modifiers in front of the variable type to be more specific:
* <code>unsigned</code> tells the compiler that this variable is always treated as positive. Computers use two's complement to represent negative numbers, meaning that if the leftmost bit of a number's binary equivalent is set, the value is considered negative. The resulting assembly code will use unsigned comparisons to check this variable against other variables.
* <code>volatile</code> tells the compiler that this variable's value can changed by the hardware. This is commonly used for hardware registers such as those that track the mouse cursor's location, a scanline counter, etc. The value will always be read from its original memory location, ensuring that its value is always up-to-date.
Examples:
<syntaxhighlight lang="c">unsigned int x;
volatile int HorizontalScroll;</syntaxhighlight>
Functions are declared in a similar fashion to variables, except a function's "type" is the type of the value it returns.
<syntaxhighlight 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.</syntaxhighlight>
==Citation==
*[[wp:C_%28programming_language%29|Wikipedia:C (programming language)]]
==Todo==
* [[Tasks not implemented in C]]
|