Difference between revisions of "TestSandbox"

From ROBOTC API Guide
Jump to: navigation, search
(Code Editor)
 
(13 intermediate revisions by one user not shown)
Line 1: Line 1:
{{DISPLAYTITLE:{{SUBPAGENAME}}}}
+
<yambe:breadcrumb self="Strings">General|General Programming</yambe:breadcrumb>
<yambe:breadcrumb self="ROBOTC Interface">Tutorials|Tutorials</yambe:breadcrumb>
+
 
<br />
 
<br />
  
{{toc}}
+
{|
<br />
+
 
+
== Overview ==
+
{|width="740px"
+
 
|-
 
|-
|ROBOTC for Mindstorms is an Integrated Development Environment. It has been developed to provide as much support as possible for the platforms it is compatible with. The ROBOTC interface will modify itself to accommodate the functionality found in your controller. ROBOTC extends the ‘C’ programming language with a number of built-in variables and functions to provide control over a robot’s hardware devices, i.e. the motors and sensors.<br />
+
|''For information about displaying on the NXT LCD, please see the article: [[NXT_Functions_Display|NXT Display Functions]].''
  
[[File:ROBOTC_IDE.png]]
+
''For information about displaying on the VEX LCD, please see the article: [[VEX2_Functions_Display|Cortex Display Functions]].''
  
  
There are three main sections to the ROBOTC IDE:
+
When displaying floats, for example, you can tell ROBOTC how many decimals places to display.  This is standard across all 'C' - like programing languages.  For example, if your float is <span class="keywordB">PI</span> '''(3.14159265)''', but you only want to dispay '''"3.14"''', your string should contain, <span class="codePunc">"%</span><span class="codeStringsNums">1.2f</span><span class="codePunc">"</span>.
#The Editor
+
#The Code Templates / Function Library
+
The number '''''before''''' the decimal is how many digits '''''before''''' the decimal you wish to display, while the number '''''after''''' the decimal is how many digits '''''after''''' the decimal you wish to display.  So <span class="codePunc">"%</span><span class="codeStringsNums">1.2f</span><span class="codePunc">"</span> tells us to display one digit before the decimal and two digits after the decimal, with '''"3.14"''' as the final result.
#The Main Menu / Toolbar
+
|-
 
|}
 
|}
 
<br />
 
<br />
  
=== Editor ===
+
{{tl|1|1}}
{|width="740px"
+
<br />
 +
 
 +
== sprintf ==
 +
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
| class="functionType"|
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">sprintf</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">string </span>
 +
<span class="bigCodeBasic">&sString</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const string </span>
 +
<span class="bigCodeBasic">sFormatString</span>
 +
<span class="bigCodePunc">,  </span>
 +
<span class="bigCodeBasic">...</span>
 +
<span class="bigCodePunc">)</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) This function writes the data from <span class="codeBasic">sFormatString</span> to <span class="codeBasic">&sString</span>. Identical to the function found in conventional C 'string.h' library.
 +
|-
 +
|
 +
  {| class="parameterTable" cellpadding="5%" width="100%"
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 +
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 +
  |-
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|''&sString''
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|This is the destination string.
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|''sFormatString, ...''
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|These are the source strings.
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  |}
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
task main()
 +
{
 +
  string srce = "ROBOTC"; // create source variable 'srce' and set it to "ROBOTC"
 +
  string dest;            // create destination variable 'dest'
 +
 
 +
  wait1Msec(1000);        // wait 1 second
 +
 
 +
  sprintf(dest, srce);    // write to 'dest', the contents of 'srce' (dest <- "ROBOTC)
 +
 
 +
  while(true);            // infinite loop (so we can watch the debbugger)
 +
}
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 
|-
 
|-
|This is the part of the interface where the user can write code.<br />
 
[[File:ROBOTC-IDE_Editor.png]]
 
 
|}
 
|}
 
<br />
 
<br />
  
=== Code Templates / Function Library ===
+
<!-- BROKEN
{|width="740px"
+
== sprintf ==
 +
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
| class="functionType"|
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">sprintf</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">string </span>
 +
<span class="bigCodeBasic">&sString</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const string </span>
 +
<span class="bigCodeBasic">sFormatString</span>
 +
<span class="bigCodePunc">, )</span>
 +
