Category:C: Difference between revisions

1,316 bytes added ,  9 months ago
m
Fixed syntax highlighting and tidied the examples a bit.
No edit summary
m (Fixed syntax highlighting and tidied the examples a bit.)
 
(7 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, plannedexpected forto abe 2021voted publicationon in 2023 - it will then become C23. [[GCC]] 9 and, [[Clang]] 9 and Pelles C 11 have preliminary support for it.
 
==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. 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.
<syntaxhighlight lang="c">{
<lang C>int main()
// 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 59 ⟶ 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 justknown foras convenience<i>formal parameters</i> and only are there to define the function. TheyVariables with those names need not be declared noror defined in your actual function, nor do they refer to any variables in your program that happen to have the same name. It'sEssentially, onlyformal whenparameters aact functionas isplaceholders actuallyfor <i>used</i>the areactual thefunction argumentparameters variablesthat requiredyou'll tobe existusing.
<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.
} // the "x" here is just a placeholder for whatever actually goes in when you invoke foo.
 
int main() {
{
 
int y;
int z = 2;
 
y = foo(z); //note Note that x was never involved. That's because the "x" earlier was just athe placeholderformal nameparameter.
 
}</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 116:
 
===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:
Line 126 ⟶ 129:
 
Examples:
<langsyntaxhighlight Clang="c">unsigned int x;
volatile int HorizontalScroll;</langsyntaxhighlight>
 
Functions workare thedeclared samein way.a Yousimilar canfashion declareto variables, except a function's without"type" definingis the type of the whatvalue it doesreturns.
<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