Casio Z1 Gr User Manual
Have a look at the manual Casio Z1 Gr User Manual online for free. It’s possible to download the document as PDF or print. UserManuals.tech offer 338 Casio manuals and user’s guides for free. Share the user manual or guide on Facebook, Twitter or Google+.
81 Editing a C program Once in the editor, there are two modes for editing: the INSERT mode and the OVERWRITE mode. Switch between these two modes by pressing the INS key. The INSERT mode is automatically specified when you enter the editor. The insert mode is indicated by a cursor thet looks like “_”. While the computer is in the INSERT mode, any characters that you enter from the keyboard are inserted between any characters already on the display. You can specify the OVERWRITE mode by pressing the INS key. The OVERWRITE mode is indicated by a cursor that looks like “_”. While the computer is in the OVERWRITE mode, any character that you enter from the keyboard replace any characters already at the current cursor position. Search function The editor search function lets you search through programs for specific character strings. We will demonstrate the search function using the following display. EDIT. . main(){↵ printf(“HELLO¥n”);↵ }↵ < 1> SRCH .P. .R. main(){↵ printf(“HELLO¥n”);↵ }↵ search?pr_ < 1> . . The specified character string is located and displayed, with the cursor located at the first letter of the string. printf(“HELLO¥n”);↵ }↵ < 2> The number in the lower right of the display indicates the program line number. You can search for the next occurrence of the string without entering it again with the Shift NEXT function (written for once under the CALC key). Jumping to a specific line The editor allows you to jump directly to a certain line using the Shift LINE function and entering the line number: Shift LINE 3 main(){↵ printf(“HELLO¥n”);↵ }↵ line?3_ < 1> . .
82 }↵ < 3> Note that the line number displayed at the lower right is the one requested. Now, let’s execute our program. 6.1 C Program Execution 6.1.1 To execute a program You can exit the editor by pressing the SUB MENU key, which will go back to the C mode menu. Shift SUB MENU < C > F 0 * 2 3 4 5 6 7 8 9 51113B F1>Run/Load/Source Note that the program area 1 you just used for the short program is now shown as occupied with a “*”. You need to load the program into the interpreter, using the L key. L. Load F1 >_ Enter run and press . . to actually execute the program .R U N . .. >run HELLO >_ Success! Your computer has said HELLO to you. 6.1.1 Using Command line Operation As you are using the interpreter, the screen always shows a “>_” prompt. In this mode, you can enter four different commands followed by the . . key. Note that these commands are not case sensitive. RUN – executes the currently loaded program EDIT – transfers to the editor TRON – enters the TRACE mode TROFF – exits the TRACE mode 6.1.2 Using the Trace Function The trace function of the interpreter executes programs statement-by-statement to let you see on the display the exact flow of all of the commands. This function comes in very handy when debugging programs or when you wish to see the operation of the control structure. When the trace function is activated, the computer is in the TRACE mode.
83 To illustrate operation of the TRACE mode, we will add a sub() procedure at the top of our HELLO program, and call it in the main(). Here is how the modified program will look like: sub(){ printf(“WORLD¥n”); } main(){ printf(“HELLO¥n”); sub(); } Loading and running this program will generate following output: HELLO WORLD >_ With the TRACE function, it is possible to follow step by step the execution: .T R O N . . >tron >_ .R U N . . >run (F1-5) printf(“HELLO¥n”); Break?_ The display shows that execution is halted at line 5 of the program F1. The question mark in the bottom line of the display indicates that the computer is asking if it should execute line 5. Press . . to execute line 5. . . HELLO (F1-6) sub(); Break?_ The display shows the result of the execution of line 5, and stands by for you to press . . and execute line 6. . . Break? (F1-2) printf(“WORLD¥n”); Break?_ The execution of line 6 led the program to line 2, which is in the function sub(). . . Break? WORLD >_
84 To interrupt execution of the TRACE mode Lets run again the program in TRACE mode: .R U N . . >run (F1-5) printf(“HELLO¥n”); Break?_ 1. You can press T or . . to execute the line and carry on the TRACE mode. 2. You can press BRK to exit the program, but stay in TRACE mode. This is useful if you want to re-run the program because you missed something. To exit the TRACE mode There are four different methods that you can use to exit the TRACE mode. 1. Enter the TROFF command and press . .. “TROFF” stands for trace off. 2. Exit the interpreter while entering the editor or the MENU mode. This automatically exits the TRACE mode. 3. Switch the power of the computer OFF. This automatically exits the TRACE mode. 4. Press N during TRACE mode execution when prompted “Break?_” to exit the TRACE mode and finish executing the program. Variable Test Function During TRACE mode execution of a program, you can press D when prompted “Break?_”. .T R O N . . .R U N . . >run (F1-5) printf(“HELLO¥n”); Break?_ .D. (F1-5) printf(“HELLO¥n”); Break?d var> Entering the name of the variable followed by . . will give you the type and value of the variable at this stage of the execution of the program. Entering only . . will resume the TRACE mode execution. 6.2 Introduction to C This chapter tells you about the important points and rules to remember when creating C programs. Simply work with the example programs presented in this chapter to become familiar with proper procedure. This chapter puts you on the route to becoming a C programmer. 6.2.1 Outputting characters Creating a program to output character strings We have already some experience in chapter 6.2 with a few simple programs that output lines of characters to the display. Here, let’s try understanding how it works.
85 Enter the following program: .M A I N ( ) Shift { . . SPC .P R I N T F ( “ CAPS H CAPS O W SPC A R E SPC .Y. .O. .U. Shift .?. Shift ¥ N ” .). .;. . . SPC .P R I N T F ( “ CAPS F CAPS I N E , T H A N. .K. SPC .Y. .O. .U. Shift ¥ N ” .). .;. . . Shift } . . printf(“How are you?¥n”);↵ printf(“Fine,thank you¥n”);↵ }↵ < 4> The following shows how the program is stored in the source file: main(){ printf(“How are you?¥n”); printf(“Fine,thank you¥n”); } As already stated, C programs instructions are written using lower case characters. Also note the following characters in lines 1 and 4 of the program: main() { ……… } These are called the function that makes up the C program. The word “main” defines the function name, and the statements between the braces are actually executed. Actually, you can assign your function any name you wish, but “main” is special in that the computer will always begin execution from the function named “main”. Lines 2 and 3 contain two printf() statements. The printf() statement is actually a function of C language that is used to output characters to the display. Such functions are called standard functions. The character strings included within the parenthesis following printf are what is output to the display. Note that a character string is defined as such by being included within double quotation marks. A character string or anything else that is included within the parentheses is called an argument. The “¥n” at the end of each printf() character string is called a newline character, and advances the output to the left margin of the next line. Here the newline character is at the end of the string, but you can also include it within the string to tell the computer to go to the beginning of the next line. Note that the newline character is noted “\n” in the ANSI C language. Nevertheless, the backslash character (Ascii code 92) is not available on the pocket computer. The character coded 92 is the Yen character (see chapter 1.6). Hence, every time a backslash would be required in ANSI C, we will use the Yen character instead.
86 Making your program easy to read You have probably noticed by now that we have been writing and editing our C programs in a certain format. The previous program was written and entered as: main(){ printf(“How are you?¥n”); printf(“Fine,thank you¥n”); } We could have just as easily written and input it as: main(){ printf(“How are you?¥nFine,thank you¥n”); } Or even: main(){printf(“How are you?¥nFine,thank you¥n”);} The computer would execute the program identically in either case. The first format is preferred, however, because it is much easier to read. Though you may not see the need for such a format at this point, but when you start dealing with longer programs you will greatly appreciate an easier to read format. Creating a program to output numeric values C programs can output octal, decimal, and hexadecimal values. Here, let’s create a program that displays the vale 65 in its decimal, hexadecimal and floating-point format, using a few more new techniques. First, enter the editor and input the following: /* Output Value */ main(){ printf(“D=%d H=%x F=%f¥n”65,65,65.0); } The first line is what we call a comment line. The computer reads everything between /* and */ as a comment, and so they are ignored. You can use comment lines inside of a program to point out certain features, or as memos for later reference. Putting /* and */ around program statements is as well a good way to temporarily forbid the interpreter executing them without deleting them for good. The remainder of the program is quite similar to the one that we created to output character strings to the display, but the argument for printf() looks a bit different. The commas inside of the parentheses are there to separate arguments. This means that the printf statement in this program has a total of four arguments. printf(“D=%d H=%x F=%f¥n”65,65,65.0); | 1st argument | | | 4th argument | | 3rd argument | 2nd argument The arguments inside the parenthesis of printf tell the computer to print the following: D= H= F=
87 Note that there is a direct relationship with • the 1st % construction and the 2nd argument • the 2nd % construction and the 3rd argument • the 3rd % construction and the 4th argument. printf(“D=%d H=%x F=%f¥n”65,65,65.0); You just have to be careful to ensure that the proper % construction is matched with the proper value, or else you will get strange results. Here is a list of some other % constructions in addition to those noted above. % construction Output %d %x %f %s %c decimal integer hexadecimal integer floating point string single character Now, let’s enter the RUN command to execute our program and look at the result. .R U N . . >run D=65 H=41 F=65.000000 >_ Here we can see that the decimal equivalent of 65 is 65, the hexadecimal equivalent is 41, and the floating-point equivalent is 65.000000. Integer notation in C In the previous program, the decimal value 65 was converted in accordance with three corresponding % constructions. In some instances, however, you might want to include the actual values as they are in printf() arguments, without conversion when they are displayed. To do this, you have to specify the value as an integer constant. With C, you can specify decimal, octal and hexadecimal values, as well as characters as integer constant. Use the following formats to specify values or characters of integer constants. • 65 – displayed as decimal integer 65 • 0101 – displayed as octal value 101. Include 0 in the first digit of an octal value to specify it as an integer constant. • 0x41 – displayed as hexadecimal value 41. Include 0x in the first two digits of a hexadecimal value to display it as an integer constant. • ‘A’ – displayed as a single character. Include the character in single quotes. If you go back to our last program and change the printf() arguments as noted below: /* Output Value V2 */ main(){ printf(“D=%d O=%o H=%x C=%c¥n”65,0101,0x41, ‘A’); }
88 The following results would be displayed: .R U N . . >run D=65 O=101 H=41 C=A >_ 6.2.2 Variable types and operations Declaring variable types With C programs, you have to declare the type of data that will be assigned to each variable before you can use the variable. The following statement, for example, tells the computer that the variable “x” will be used for the storage of integers: int x; The following table shows the other declarations that can be made for variables. Declaration Meaning char short int long float double 8-bit integer 16-bit integer 16-bit integer* 32-bit integer 32-bit single precision floating point 64-bit double precision floating point Besides these, you can also include the declaration “unsigned” in front of any of the integer declaration (except long) to indicate that the integer is unsigned (no positive/negative sign) *Note: the “int” declaration is hardware dependent, despite the fact it is the default type in many cases, and the fastest for arithmetics. On the pocket computer, which uses a 16-bit microprocessor, it refers to a 16-bit integer. But on other processors, it may refer to 24-bit or 32-bit integers. For best portability, it is recommended to use the “short” declaration instead, when possible. Assigning values to variables To see how to declare variables and also how to assign values, let’s write a program that performs various operations using the values 49 and 12. Enter the editor and input the following program. /* Arithmetic Operations 1 */ main(){ short a,b,c,d,e; a=49+12;printf(“%d “,a); b=49-12;printf(“%d “,b); c=49*12;printf(“%d “,c); d=49/12;printf(“%d “,d); e=49%12;printf(“%d¥n“,e);
89 } The first four operations are addition, subtraction, multiplication and division. The value for “e” will be the modulus (remainder) of 49 divided by 12. When you execute the program, the display should appear as follows: .R U N . . >run 61 37 588 4 1 >_ The following statement in the first line of the program declares that all five variables will be for the storage of 16-bit integers. Short a,b,c,d,e; As you can see, you can use a single declaration for multiple variables by separating the variables by commas. In each of the following lines, the computer performs the calculation and assigns the result to the corresponding variable. Then, the result is displayed by including the variable as an argument in the printf() statement. Using arrays An array is a variable with depth. With an array, you use a single name followed by a number to indicate the variable name. For example, a[0], a[1], a[2], a[3], a[4] represent five memory areas within an array called “a[5]”. Note that the values following the name must be enclosed within brackets. With arrays, declaration of repetitive variables becomes very simple. Let’s go back to our original program where we used variables “a” to “e” to store calculation results, and use an array” a[5]” instead. /* Arithmetic Operations 2 */ main(){ short a[5]; a[0]=49+12;printf(“%d “,a[0]); a[1]=49-12;printf(“%d “,a[1]); a[2]=49*12;printf(“%d “,a[2]); a[3]=49/12;printf(“%d “,a[3]); a[4]=49%12;printf(“%d¥n“,a[4]); } Note that like in BASIC, an array can have multiple dimensions. The array a[3][3] would represent a total of nine values: a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2] a[2][0] a[2][1] a[2][2]
90 6.2.3 Entering characters and values Entering a single character from the keyboard Here we will create a program that outputs a character and its corresponding character code in hexadecimal format. If you press the key for the letter “B”, for example, the format will be: Char=B Hex=0x42 The standard function getchar() is used to tell the computer to get one character input from the keyboard. In most ANSI C compilers, this function is part of the standard input / output library which complements the core of C language. You need then to include this library with the following statement: #include “stdio.h” If you use mathematical functions, which are again not in the historical core of C language, you need to include the mathematical library: #include “math.h” The C interpreter of your pocket computer does not need these instructions for a compiler. Nevertheless, it is a good idea to add a comment reminding the need for the standard libraries to help portability. The following is the program that will accomplish our task: /* Diplay character codes */ /* #include */ main(){ int c; c=getchar(); printf(“Char=%c Hex=0x%x¥n“,c,c); } The getchar() function only returns the character code, and so no argument is used inside of the parenthesis. In the printf() function, %c specifies character format while %x specifies hexadecimal format. Try inputting and executing this program. When you enter the RUN command, the interpreter executes the program until it comes to the getchar() function. At that time, execution stops and the cursor flashes at the bottom of the display waiting for further input. At this time, you should enter a letter, number or symbol. The following shows the display that should result if you enter the letter “Q”. .R U N . . .Q. . . Q Char=Q Hex=0x51 >_ This shows that the character code for “Q” is hexadecimal 51.