<span class="bigCodeBasic">...</span>
 +
<span class="bigCodePunc">)</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) This function writes the data from <span class="codeBasic">sFormatString</span> to <span class="codeBasic">&sString</span>. Identical to the function found in conventional C 'string.h' library.
 +
|-
 +
|
 +
  {| class="parameterTable" cellpadding="5%" width="100%"
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 +
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 +
  |-
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|''&sString''
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|This is the destination string.
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|''sFormatString, ...''
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|These are the source strings.
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  |}
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
task main()
 +
{
 +
  string srce = "ROBOTC"; // create source variable 'srce' and set it to "ROBOTC"
 +
  string dest;            // create destination variable 'dest'
 +
 
 +
  wait1Msec(1000);        // wait 1 second
 +
 
 +
  sprintf(dest, srce);    // write to 'dest', the contents of 'srce' (dest <- "ROBOTC)
 +
 
 +
  while(true);            // infinite loop (so we can watch the debbugger)
 +
}
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 
|-
 
|-
|This portion of the interface allows user to see all the functions available in ROBOTC at their user level. Portions of code from the "Function Library" can be dragged into the Editor.<br />
 
[[File:ROBOTC-IDE_Toolbar.png]]
 
 
|}
 
|}
 
<br />
 
<br />
 +
-->
  
=== Main Menu / Toolbar ===
+
== strcat ==
{|width="740px"
+
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
| class="functionType"|
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">strcat</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigCodeBasic">&pToBuffer</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const void </span>
 +
<span class="bigCodeBasic">&pFromBuffer</span>
 +
<span class="bigCodePunc">)</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Function concatenates <span class="codeBasic">pFromBuffer</span> onto end of <span class="codeBasic">pToBuffer</span>. The variables are arrays of bytes terminated with zero character. It is user responsibility to ensure that the 'To' array is large enough to hold the result. ROBOTC is not able to do any range checking! Identical to the function found in conventional C 'string.h' library.
 +
|-
 +
|
 +
  {| class="parameterTable" cellpadding="5%" width="100%"
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 +
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 +
  |-
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|''pToBuffer''
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|Characters from 'pFromBuffer' are concatenated ''to the end'' of this.
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|''pFromBuffer''
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|Concatenates characters ''from'' here.
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  |}
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
string str1 = "ROBOT";      // string 'str1' is "ROBOT"
 +
string str2 = "C";          // string 'str2' is "C"
 +
 
 +
strcat(str1, str2);          // concatinate string 'str2' onto string 'str1'
 +
 
 +
while(true);  // infinite loop (to keep debugger running)
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 
|-
 
|-
|This area allows you to perform basic tasks (saving, copy & pasting, undo, etc.), switch between multiple open programs using the tabbed interface, and access all of ROBOTC's additional functionality through the various menus.<br />
 
[[File:ROBOTC-IDE_Templates.png]]
 
 
|}
 
|}
 
<br />
 
<br />
  
== Code Editor ==
+
== strcmp ==
=== Writing Code ===
+
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
{|width="740px"
+
 
|-
 
|-
|Writing Code is the primary focus of ROBOTC, being a C-Based development environment. The ROBOTC Code Editor provides some visual assistance while programming with the use of line numbers and color coding:<br /><br />
+
| class="functionType"|
[[File:ROBOTC-2010.01.13-13.14.29.png]]<br />
+
<span class="bigKeywordBI">short </span>
 +
<span class="bigKeywordB">strcmp</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigCodeBasic">&pString1</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const void </span>
 +
<span class="bigCodeBasic">&pString2</span>
 +
<span class="bigCodePunc">)</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_short|short]]) Function compares <span class="codeBasic">pString1</span> with <span class="codeBasic">pString2</span>. Returns negative value if less than, 0 if equal and positive value if greater than. The variables are arrays of bytes terminated with a zero char. Identical to the function found in conventional C 'string.h' library.
 +
|-
 +
|
 +
  {| class="parameterTable" cellpadding="5%" width="100%"
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 +
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 +
  |-
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|''pString1''
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|A string to compare with 'pString2'.
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|''pString2''
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|A string to compare with 'pString1'.
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  |}
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
string str1 = "Fett";                      // string 'str1' is "Fett"
 +
