The last part is the code that is to be executed and belongs to the main function. Curly brackets { } define the beginning and end of the function. This example contains only one print statement to be executed. Note how the code within the curly brackets is indented (using a tab or spaces) to show that it belongs to the main function block. This indentation is not required but helps readers see the program structure. Instructions must end with a semicolon ';'. Double forward slashes allow comments to be added at the end of an instruction. The include statement has a # symbol in front of it.
<pre class="brush:c">include "KMotionDef.h"main(){ printf("Hello World!\n"); // send message to console}</pre> ===<span id="C Curly Brackets" class="mw-headline">C Curly Brackets { }<br data/></span>===<span class="mw-attributesheadline">Often is is necessary to group a number of individual C instructions into a group of instructions that can then be treated as a single instruction. The name given to this is a "Block" of instructions. The beginning and end of the block are marked with Curly Brackets. Some other programming languages use the words "begin" and "end" for this purpose. C Language uses { } brackets. Here is an example of 3 instructions (2 arithmetic assignment instructions and a function call instruction) grouped into a Block:</span> <pre class="%20brush:c">{ X = 1 + 1; Y = X + 1; Move(1,100);}</pre> The begin curly bracket and end curly bracket mark the beginning and end of the block. Notice the curly brackets are on separate lines and we indent the instructions in the block by 4 spaces (or a tab) to make it easy to read, to be able to easily see where the blocks begin and end, and to easily see which end bracket matches with which beginning bracket. This style doesn't matter to the compiler. The code written below would produce exactly the same result: <pre class="brush:c">main {X = 1 + 1; Y = X + 1; Move(1,100);}<br data/pre> But please take the time to format your code properly to make it easier to read and help yourself avoid bugs. The Microsoft Visual Studio Editor has a nice feature to automatically format C Code. See the Edit | Advanced | Format Document option. <span class="mw-attributesheadline"> By default most C operations (like if/else, for loops, while loops, functions, etc ) target a single instruction. Take for example an "if" statement. If the condition is true the next single instruction only will be executed and if the condition is not true then the next single instruction will be skipped. Here is an example:<br /></span> <pre class="%20brush:c">if (condition) X = 2;Y = X + 1;</pre>In this case only the X = 2 is the only instruction that is part of the "if". The Y = X + 1 instruction will always be executed. This is true regardless of how the lines are indented or formatted. If our desire is to have both instructions to be part of the if condition then it is necessary to put the two instructions in a block like this: <pre class="brush:c">if (condition){ X = 2; Y = X + 1;}<br data-attributes/pre> ===<span id="%20/C Functions" class="mw-headline"> printfC Functions (Subroutines)</span>===A C Function is a list of C instructions than can be called to execute from other places in a C Program. <pre class="Hello Worldbrush:c">// define function to Enable, Move, and wait with two parametersvoid EnableMoveWait(int Axis, double dist){ EnableAxis(Axis); // enable the Axis Move(Axis,dist); // move the Axis while (!\nCheckDone(Axis)) ; // wait until done moving}</pre> This can be useful to simply group the instructions together as a single operation to make a program more understandable. Furthermore it can be useful if the same operations need to be performed multiple places in a program to avoid repeating all the instructions multiple times. Instead of repeating the same block of instructions multiple places the block of instructions is formed into a function with a name and then the function can be "called" from multiple places in the program. A Function can also have "parameters". This is useful when the group of instructions that might occur at different places in the program are not exactly the same but only very similar. The parameters can be used to allow the caller to substitute values in the function so the same function can be used in different circumstances. The number of parameters and type of each parameter must be defined in the function. Although parameter types can be anything they are most commonly: Integers (int), Single precision floating point numbers (float), double precision floating point numbers (double), or character strings (char *). void EnableMoveWait(<span style="color: #ff0000;">'''int Axis, double dist'''</span>) A Function may have a return value. Some other programming languages (ie Basic) have Subroutines and Sub Functions that are declared in different ways. A Subroutine is a list of instructions. A Sub Function is a list of instructions that also returns a value. The C language treats both the same as Functions. If nothing needs to be returned by the function then the return type can be specified as "void". <span style="color: #ff0000;">'''void'''</span> EnableMoveWait(int Axis, double dist) If a function doesn't require any parameters the parameter list should be specified as "void". A common mistake is specify nothing for the parameter list. This is telling the compiler to accept any parameters and is usually not desirable. A function with no parameters and no return value should be specified as: <span style="color: #ff0000;">'''void'''</span> SomeFunction(<span style="color: #ff0000;">'''void'''</ send message span>) There is a special function called "main". This is the most top level of the C Program and is the function that is first called when the C Program begins. Below is a program with duplicated code. It performs the same 3 similar operations on 3 different axes to consoleEnable, Move, and Wait an Axis. Notice the same operations are performed each time with only two differences: the Axis and the distance moved. So by using a function with two parameters we can replace the 3 instructions with a single function call. The first parameter is which Axis which is an integer value. The second parameter is the distance to move which is a double precision value. <br datapre class="brush:c">include "KMotionDef.h"main(){ EnableAxis(0); // enable the Axis Move(0,1000.0); // move the Axis while (!CheckDone(0)) ; // wait until done moving EnableAxis(1); // enable the Axis Move(1,2000.0); // move the Axis while (!CheckDone(1)) ; // wait until done moving EnableAxis(2); // enable the Axis Move(1,-attributes3000.0); // move the Axis while (!CheckDone(2)) ; // wait until done moving}</pre> Now declaring a function named EnableMoveWait with 2 parameters (and void return value) we can simplify the C Program, lessen the number of total instructions in the program, and most importantly guarantee that the exact same operations are performed for each axis. <pre class="%20brush:c">include "KMotionDef.h" // define function to Enable, Move, and wait with two parametersvoid EnableMoveWait(int Axis, double dist){ EnableAxis(Axis); // enable the Axis Move(Axis,dist); // move the Axis while (!CheckDone(Axis)) ; // wait until done moving} main(){ EnableMoveWait(0, 1000.0); // call function for Axis 0 EnableMoveWait(1, 2000.0); // call function for Axis 1 EnableMoveWait(2,-3000.0); // call function for Axis 2}</pre> It is often desirable to place the functions toward the end of file with the main starting point of the program toward the beginning. This tends to make the program more readable and logical. This is not necessary but more a personal preference. Because the C Compiler only reads the program one time from beginning to end it is good to place at the beginning a description of any Functions that exist in the program. The allows the compiler to know the names of any functions and what parameter and return value types they have before they are used in the program. It is critical that the parameters passed in the call to the function are the exact correct number and type that are defined to be used by the function itself. So to inform the compiler of what function to expect a "function prototype" is specified. This is exactly the same as the function declaration itself except with no body of instructions. It consists of only one line followed by a semicolon to indicate it is only a prototype and not the entire function. See the Program below that has the function moved below the main function and with a function prototype at the beginning: <pre class="brush:c">include "KMotionDef.h" // define function prototype of function to expect latervoid EnableMoveWait(int Axis, double dist); main(){ EnableMoveWait(0, 1000.0); // call function for Axis 0 EnableMoveWait(1, 2000.0); // call function for Axis 1 EnableMoveWait(2,-3000.0); // call function for Axis 2} // define function to Enable, Move, and wait with two parametersvoid EnableMoveWait(int Axis, double dist){ EnableAxis(Axis); // enable the Axis Move(Axis,dist); // move the Axis while (!CheckDone(Axis)) ; // wait until done moving}</pre> Final C Program shown below with annotations [[File:SimpleFunction.png|none|link=]]
===<span id="Basic_Disk_Read.2FWrite" class="mw-headline">Basic Disk Read/Write</span>===