string str2 = "Fett";                      // string 'str2' is "Fett"
  
Normal Text is displayed as <span class="codeBasic">black text</span>. Normal text is used for variable and function names that are user-defined and not recognized by the compiler as reserved words.
+
if(strcmp(str1, str2) < 0)                    // if 'str1' < 'str2':
 +
{
 +
  // 'str1' is < 'str2'
 +
}
 +
else if(strcmp(str1, str2) > 0)              // if 'str1' > 'str2':
 +
{
 +
  // 'str1' is > 'str2'
 +
}
 +
else                                          // if 'str1' == 'str2':
 +
{
 +
  // 'str1' is = 'str2'
 +
}
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 +
|-
 +
|}
 +
<br />
  
Comments are displayed as <span class="codeComments">green text</span> . Comment are any bits of text in a program prefaced with a "<span class="codeComments">//</span>" or a "<span class="codeComments">/*</span>". This "commented text" is not considered code and is ignored when the compiler is generating byte-code to send to the robot controller.
+
== strcpy ==
 +
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
| class="functionType"|
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">strcpy</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigCodeBasic">&pToBuffer</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const void </span>
 +
<span class="bigCodeBasic">&pFromBuffer</span>
 +
<span class="bigCodePunc">)</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Function copies <span class="codeBasic">pFromBuffer</span> to <span class="codeBasic">pToBuffer</span>. The variables are arrays of bytes terminated with a zero character. It is user responsibility to ensure that the 'To' array is large enough to hold the result. ROBOTC is not able to do any range checking! Identical to the function found in conventional C 'string.h' library.
 +
|-
 +
|
 +
  {| class="parameterTable" cellpadding="5%" width="100%"
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 +
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 +
  |-
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|''pToBuffer''
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|Copies 'pFromBuffer' to this.
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|''pFromBuffer''
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|Copies this to 'pToBuffer'.
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  |}
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
string str1 = "";            // 'str1' is "" (empty)
 +
string str2 = "DNA";        // string 'str2' is "DNA"
  
Reserved Words/Pre-Defined Functions (int, motor) are displayed as <span class="keywordB">bold blue text</span> and <span class="keywordBI">italicized blue text</span>.
+
strcpy(str1, str2);          // copy string 'str2' onto string 'str1'
  
Constants and Parameters (127, port1) are displayed as <span class="codeStringsNums">red text</span>.
+
while(true);  // infinite loop (to keep debugger running)
 
+
</syntaxhighlight>
Syntax and Operands (+, -, *, {, <, [, etc.) are displayed as  <span class="codePunc">light red text</span>.
+
  |-
 +
  |}
 +
|-
 
|}
 
|}
 
<br />
 
<br />
  
=== Error Display ===
+
== StringDelete ==
{|width="740px"
+
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
|At compilation time, the ROBOTC compiler analyzes your code to identify syntax errors, capitalization and spelling mistakes, and code inefficiency, such as unused variables and redundant code. The ROBOTC compiler also has a powerful code optimizer that can shrink your program size by up to 50% before sending it to the robot to preserve memory space on your controller.
+
| class="functionType"|
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">StringDelete</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">string </span>
 +
<span class="bigCodeBasic">&sDest</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const int </span>
 +
<span class="bigCodeBasic">nIndex</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const int </span>
 +
<span class="bigCodeBasic">nSize</span>
 +
<span class="bigCodePunc">)</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Deletes a substring from a string.
 +
|-
 +
|
 +
  {| class="parameterTable" cellpadding="5%" width="100%"
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 +
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 +
  |-
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|''sDest''
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|The string to delete from.
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|[[Data_Types#dataType_string|string]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 1px 0px"|''nIndex''
 +
  | style="border-style: solid; border-width: 0px 0px 1px 0px"|The index of the first character to delete (1st character = index 0).
 +
  | style="border-style: solid; border-width: 0px 0px 1px 0px"|[[Data_Types#dataType_int|int]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|''nSize''
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|The number of characters to delete, starting 'nIndex'.
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|[[Data_Types#dataType_int|int]]
 +
  |-
 +
  |}
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
string str = "mesGARBAGEsage";        // String 'str' is "mesGARBAGEsage".
  
The Error display screen reports the number of errors in your code, as well as their types. Double-clicking a compiler message in the Error display screen will highlight the relevant line of code in your program. The "Previous", "Select", and "Next" buttons can also be used to cycle through and select the errors in your program.
+
StringDelete(str, 3, 7);              // Delete from index 3 to the 7th char after that.
 +
                                      /* Confusing because 3 = index 3, not char 3.  Since indexing */
 +
                                      /* starts with 0, index 3 is the 4th character.               */
 +
 +
while(true);  // infinite loop (to keep debugger running)
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 +
|-
 +
|}
 +
<br />
  
Depending on the type of error, ROBOTC will only be able to highlight the approximate location of the error. For instance, in the example below the missing semicolon is on line 6, but ROBOTC will highlight line 7.<br /><br />
+
== StringFind ==
[[File:ROBOTC-2010.01.13-13.17.20.png]]<br />
+
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
| class="functionType"|
 +
<span class="bigKeywordBI">int </span>
 +
<span class="bigKeywordB">StringFind</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">const string </span>
 +
<span class="bigCodeBasic">&sSrce</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const string </span>
 +
<span class="bigCodeBasic">&sSearch</span>
 +
<span class="bigCodePunc">)</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_int|int]]) Finds the position in a string of the selected substring.
 +
|-
 +
|
 +
  {| class="parameterTable" cellpadding="5%" width="100%"
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 +
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 +
  |-
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|''sSrce''
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|String to search through.
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|[[Data_Types#dataType_string|string]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|''sSearch''
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|Substring to look for.
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|[[Data_Types#dataType_string|string]]
 +
  |-
 +
  |}
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
string str = "mesHIDDENsage";                    // String 'str' is "mesGARBAGEsage".
 +
int index_of_substr;                              // Int, 'index_of_substr' to be used by "StringFind()".
  
ROBOTC generates three types of compiler messages: Errors, Warnings and Information Statements.
+
index_of_substr = StringFind(str_msg, "HIDDEN");  /* Search for the substring, "HIDDEN" */
 +
                                                  /* withing the string, 'str'.         */
 +
 +
while(true);  // infinite loop (to keep debugger running)
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 +
|-
 +
|}
 +
<br />
  
'''Errors:'''<br />
+
== StringFormat ==
ROBOTC has found an issue while compiling your program that prevented it from compiling. These are usually misspelled words, missing semicolons, and improper syntax. Errors are denoted with a <span style="background-color: black; color: red;"> '''Red X''' </span>.
+
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
| class="functionType"|
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">StringFormat</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">string </span>
 +
<span class="bigCodeBasic">&sDest</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const string </span>
 +
<span class="bigCodeBasic">sFormatString</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigCodeBasic">...</span>
 +
<span class="bigCodePunc">)</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Formats a string using the specified format-string.
 +
|-
 +
|
 +
  {| class="parameterTable" cellpadding="5%" width="100%"
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 +
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 +
  |-
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|''sDest''  
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|The string to format.
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|[[Data_Types#dataType_string|string]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|''sFormatString''
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|The format-string to apply to 'sDest'.
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|[[Data_Types#dataType_string|string]]
 +
  |-
 +
  |}
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
string str = "";                  // Create String, 'str' and initialize it as 'empty'.
 +
float num = 3.14159;              // Create float, 'num' and initialize it as first 6 digits of pi.
  
'''Warnings:'''<br />
+
StringFormat(str, "%1.2f", num);  /* Format num into a float to 2 decimal places, then send it to
ROBOTC has found a minor issue with your program, but the compiler was able to fix or ignore it to get your code to compile. These are usually incorrect capitalizations of words or infinite loops without any code inside. Warnings are denoted with a <span style="background-color: black; color: yellow;"> '''Yellow X''' </span>.
+
                                    the string, 'str'.                                         */
 +
while(true);  // infinite loop (to keep debugger running)
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 +
|-
 +
|}
 +
<br />
  
'''Information:'''<br />
+
== StringFromChars ==
ROBOTC will generate information messages when it thinks you have declared functions or variables that are not used in your program. These messages have no affect on your program and only serve the purpose of informing you about inefficient programming. Information messages are denoted with a <span style="background-color: black; color: white;"> '''White X''' </span>.
+
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
| class="functionType"|
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">StringFromChars</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">string </span>
 +
<span class="bigCodeBasic">&sToString</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const char </span>
 +
<span class="bigCodeBasic">&FromChars</span>
 +
<span class="bigCodePunc">)</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Converts an array of bytes to a string value.  You MUST end your char array with a char value of zero!
 +
|-
 +
|
 +
  {| class="parameterTable" cellpadding="5%" width="100%"
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 +
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 +
  |-
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|''sToString''
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|The string to copy characters ''to''.
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|[[Data_Types#dataType_string|string]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|''FromChars''
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|The character array to copy characters ''from''.
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|[[Data_Types#dataType_char|char]]
 +
  |-
 +
  |}
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
char test[10];    // create an array of chars of size 10 named 'test'
 +
string str = "";  // create an empty string named 'str'
 +
 
 +
test[0] = 'R';
 +
test[1] = 'O';
 +
test[2] = 'B';
 +
test[3] = 'O';
 +
test[4] = 'T';
 +
test[5] = 'C';
 +
test[6] = (char)0;  // index 6 is character value zero
 +
test[7] = 'X';      /* these characters              */
 +
test[8] = 'X';      /* never get copied              */
 +
test[9] = 'X';      /* since they are after the zero  */
 +
 
 +
StringFromChars(str, test);  // copy chars from 'test' to 'str'
 +
 
 +
while(true);  // infinite loop (to keep debugger running)
 +
}
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 +
|-
 
|}
 
|}
 
<br />
 
<br />
  
=== Breakpoints ===
+
== strncat ==
{|width="740px"
+
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
| class="functionType"|
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">strncat</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigCodeBasic">&pToBuffer</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const void </span>
 +
<span class="bigCodeBasic">&pFromBuffer</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const short </span>
 +
<span class="bigCodeBasic">nMaxBufferSize</span>
 +
<span class="bigCodePunc">)</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Function concatenates <span class="codeBasic">pFromBuffer</span> onto end of <span class="codeBasic">pToBuffer</span>. The variables are arrays of bytes terminated with a zero character. nMaxBufferSize is the maximum size of ‘pFromBuffer’ and is usually created with a <span class="keywordBI">sizeof</span><span class="codePunc">(</span><span class="codeBasic">..</span><span class="codePunc">)</span> function call. Identical to the function found in conventional C 'string.h' library file.
 +
|-
 +
|
 +
  {| class="parameterTable" cellpadding="5%" width="100%"
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 +
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 +
  |-
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|''pToBuffer''
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|'pFromBuffer' gets concatenated onto the end of this.
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 1px 0px"|''pFromBuffer''
 +
  | style="border-style: solid; border-width: 0px 0px 1px 0px"|This gets concatenated onto the end of 'pToBuffer'.
 +
  | style="border-style: solid; border-width: 0px 0px 1px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|''nMaxBufferSize''
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|How many characters to concatenate.
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|[[Data_Types#dataType_short|short]]
 +
  |-
 +
  |}
 
|-
 
|-
|Breakpoints are a useful debugging tool in ROBOTC. Breakpoints can be placed on any line of code and tell the controller to stop executing the program when that line is reached. When a breakpoint is reached, the ROBOTC debugger informs the programmer, who can then check the status of the robot, code editor, and debugger windows at that point. ROBOTC supports multiple break points.
+
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
string str1 = "ROBOT";            // String 'str1' is "ROBOT".
 +
string str2 = "C!";                // String 'str2' is "C!".
  
Breakpoints are denoted by a red circle in the gray area between the code and the line numbers.<br />
+
int copy_length = 1;              // Int 'copy_length' is set to 1.
  
'''To add a breakpoint:'''<br />
+
strncat(str1, str2, copy_length);  /* Concatinate 'copy_length' amount of characters
Right-click on the line number where you wish to add a break point and a context menu will appear. Select "Insert Breakpoint" to place a breakpoint at that line.<br /><br />
+
                                      from string 'str2' onto string 'str1'.         */
[[File:insert-breakpoint.png]]<br />''
+
  
 +
while(true);  // infinite loop (to keep debugger running)
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 +
|-
 +
|}
 +
<br />
  
'''To remove a breakpoint:'''<br />
+
== strncmp ==
Right click on the red breakpoint circle and select "Remove Breakpoint".<br /><br />
+
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
[[File:remove-breakpoint.png]]<br />
+
|-
 +
| class="functionType"|
 +
<span class="bigKeywordBI">short </span>
 +
<span class="bigKeywordB">strncmp</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigCodeBasic">&pString1</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const void </span>
 +
<span class="bigCodeBasic">&pString2</span
 +
><span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const short </span>
 +
<span class="bigCodeBasic">nMaxBufferSize</span>
 +
<span class="bigCodePunc">)</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_short|short]]) Function compares <span class="codeBasic">pString1</span> with <span class="codeBasic">pString2</span>. Returns negative value if less than, 0 if equal and positive value if greater than. The variables are arrays of bytes terminated with a zero char. <span class="codeBasic">nMaxBufferSize</span> is the maximum number of bytes to compare and is usually created with a <span class="keywordBI">sizeof</span><span class="codePunc">(</span><span class="codeBasic">..</span><span class="codePunc">)</span> function call. Identical to the function found in conventional C 'string.h'
 +
|-
 +
|
 +
  {| class="parameterTable" cellpadding="5%" width="100%"
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 +
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 +
  |-
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|''pString1''
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|A string to compare with 'pString2'.
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 1px 0px"|''pString2''
 +
  | style="border-style: solid; border-width: 0px 0px 1px 0px"|A string to compare with 'pString1'.
 +
  | style="border-style: solid; border-width: 0px 0px 1px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|''nMaxBufferSize''
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|How many characters to compare, starting from index 0.
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|[[Data_Types#dataType_short|short]]
 +
  |-
 +
  |}
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
string str1 = "Fett";                      // String 'str1' is "Fett".
 +
string str2 = "Fetts";                      // String 'str2' is "Fetts".
  
 +
int cmp_chars = 4;                          /* Int 'cmp_chars' is the amount of chars
 +
                                              to compare in "strncmp()".  Here we will
 +
                                              compare the first 4 chars of each string. */
  
'''Running your program with breakpoints:'''<br />
+
if(strncmp(str1, str2, cmp_chars) < 0)        // If 'str1' < 'str2' up to 4 chars:
When a breakpoint is in a program executing on the controller and the debugger is open, you will see the following message from ROBOTC:<br /><br />
+
{
[[File:debugger_breakpoint_info.gif]]<br />
+
  // 'str1' is < 'str2' up to 4 chars.
 
+
}
This message is just an indication that the breakpoint has been reached. Click OK and then look at your program in the code editor. ROBOTC will highlight the line of the breakpoint that program stopped at, allowing you to see exactly which breakpoint the program executed.<br /><br />
+
else if(strncmp(str1, str2, cmp_chars) > 0)  // If 'str1' > 'str2' up to 4 chars:
[[File:execution-stopped.png]]
+
{
 +
  // 'str1' is > 'str2' up to 4 chars.
 +
}
 +
else                                          // If 'str1' == 'str2' up to 4 chars:
 +
{
 +
  // 'str1' is = 'str2' up to 4 chars.
 +
}
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 +
|-
 
|}
 
|}
 
<br />
 
<br />

Latest revision as of 06:06, 21 May 2012


For information about displaying on the NXT LCD, please see the article: NXT Display Functions.

For information about displaying on the VEX LCD, please see the article: Cortex Display Functions.


When displaying floats, for example, you can tell ROBOTC how many decimals places to display. This is standard across all 'C' - like programing languages. For example, if your float is PI (3.14159265), but you only want to dispay "3.14", your string should contain, "%1.2f".

The number before the decimal is how many digits before the decimal you wish to display, while the number after the decimal is how many digits after the decimal you wish to display. So "%1.2f" tells us to display one digit before the decimal and two digits after the decimal, with "3.14" as the final result.


Color Key
Function:
Variable:


sprintf

void sprintf ( string &sString , const string sFormatString , ... )

(void) This function writes the data from sFormatString to &sString. Identical to the function found in conventional C 'string.h' library.
Parameter Explanation Data Type
&sString This is the destination string. void
sFormatString, ... These are the source strings. void
task main()
{
  string srce = "ROBOTC"; // create source variable 'srce' and set it to "ROBOTC"
  string dest;            // create destination variable 'dest'
 
  wait1Msec(1000);        // wait 1 second
 
  sprintf(dest, srce);    // write to 'dest', the contents of 'srce' (dest <- "ROBOTC)
 
  while(true);            // infinite loop (so we can watch the debbugger)
}



strcat

void strcat ( void &pToBuffer , const void &pFromBuffer )

(void) Function concatenates pFromBuffer onto end of pToBuffer. The variables are arrays of bytes terminated with zero character. It is user responsibility to ensure that the 'To' array is large enough to hold the result. ROBOTC is not able to do any range checking! Identical to the function found in conventional C 'string.h' library.
Parameter Explanation Data Type
pToBuffer Characters from 'pFromBuffer' are concatenated to the end of this. void
pFromBuffer Concatenates characters from here. void
string str1 = "ROBOT";       // string 'str1' is "ROBOT"
string str2 = "C";           // string 'str2' is "C"
 
strcat(str1, str2);          // concatinate string 'str2' onto string 'str1'
 
while(true);  // infinite loop (to keep debugger running)


strcmp

short strcmp ( void &pString1 , const void &pString2 )

(short) Function compares pString1 with pString2. Returns negative value if less than, 0 if equal and positive value if greater than. The variables are arrays of bytes terminated with a zero char. Identical to the function found in conventional C 'string.h' library.
Parameter Explanation Data Type
pString1 A string to compare with 'pString2'. void
pString2 A string to compare with 'pString1'. void
string str1 = "Fett";                       // string 'str1' is "Fett"
string str2 = "Fett";                       // string 'str2' is "Fett"
 
if(strcmp(str1, str2) < 0)                    // if 'str1' < 'str2':
{
  // 'str1' is < 'str2'
}
else if(strcmp(str1, str2) > 0)               // if 'str1' > 'str2':
{ 
  // 'str1' is > 'str2'
}
else                                          // if 'str1' == 'str2':
{
  // 'str1' is = 'str2'
}


strcpy

void strcpy ( void &pToBuffer , const void &pFromBuffer )

(void) Function copies pFromBuffer to pToBuffer. The variables are arrays of bytes terminated with a zero character. It is user responsibility to ensure that the 'To' array is large enough to hold the result. ROBOTC is not able to do any range checking! Identical to the function found in conventional C 'string.h' library.
Parameter Explanation Data Type
pToBuffer Copies 'pFromBuffer' to this. void
pFromBuffer Copies this to 'pToBuffer'. void
string str1 = "";            // 'str1' is "" (empty)
string str2 = "DNA";         // string 'str2' is "DNA"
 
strcpy(str1, str2);          // copy string 'str2' onto string 'str1'
 
while(true);  // infinite loop (to keep debugger running)


StringDelete

void StringDelete ( string &sDest , const int nIndex , const int nSize )

(void) Deletes a substring from a string.
Parameter Explanation Data Type
sDest The string to delete from. string
nIndex The index of the first character to delete (1st character = index 0). int
nSize The number of characters to delete, starting 'nIndex'. int
string str = "mesGARBAGEsage";        // String 'str' is "mesGARBAGEsage".
 
StringDelete(str, 3, 7);              // Delete from index 3 to the 7th char after that.
                                      /* Confusing because 3 = index 3, not char 3.  Since indexing */
                                      /* starts with 0, index 3 is the 4th character.               */
 
while(true);  // infinite loop (to keep debugger running)


StringFind

int StringFind ( const string &sSrce , const string &sSearch )

(int) Finds the position in a string of the selected substring.
Parameter Explanation Data Type
sSrce String to search through. string
sSearch Substring to look for. string
string str = "mesHIDDENsage";                     // String 'str' is "mesGARBAGEsage".
int index_of_substr;                              // Int, 'index_of_substr' to be used by "StringFind()".
 
index_of_substr = StringFind(str_msg, "HIDDEN");  /* Search for the substring, "HIDDEN" */
                                                  /* withing the string, 'str'.         */
 
while(true);  // infinite loop (to keep debugger running)


StringFormat

void StringFormat ( string &sDest , const string sFormatString , ... )

(void) Formats a string using the specified format-string.
Parameter Explanation Data Type
sDest The string to format. string
sFormatString The format-string to apply to 'sDest'. string
string str = "";                  // Create String, 'str' and initialize it as 'empty'.
float num = 3.14159;              // Create float, 'num' and initialize it as first 6 digits of pi.
 
StringFormat(str, "%1.2f", num);  /* Format num into a float to 2 decimal places, then send it to
                                     the string, 'str'.                                          */
while(true);  // infinite loop (to keep debugger running)


StringFromChars

void StringFromChars ( string &sToString , const char &FromChars )

(void) Converts an array of bytes to a string value. You MUST end your char array with a char value of zero!
Parameter Explanation Data Type
sToString The string to copy characters to. string
FromChars The character array to copy characters from. char
char test[10];    // create an array of chars of size 10 named 'test'
string str = "";  // create an empty string named 'str'
 
test[0] = 'R';
test[1] = 'O';
test[2] = 'B';
test[3] = 'O';
test[4] = 'T';
test[5] = 'C';
test[6] = (char)0;  // index 6 is character value zero
test[7] = 'X';      /* these characters               */
test[8] = 'X';      /* never get copied               */
test[9] = 'X';      /* since they are after the zero  */
 
StringFromChars(str, test);  // copy chars from 'test' to 'str'
 
while(true);  // infinite loop (to keep debugger running)
}


strncat

void strncat ( void &pToBuffer , const void &pFromBuffer , const short nMaxBufferSize )

(void) Function concatenates pFromBuffer onto end of pToBuffer. The variables are arrays of bytes terminated with a zero character. nMaxBufferSize is the maximum size of ‘pFromBuffer’ and is usually created with a sizeof(..) function call. Identical to the function found in conventional C 'string.h' library file.
Parameter Explanation Data Type
pToBuffer 'pFromBuffer' gets concatenated onto the end of this. void
pFromBuffer This gets concatenated onto the end of 'pToBuffer'. void
nMaxBufferSize How many characters to concatenate. short
string str1 = "ROBOT";             // String 'str1' is "ROBOT".
string str2 = "C!";                // String 'str2' is "C!".
 
int copy_length = 1;               // Int 'copy_length' is set to 1.
 
strncat(str1, str2, copy_length);  /* Concatinate 'copy_length' amount of characters 
                                      from string 'str2' onto string 'str1'.         */
 
while(true);  // infinite loop (to keep debugger running)


strncmp

short strncmp ( void &pString1 , const void &pString2, const short nMaxBufferSize )

(short) Function compares pString1 with pString2. Returns negative value if less than, 0 if equal and positive value if greater than. The variables are arrays of bytes terminated with a zero char. nMaxBufferSize is the maximum number of bytes to compare and is usually created with a sizeof(..) function call. Identical to the function found in conventional C 'string.h'
Parameter Explanation Data Type
pString1 A string to compare with 'pString2'. void
pString2 A string to compare with 'pString1'. void
nMaxBufferSize How many characters to compare, starting from index 0. short
string str1 = "Fett";                       // String 'str1' is "Fett".
string str2 = "Fetts";                      // String 'str2' is "Fetts".
 
int cmp_chars = 4;                          /* Int 'cmp_chars' is the amount of chars 
                                               to compare in "strncmp()".  Here we will 
                                               compare the first 4 chars of each string. */
 
if(strncmp(str1, str2, cmp_chars) < 0)        // If 'str1' < 'str2' up to 4 chars:
{
  // 'str1' is < 'str2' up to 4 chars.
}
else if(strncmp(str1, str2, cmp_chars) > 0)   // If 'str1' > 'str2' up to 4 chars:
{ 
  // 'str1' is > 'str2' up to 4 chars.
}
else                                          // If 'str1' == 'str2' up to 4 chars:
{
  // 'str1' is = 'str2' up to 4 chars.
}